From c6e502dd5ff049b30416e8f43d6a0389b7981037 Mon Sep 17 00:00:00 2001 From: lenisko <10072920+lenisko@users.noreply.github.com> Date: Thu, 29 Feb 2024 18:31:12 +0100 Subject: [PATCH 1/5] feat: add create/list slash commands --- discord/handler.go | 106 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 55 +++++++++++++++-------- 2 files changed, 144 insertions(+), 17 deletions(-) create mode 100644 discord/handler.go diff --git a/discord/handler.go b/discord/handler.go new file mode 100644 index 0000000..1299493 --- /dev/null +++ b/discord/handler.go @@ -0,0 +1,106 @@ +package discord + +import ( + "encoding/base64" + "fmt" + "github.com/bwmarrin/discordgo" + "os" + "path/filepath" + "strings" +) + +var ( + dmPermission = false + defaultMemberPermissions int64 = discordgo.PermissionAdministrator + + Commands = []*discordgo.ApplicationCommand{ + { + Name: "list-emotes", + Description: "List emotes", + DefaultMemberPermissions: &defaultMemberPermissions, + DMPermission: &dmPermission, + }, + { + Name: "create-emotes", + Description: "Create emotes", + DefaultMemberPermissions: &defaultMemberPermissions, + DMPermission: &dmPermission, + }, + } + + CommandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){ + "list-emotes": listEmotes, + "create-emotes": createEmotes, + } +) + +func listEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) { + var emotesList strings.Builder + + guildEmotes, _ := s.GuildEmojis(i.GuildID) + + emotesList.WriteString("```") + for _, emote := range guildEmotes { + emotesList.WriteString(fmt.Sprintf("<:%s:%s>\n", emote.Name, emote.ID)) + } + emotesList.WriteString("```") + + _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: emotesList.String(), + Flags: discordgo.MessageFlagsEphemeral, + }, + }) +} + +func createEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) { + emotesDir := "emojis" + var output strings.Builder + + files, err := os.ReadDir(emotesDir) + if err != nil { + fmt.Println("Error reading emotes directory:", err) + return + } + + // fetch existing emotes + guildEmotes, _ := s.GuildEmojis(i.GuildID) + existingEmotes := make(map[string]bool) + for _, emote := range guildEmotes { + existingEmotes[emote.Name] = true + } + + output.WriteString("```") + // check and upload every emote we have under emotesDir + for _, file := range files { + emoteName := strings.TrimSuffix(file.Name(), ".png") + + if _, exists := existingEmotes[emoteName]; exists { + output.WriteString(fmt.Sprintf("%s - already there\n", emoteName)) + continue + } + emoteFile, err := os.ReadFile(filepath.Join(emotesDir, file.Name())) + encodedImage := base64.StdEncoding.EncodeToString(emoteFile) + dataURI := fmt.Sprintf("data:image/png;base64,%s", encodedImage) + + _, err = s.GuildEmojiCreate(i.GuildID, &discordgo.EmojiParams{ + Name: emoteName, + Image: dataURI, + }) + if err != nil { + output.WriteString(fmt.Sprintf("%s - upload error: %s\n", emoteName, err)) + continue + } + output.WriteString(fmt.Sprintf("%s - success\n", emoteName)) + } + output.WriteString("```") + + _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: output.String(), + Flags: discordgo.MessageFlagsEphemeral, + }, + }) +} diff --git a/main.go b/main.go index 76de894..f8a5d2e 100644 --- a/main.go +++ b/main.go @@ -133,44 +133,71 @@ func gatherStats(db *sqlx.DB, config config.Config) (discord.GatheredStats, erro } func main() { - var config config.Config - if err := config.ParseConfig(); err != nil { + var c config.Config + if err := c.ParseConfig(); err != nil { panic(err) } messageIDs := loadMessageIDs("messageIDs.json") - dg, err := discordgo.New("Bot " + config.Discord.Token) + dg, err := discordgo.New("Bot " + c.Discord.Token) + defer dg.Close() + if err != nil { fmt.Println("error creating Discord session,", err) return } + fmt.Println("Add slash commands handlers") + dg.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { + if h, ok := discord.CommandHandlers[i.ApplicationCommandData().Name]; ok { + h(s, i) + } + }) + + fmt.Println("Open Discord connection") + err = dg.Open() + if err != nil { + fmt.Println("error opening connection,", err) + return + } + + fmt.Println("Register commands") + registeredCommands := make([]*discordgo.ApplicationCommand, len(discord.Commands)) + for i, v := range discord.Commands { + cmd, err := dg.ApplicationCommandCreate(dg.State.User.ID, "", v) + if err != nil { + fmt.Printf("Cannot create '%v' command: %v\n", v.Name, err) + } + registeredCommands[i] = cmd + } + + fmt.Println("Start loop") go func() { for { - db, err := database.DbConn(config) + db, err := database.DbConn(c) if err != nil { fmt.Println("error connecting to MariaDB,", err) - time.Sleep(time.Duration(config.Config.ErrorRefreshInterval) * time.Second) + time.Sleep(time.Duration(c.Config.ErrorRefreshInterval) * time.Second) continue } - gathered, err := gatherStats(db, config) + gathered, err := gatherStats(db, c) db.Close() if err != nil { fmt.Println("failed to fetch stats,", err) - time.Sleep(time.Duration(config.Config.ErrorRefreshInterval) * time.Second) + time.Sleep(time.Duration(c.Config.ErrorRefreshInterval) * time.Second) continue } - fields := discord.GenerateFields(gathered, config) + fields := discord.GenerateFields(gathered, c) embed := &discordgo.MessageEmbed{ - Title: config.Config.EmbedTitle, + Title: c.Config.EmbedTitle, Fields: fields, Timestamp: time.Now().Format(time.RFC3339), } - for _, channelID := range config.Discord.ChannelIDs { + for _, channelID := range c.Discord.ChannelIDs { var msg *discordgo.Message var err error var msgID string @@ -194,16 +221,10 @@ func main() { } } - time.Sleep(time.Duration(config.Config.RefreshInterval) * time.Second) + time.Sleep(time.Duration(c.Config.RefreshInterval) * time.Second) } }() - err = dg.Open() - if err != nil { - fmt.Println("error opening connection,", err) - return - } - fmt.Println("Porygon is now running. Press CTRL-C to exit.") <-make(chan struct{}) return From 0cbf08fa1f052ff6b686e73025d0cd9265fdf2bb Mon Sep 17 00:00:00 2001 From: lenisko <10072920+lenisko@users.noreply.github.com> Date: Fri, 1 Mar 2024 02:16:20 +0100 Subject: [PATCH 2/5] feat: use log, greaceful shutdown, use discord reffer for create cmd --- config/parser.go | 6 +++--- database/db.go | 8 +++---- discord/constructor.go | 9 ++++---- discord/handler.go | 37 ++++++++++++++++++++++----------- emojis/override/.gitkeep | 0 main.go | 45 +++++++++++++++++++++++----------------- 6 files changed, 63 insertions(+), 42 deletions(-) create mode 100644 emojis/override/.gitkeep diff --git a/config/parser.go b/config/parser.go index 067c3fe..2f4912e 100644 --- a/config/parser.go +++ b/config/parser.go @@ -1,18 +1,18 @@ package config import ( - "fmt" "github.com/BurntSushi/toml" + "log" ) func (config *Config) ParseConfig() error { if _, err := toml.DecodeFile("default.toml", &config); err != nil { - fmt.Println("error decoding default config file,", err) + log.Println("error decoding default config file,", err) return err } if _, err := toml.DecodeFile("config.toml", &config); err != nil { - fmt.Println("error decoding user config file,", err) + log.Println("error decoding user config file,", err) return err } diff --git a/database/db.go b/database/db.go index 5e3a17d..d8aa512 100644 --- a/database/db.go +++ b/database/db.go @@ -1,9 +1,9 @@ package database import ( - "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" + "log" "porygon/config" ) @@ -74,7 +74,7 @@ func GetRaidStats(db *sqlx.DB) ([]RaidStats, error) { ` err := db.Select(&raidStatsList, query) if err != nil { - fmt.Println(err) + log.Println(err) return nil, err } @@ -97,7 +97,7 @@ func GetGymStats(db *sqlx.DB) (GymStats, error) { `) if err != nil { - fmt.Println(err) + log.Println(err) return GymStats{}, err } @@ -132,7 +132,7 @@ func GetRewardStats(db *sqlx.DB) ([]TypeCountStats, error) { GROUP BY reward_type `) if err != nil { - fmt.Println(err) + log.Println(err) return rewardStatsList, err } diff --git a/discord/constructor.go b/discord/constructor.go index 1770918..7fd52a2 100644 --- a/discord/constructor.go +++ b/discord/constructor.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/bwmarrin/discordgo" "github.com/dustin/go-humanize" + "log" "os" "porygon/config" "porygon/database" @@ -90,7 +91,7 @@ func GenerateFields(gathered GatheredStats, config config.Config) []*discordgo.M if err != nil { currentTemplateFile, err = os.ReadFile("templates/current.json") if err != nil { - panic(err) + log.Panicln(err) } } @@ -104,17 +105,17 @@ func GenerateFields(gathered GatheredStats, config config.Config) []*discordgo.M "EventEmoji": func(level int) string { return convertToEmoji(level, config.EventEmoji) }, }).Parse(string(currentTemplateFile)) if err != nil { - panic(err) + log.Panicln(err) } var resultJSON bytes.Buffer if err := tmpl.Execute(&resultJSON, gathered); err != nil { - panic(err) + log.Panicln(err) } var fields []*discordgo.MessageEmbedField if err := json.Unmarshal(resultJSON.Bytes(), &fields); err != nil { - panic(err) + log.Panicln(err) } return fields diff --git a/discord/handler.go b/discord/handler.go index 1299493..1678c39 100644 --- a/discord/handler.go +++ b/discord/handler.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "fmt" "github.com/bwmarrin/discordgo" + "log" "os" "path/filepath" "strings" @@ -55,12 +56,29 @@ func listEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) { } func createEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) { - emotesDir := "emojis" var output strings.Builder + output.WriteString("```") + + _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseDeferredChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Flags: discordgo.MessageFlagsEphemeral, + }, + }) + + emotesDir := "emojis/override" + files, err := filepath.Glob(filepath.Join(emotesDir, "*.png")) + + if len(files) > 0 { + output.WriteString("Using `emojis/override` directory as emotes source.\n") + } else { + output.WriteString("Using `emojis` directory as emotes source.\n") + emotesDir = "emojis" + files, err = filepath.Glob(filepath.Join(emotesDir, "*.png")) + } - files, err := os.ReadDir(emotesDir) if err != nil { - fmt.Println("Error reading emotes directory:", err) + log.Println("Error reading emotes directory:", err) return } @@ -71,16 +89,15 @@ func createEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) { existingEmotes[emote.Name] = true } - output.WriteString("```") // check and upload every emote we have under emotesDir for _, file := range files { - emoteName := strings.TrimSuffix(file.Name(), ".png") + emoteName := strings.TrimSuffix(filepath.Base(file), ".png") if _, exists := existingEmotes[emoteName]; exists { output.WriteString(fmt.Sprintf("%s - already there\n", emoteName)) continue } - emoteFile, err := os.ReadFile(filepath.Join(emotesDir, file.Name())) + emoteFile, err := os.ReadFile(filepath.Join(emotesDir, filepath.Base(file))) encodedImage := base64.StdEncoding.EncodeToString(emoteFile) dataURI := fmt.Sprintf("data:image/png;base64,%s", encodedImage) @@ -96,11 +113,7 @@ func createEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) { } output.WriteString("```") - _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: output.String(), - Flags: discordgo.MessageFlagsEphemeral, - }, + _, _ = s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{ + Content: output.String(), }) } diff --git a/emojis/override/.gitkeep b/emojis/override/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/main.go b/main.go index f8a5d2e..5a1585c 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,13 @@ package main import ( "encoding/json" - "fmt" "github.com/bwmarrin/discordgo" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" + "log" "os" + "os/signal" + "syscall" "time" "porygon/api" @@ -18,7 +20,7 @@ import ( func saveMessageIDs(filename string, messageIDs map[string]string) { file, err := os.Create(filename) if err != nil { - fmt.Println("error creating message IDs file:", err) + log.Println("error creating message IDs file:", err) return } defer file.Close() @@ -32,7 +34,7 @@ func loadMessageIDs(filename string) map[string]string { if _, err := os.Stat(filename); os.IsNotExist(err) { file, err := os.Create(filename) if err != nil { - fmt.Println("error creating message IDs file:", err) + log.Println("error creating message IDs file:", err) return messageIDs } defer file.Close() @@ -41,7 +43,7 @@ func loadMessageIDs(filename string) map[string]string { } else { file, err := os.Open(filename) if err != nil { - fmt.Println("error opening message IDs file:", err) + log.Println("error opening message IDs file:", err) return messageIDs } defer file.Close() @@ -128,14 +130,15 @@ func gatherStats(db *sqlx.DB, config config.Config) (discord.GatheredStats, erro } elapsed := time.Since(start) - fmt.Printf("Fetched stats in %s\n", elapsed) + log.Printf("Fetched stats in %s\n", elapsed) return gathered, nil } func main() { var c config.Config + log.Println("Starting porygon") if err := c.ParseConfig(); err != nil { - panic(err) + log.Panicln(err) } messageIDs := loadMessageIDs("messageIDs.json") @@ -144,47 +147,48 @@ func main() { defer dg.Close() if err != nil { - fmt.Println("error creating Discord session,", err) + log.Println("error creating Discord session,", err) return } - fmt.Println("Add slash commands handlers") + log.Println("Add slash commands handlers") dg.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { if h, ok := discord.CommandHandlers[i.ApplicationCommandData().Name]; ok { h(s, i) } }) - fmt.Println("Open Discord connection") + log.Println("Open Discord connection") err = dg.Open() if err != nil { - fmt.Println("error opening connection,", err) + log.Println("error opening connection,", err) return } - fmt.Println("Register commands") + log.Println("Register commands") registeredCommands := make([]*discordgo.ApplicationCommand, len(discord.Commands)) for i, v := range discord.Commands { cmd, err := dg.ApplicationCommandCreate(dg.State.User.ID, "", v) if err != nil { - fmt.Printf("Cannot create '%v' command: %v\n", v.Name, err) + log.Printf("Cannot create '%v' command: %v\n", v.Name, err) } registeredCommands[i] = cmd } - fmt.Println("Start loop") + log.Println("Start loop") go func() { for { db, err := database.DbConn(c) if err != nil { - fmt.Println("error connecting to MariaDB,", err) + log.Println("error connecting to MariaDB,", err) time.Sleep(time.Duration(c.Config.ErrorRefreshInterval) * time.Second) continue } gathered, err := gatherStats(db, c) db.Close() + if err != nil { - fmt.Println("failed to fetch stats,", err) + log.Println("failed to fetch stats,", err) time.Sleep(time.Duration(c.Config.ErrorRefreshInterval) * time.Second) continue } @@ -213,7 +217,7 @@ func main() { } if err != nil { - fmt.Println("error sending or editing message in channel", channelID, ":", err) + log.Println("error sending or editing message in channel", channelID, ":", err) continue } else if msgID == "" || msgID != msg.ID { messageIDs[channelID] = msg.ID @@ -225,7 +229,10 @@ func main() { } }() - fmt.Println("Porygon is now running. Press CTRL-C to exit.") - <-make(chan struct{}) - return + log.Println("Porygon is now running. Press CTRL-C to exit.") + stop := make(chan os.Signal, 1) + signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM) + <-stop + + log.Println("Received signal. Exiting...") } From f901df2f7e014b6a13fa602a3bf062571e164dae Mon Sep 17 00:00:00 2001 From: lenisko <10072920+lenisko@users.noreply.github.com> Date: Fri, 1 Mar 2024 13:45:15 +0100 Subject: [PATCH 3/5] chore: add emoijis/override/* to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 7b85209..8745a1f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ messageIDs.json Porygon porygon templates/*.override.json +emojis/override/* .idea/ From deeaab0f95e019304dbcb9722c7db486b1f68ed7 Mon Sep 17 00:00:00 2001 From: lenisko <10072920+lenisko@users.noreply.github.com> Date: Fri, 1 Mar 2024 14:02:32 +0100 Subject: [PATCH 4/5] fix: log to message, more bulletproof --- discord/handler.go | 65 ++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/discord/handler.go b/discord/handler.go index 1678c39..a2cf122 100644 --- a/discord/handler.go +++ b/discord/handler.go @@ -4,7 +4,6 @@ import ( "encoding/base64" "fmt" "github.com/bwmarrin/discordgo" - "log" "os" "path/filepath" "strings" @@ -57,7 +56,6 @@ func listEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) { func createEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) { var output strings.Builder - output.WriteString("```") _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseDeferredChannelMessageWithSource, @@ -78,40 +76,45 @@ func createEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) { } if err != nil { - log.Println("Error reading emotes directory:", err) - return - } - - // fetch existing emotes - guildEmotes, _ := s.GuildEmojis(i.GuildID) - existingEmotes := make(map[string]bool) - for _, emote := range guildEmotes { - existingEmotes[emote.Name] = true - } + output.WriteString(fmt.Sprintf("Error reading emotes directory: %s\n", err)) + } else { + output.WriteString("```") - // check and upload every emote we have under emotesDir - for _, file := range files { - emoteName := strings.TrimSuffix(filepath.Base(file), ".png") + // fetch existing emotes + guildEmotes, _ := s.GuildEmojis(i.GuildID) + existingEmotes := make(map[string]bool) + for _, emote := range guildEmotes { + existingEmotes[emote.Name] = true + } - if _, exists := existingEmotes[emoteName]; exists { - output.WriteString(fmt.Sprintf("%s - already there\n", emoteName)) - continue + if len(files) == 0 { + output.WriteString("no files to upload") } - emoteFile, err := os.ReadFile(filepath.Join(emotesDir, filepath.Base(file))) - encodedImage := base64.StdEncoding.EncodeToString(emoteFile) - dataURI := fmt.Sprintf("data:image/png;base64,%s", encodedImage) - - _, err = s.GuildEmojiCreate(i.GuildID, &discordgo.EmojiParams{ - Name: emoteName, - Image: dataURI, - }) - if err != nil { - output.WriteString(fmt.Sprintf("%s - upload error: %s\n", emoteName, err)) - continue + + // check and upload every emote we have under emotesDir + for _, file := range files { + emoteName := strings.TrimSuffix(filepath.Base(file), ".png") + + if _, exists := existingEmotes[emoteName]; exists { + output.WriteString(fmt.Sprintf("%s - already there\n", emoteName)) + continue + } + emoteFile, err := os.ReadFile(file) + encodedImage := base64.StdEncoding.EncodeToString(emoteFile) + dataURI := fmt.Sprintf("data:image/png;base64,%s", encodedImage) + + _, err = s.GuildEmojiCreate(i.GuildID, &discordgo.EmojiParams{ + Name: emoteName, + Image: dataURI, + }) + if err != nil { + output.WriteString(fmt.Sprintf("%s - upload error: %s\n", emoteName, err)) + continue + } + output.WriteString(fmt.Sprintf("%s - success\n", emoteName)) } - output.WriteString(fmt.Sprintf("%s - success\n", emoteName)) + output.WriteString("```") } - output.WriteString("```") _, _ = s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{ Content: output.String(), From 4981dea601047cac583785bf59d2384501a11e47 Mon Sep 17 00:00:00 2001 From: lenisko <10072920+lenisko@users.noreply.github.com> Date: Fri, 1 Mar 2024 14:13:54 +0100 Subject: [PATCH 5/5] feat: add delete-emotes --- discord/handler.go | 59 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/discord/handler.go b/discord/handler.go index a2cf122..9415457 100644 --- a/discord/handler.go +++ b/discord/handler.go @@ -16,13 +16,19 @@ var ( Commands = []*discordgo.ApplicationCommand{ { Name: "list-emotes", - Description: "List emotes", + Description: "List all guild emotes", DefaultMemberPermissions: &defaultMemberPermissions, DMPermission: &dmPermission, }, { Name: "create-emotes", - Description: "Create emotes", + Description: "Create Porygon emotes", + DefaultMemberPermissions: &defaultMemberPermissions, + DMPermission: &dmPermission, + }, + { + Name: "delete-emotes", + Description: "Delete all emotes (created by Porygon)", DefaultMemberPermissions: &defaultMemberPermissions, DMPermission: &dmPermission, }, @@ -31,6 +37,7 @@ var ( CommandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){ "list-emotes": listEmotes, "create-emotes": createEmotes, + "delete-emotes": deleteEmotes, } ) @@ -39,11 +46,15 @@ func listEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) { guildEmotes, _ := s.GuildEmojis(i.GuildID) - emotesList.WriteString("```") - for _, emote := range guildEmotes { - emotesList.WriteString(fmt.Sprintf("<:%s:%s>\n", emote.Name, emote.ID)) + if len(guildEmotes) > 0 { + emotesList.WriteString("```") + for _, emote := range guildEmotes { + emotesList.WriteString(fmt.Sprintf("<:%s:%s>\n", emote.Name, emote.ID)) + } + emotesList.WriteString("```") + } else { + emotesList.WriteString("No guild emotes.") } - emotesList.WriteString("```") _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, @@ -54,6 +65,42 @@ func listEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) { }) } +func deleteEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) { + var output strings.Builder + + _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseDeferredChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Flags: discordgo.MessageFlagsEphemeral, + }, + }) + + guildEmotes, _ := s.GuildEmojis(i.GuildID) + + if len(guildEmotes) > 0 { + output.WriteString("```") + for _, emote := range guildEmotes { + if emote.User.ID == s.State.User.ID { + err := s.GuildEmojiDelete(i.GuildID, emote.ID) + if err != nil { + output.WriteString(fmt.Sprintf("%s - failed to remove: %s\n", emote.Name, err)) + } else { + output.WriteString(fmt.Sprintf("%s - removed\n", emote.Name)) + } + } else { + output.WriteString(fmt.Sprintf("%s - skipping, other owner %s\n", emote.Name, emote.User.String())) + } + } + output.WriteString("```") + } else { + output.WriteString("No guild emotes to delete.") + } + + _, _ = s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{ + Content: output.String(), + }) +} + func createEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) { var output strings.Builder