From 91990e0a10c7af9e67c780be64a3d900f4511898 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Thu, 22 Feb 2024 09:45:14 +0100 Subject: [PATCH] now actually stabilized command registering --- register_commands.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/register_commands.go b/register_commands.go index 69c9944..ce7eb35 100644 --- a/register_commands.go +++ b/register_commands.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "os" "slices" "strings" @@ -19,19 +20,28 @@ type Command struct { 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) + var existingCommandNames []string for _, guild := range event.Guilds { + existingCommands, err := s.ApplicationCommands(s.State.User.ID, guild.ID) + for _, existingCommand := range existingCommands { + existingCommandNames = append(existingCommandNames, existingCommand.Name) + } + if err != nil { + fmt.Printf("error fetching existing commands for guild %s: %v\n", guild.Name, err) + continue + } for _, command := range commands { - 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 + if !slices.Contains(existingCommandNames, command.Definition.Name) || slices.Contains(os.Args, "--update") { + cmd, err := s.ApplicationCommandCreate(s.State.User.ID, guild.ID, &command.Definition) + fmt.Printf("\nDeleted 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) {