From cf5e81f65ccbc9c3f0e5c7ea0d3544cf75ff2c3a Mon Sep 17 00:00:00 2001 From: vaporvee Date: Thu, 22 Feb 2024 09:32:40 +0100 Subject: [PATCH] stabilized command registering --- main.go | 2 -- register_commands.go | 25 ++++++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index c1ae5fa..34b770e 100644 --- a/main.go +++ b/main.go @@ -39,7 +39,6 @@ func main() { fmt.Println("Discord session created") } discord.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuilds | discordgo.IntentMessageContent - defer removeCommandFromAllGuilds(discord) discord.AddHandler(ready) discord.AddHandler(interactionCreate) discord.AddHandler(messageCreate) @@ -53,7 +52,6 @@ func main() { signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-sc fmt.Println("\nShutting down...") - defer removeCommandFromAllGuilds(discord) discord.Close() } diff --git a/register_commands.go b/register_commands.go index d0e0576..69c9944 100644 --- a/register_commands.go +++ b/register_commands.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "slices" "strings" "github.com/bwmarrin/discordgo" @@ -15,18 +16,22 @@ type Command struct { ModalID string } -var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command, ask_command, sticky_command} +var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command, ask_command, sticky_command, cat_command} func ready(s *discordgo.Session, event *discordgo.Ready) { + fmt.Print("\nStarting up... (This may take a bit when Discord rate limits the bot)\n") + removeOldCommandFromAllGuilds(s) for _, guild := range event.Guilds { for _, command := range commands { - _, err := s.ApplicationCommandCreate(s.State.User.ID, guild.ID, &command.Definition) + cmd, err := s.ApplicationCommandCreate(s.State.User.ID, guild.ID, &command.Definition) + fmt.Printf("\nAdded command \"%s\"", cmd.Name) if err != nil { fmt.Println("error creating command,", err) continue } } } + fmt.Print("Successfully started the Discord bot!") } func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) { @@ -48,18 +53,24 @@ func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) { } } -func removeCommandFromAllGuilds(s *discordgo.Session) { +func removeOldCommandFromAllGuilds(s *discordgo.Session) { for _, guild := range s.State.Guilds { existingCommands, err := s.ApplicationCommands(s.State.User.ID, guild.ID) if err != nil { fmt.Printf("error fetching existing commands for guild %s: %v\n", guild.Name, err) continue } - + var commandIDs []string + for _, command := range commands { + commandIDs = append(commandIDs, command.Definition.Name) + } for _, existingCommand := range existingCommands { - err := s.ApplicationCommandDelete(s.State.User.ID, guild.ID, existingCommand.ID) - if err != nil { - fmt.Printf("error deleting command %s for guild %s: %v\n", existingCommand.Name, guild.Name, err) + if !slices.Contains(commandIDs, existingCommand.Name) { + fmt.Printf("\nDeleting command \"%s\"", existingCommand.Name) + err := s.ApplicationCommandDelete(s.State.User.ID, guild.ID, existingCommand.ID) + if err != nil { + fmt.Printf("error deleting command %s for guild %s: %v\n", existingCommand.Name, guild.Name, err) + } } } }