2023-10-19 18:08:38 -04:00
|
|
|
package botEvents
|
|
|
|
|
|
|
|
import (
|
2023-10-20 01:18:13 -04:00
|
|
|
"corn-util/bot/loaders"
|
2023-10-19 18:08:38 -04:00
|
|
|
"corn-util/bot/toolbox"
|
2023-10-20 01:18:13 -04:00
|
|
|
"encoding/json"
|
2023-10-19 18:08:38 -04:00
|
|
|
"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 (
|
2023-10-20 01:18:13 -04:00
|
|
|
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."
|
|
|
|
attemptFailText = "There was an attempt..."
|
|
|
|
noConfigValText = "Unconfigured"
|
2023-10-19 18:08:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2023-10-20 01:18:13 -04:00
|
|
|
case "repository":
|
2023-10-19 18:08:38 -04:00
|
|
|
e.CreateMessage(discord.MessageCreate{
|
|
|
|
Components: []discord.ContainerComponent{
|
|
|
|
discord.ActionRowComponent{
|
|
|
|
discord.ButtonComponent{
|
2023-10-20 01:18:13 -04:00
|
|
|
Label: "Check out the repsotiry!",
|
2023-10-19 18:08:38 -04:00
|
|
|
Style: discord.ButtonStyleLink,
|
2023-10-20 01:18:13 -04:00
|
|
|
URL: "https://git.toast-server.net/toast/Corn-Utility.git",
|
2023-10-19 18:08:38 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
break
|
2023-10-20 01:18:13 -04:00
|
|
|
case "config":
|
|
|
|
type jsonDataStruct struct {
|
|
|
|
BanRecords string `json:"banRecords"`
|
|
|
|
GoogleSheets string `json:"googleSheets"`
|
|
|
|
}
|
|
|
|
readData, _ := loaders.DataLoader.Read(&loaders.JSON{}, "config.json")
|
|
|
|
jsonData := jsonDataStruct{}
|
|
|
|
jsonDataBytes, _ := json.Marshal(readData)
|
|
|
|
json.Unmarshal(jsonDataBytes, &jsonData)
|
|
|
|
fmt.Println(jsonData)
|
|
|
|
|
|
|
|
banRecordsCh, _ := e.SlashCommandInteractionData().OptChannel("ban-records")
|
|
|
|
googleSheetsURL, _ := e.SlashCommandInteractionData().OptString("google-sheets")
|
|
|
|
|
|
|
|
if !isGuildManager(e) {
|
|
|
|
DumpErrToInteraction(e, fmt.Errorf(noPermText))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(e.SlashCommandInteractionData().Options) == 0 {
|
|
|
|
e.CreateMessage(discord.MessageCreate{
|
|
|
|
Embeds: []discord.Embed{
|
|
|
|
{
|
|
|
|
Title: "Current configuration",
|
|
|
|
Description: "You can configure the bot using the options below.",
|
|
|
|
Fields: []discord.EmbedField{
|
|
|
|
{
|
|
|
|
Name: "Ban Records",
|
|
|
|
Value: func() string {
|
|
|
|
if jsonData.BanRecords == "" || jsonData.BanRecords == "0" {
|
|
|
|
return noConfigValText
|
|
|
|
} else {
|
|
|
|
return fmt.Sprintf("<#%v>", jsonData.BanRecords)
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
Inline: &TRUE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Google Sheets",
|
|
|
|
Value: func() string {
|
|
|
|
if jsonData.GoogleSheets == "" {
|
|
|
|
return noConfigValText
|
|
|
|
} else {
|
|
|
|
return fmt.Sprintf("`%v`", jsonData.GoogleSheets)
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
Inline: &TRUE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Color: mainEmbedColor,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if banRecordsCh.Permissions.Has(discord.PermissionSendMessages, discord.PermissionEmbedLinks) {
|
|
|
|
// Create a placeholder embed in the configured channel.
|
|
|
|
if _, err := e.Client().Rest().CreateMessage(banRecordsCh.ID, discord.MessageCreate{
|
|
|
|
Embeds: []discord.Embed{
|
|
|
|
{
|
|
|
|
Description: "This is a placeholder embed for ban records.\n" +
|
|
|
|
"Once the bot is configured, this will be updated with the latest ban records from Google Sheets.",
|
|
|
|
Color: mainEmbedColor,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
DumpErrToChannel(e, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("google-sheets: %v\n", googleSheetsURL)
|
|
|
|
if err := e.CreateMessage(discord.MessageCreate{
|
|
|
|
Embeds: []discord.Embed{{Title: "Config saved!", Color: mainEmbedColor}},
|
|
|
|
}); err != nil {
|
|
|
|
DumpErrToInteraction(e, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the config file with the new configuration.
|
|
|
|
data := map[string]interface{}{}
|
|
|
|
|
|
|
|
if banRecordsCh.ID.String() != "0" {
|
|
|
|
data["banRecords"] = banRecordsCh.ID
|
|
|
|
} else {
|
|
|
|
data["banRecords"] = jsonData.BanRecords
|
|
|
|
}
|
|
|
|
|
|
|
|
if googleSheetsURL != "" {
|
|
|
|
data["googleSheets"] = googleSheetsURL
|
|
|
|
} else {
|
|
|
|
data["googleSheets"] = jsonData.GoogleSheets
|
|
|
|
}
|
|
|
|
|
|
|
|
err := loaders.DataLoader.Write(&loaders.JSON{}, "config.json", data)
|
|
|
|
if err != nil {
|
|
|
|
DumpErrToChannel(e, err)
|
|
|
|
return
|
|
|
|
}
|
2023-10-19 18:08:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
|
|
|
{
|
2023-10-20 01:18:13 -04:00
|
|
|
Title: attemptFailText,
|
2023-10-19 18:08:38 -04:00
|
|
|
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{
|
|
|
|
{
|
2023-10-20 01:18:13 -04:00
|
|
|
Title: attemptFailText,
|
2023-10-19 18:08:38 -04:00
|
|
|
Description: fmt.Sprintf("```%v```", err.Error()),
|
|
|
|
Color: 0x560000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
DumpErrToConsole(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isGuildManager(e *events.ApplicationCommandInteractionCreate) bool {
|
2023-10-20 01:18:13 -04:00
|
|
|
if e.Member().Permissions.Has(discord.PermissionManageGuild) {
|
2023-10-19 18:08:38 -04:00
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|