This repository has been archived on 2024-05-11. You can view files and clone it, but cannot push or open issues or pull requests.
Corn-Utility/events/interaction.go
toast c4ddf77a7b
All checks were successful
Build and push container image / build (push) Successful in 41s
Upload the rest of files
2023-10-20 09:08:38 +11:00

142 lines
3.6 KiB
Go

package botEvents
import (
"corn-util/bot/toolbox"
"fmt"
"runtime"
"strings"
"time"
"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/log"
"github.com/dustin/go-humanize"
"github.com/mackerelio/go-osstat/cpu"
"github.com/shirou/gopsutil/v3/mem"
"github.com/zcalusic/sysinfo"
)
var (
mainEmbedColor = 0xff69b4 //0xf9c62c - main embed color for the bot (saffron / yellow)
noPermText = "You need to have a role with `Manage Server` permission to use this command."
)
func ListenForCommand(e *events.ApplicationCommandInteractionCreate) {
TRUE := true
switch name := e.Data.CommandName(); name {
case "host-stats":
before, err := cpu.Get()
if err != nil {
DumpErrToConsole(err)
return
}
time.Sleep(time.Duration(1) * time.Second)
after, err := cpu.Get()
if err != nil {
DumpErrToConsole(err)
return
}
total := float64(after.Total - before.Total)
memInfo, _ := mem.VirtualMemory()
var si sysinfo.SysInfo
si.GetSysInfo()
if err := e.CreateMessage(discord.MessageCreate{
Embeds: []discord.Embed{
{
Description: fmt.Sprintf("**OS:** %s\n**CPU:** %s", si.OS.Name, si.CPU.Model),
Fields: []discord.EmbedField{
{
Name: "CPU Usage",
Value: fmt.Sprintf("User: %.1f %%\nSys: %.1f %%", float64(after.User-before.User)/total*100, float64(after.System-before.System)/total*100),
Inline: &TRUE,
},
{
Name: "Memory",
Value: fmt.Sprintf("Used: %v\nTotal: %v", humanize.IBytes(memInfo.Used), humanize.IBytes(memInfo.Total)),
Inline: &TRUE,
},
{
Name: "\u200b",
Value: "\u200b",
Inline: &TRUE,
},
{
Name: "Version",
Value: fmt.Sprintf("Disgo %v\nGo %v", disgo.Version, strings.TrimPrefix(runtime.Version(), "go")),
Inline: &TRUE,
},
{
Name: "Goroutines",
Value: fmt.Sprintf("%v", runtime.NumGoroutine()),
Inline: &TRUE,
},
},
Footer: &discord.EmbedFooter{
Text: fmt.Sprintf("Uptime: %v", toolbox.GetUptime()),
},
Color: mainEmbedColor,
},
},
}); err != nil {
DumpErrToConsole(err)
}
break
case "invite":
e.CreateMessage(discord.MessageCreate{
Components: []discord.ContainerComponent{
discord.ActionRowComponent{
discord.ButtonComponent{
Label: "Invite me!",
Style: discord.ButtonStyleLink,
URL: fmt.Sprintf("https://discord.com/api/oauth2/authorize?client_id=%v&permissions=19456&scope=bot", e.Client().ApplicationID()),
},
},
},
})
break
}
}
func DumpErrToConsole(err error) {
log.Errorf("failed to send interaction response: %v", err.Error())
}
func DumpErrToInteraction(e *events.ApplicationCommandInteractionCreate, err error) {
if err := e.CreateMessage(discord.MessageCreate{
Embeds: []discord.Embed{
{
Title: "There was an attempt...",
Description: fmt.Sprintf("```%v```", err.Error()),
Color: 0x560000,
},
},
}); err != nil {
DumpErrToConsole(err)
}
}
func DumpErrToChannel(e *events.ApplicationCommandInteractionCreate, err error) {
if _, err := e.Client().Rest().CreateMessage(e.Channel().ID(), discord.MessageCreate{
Embeds: []discord.Embed{
{
Title: "There was an attempt...",
Description: fmt.Sprintf("```%v```", err.Error()),
Color: 0x560000,
},
},
}); err != nil {
DumpErrToConsole(err)
}
}
func isGuildManager(e *events.ApplicationCommandInteractionCreate) bool {
member := e.Member()
if member.Permissions.Has(discord.PermissionManageGuild) {
return true
} else {
return false
}
}