package botEvents import ( "corn-util/bot/loaders" "corn-util/bot/toolbox" "encoding/json" "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." attemptFailText = "There was an attempt..." noConfigValText = "Unconfigured" ) 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 "repository": e.CreateMessage(discord.MessageCreate{ Components: []discord.ContainerComponent{ discord.ActionRowComponent{ discord.ButtonComponent{ Label: "Check out the repository!", Style: discord.ButtonStyleLink, URL: "https://git.toast-server.net/toast/Corn-Utility.git", }, }, }, }) break 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 } } } 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: attemptFailText, 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: attemptFailText, Description: fmt.Sprintf("```%v```", err.Error()), Color: 0x560000, }, }, }); err != nil { DumpErrToConsole(err) } } func isGuildManager(e *events.ApplicationCommandInteractionCreate) bool { if e.Member().Permissions.Has(discord.PermissionManageGuild) { return true } else { return false } }