stabilized command registering

This commit is contained in:
2024-02-22 09:32:40 +01:00
parent ca6a199244
commit cf5e81f65c
2 changed files with 18 additions and 9 deletions

View File

@@ -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()
}

View File

@@ -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,15 +53,20 @@ 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 {
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)
@@ -64,3 +74,4 @@ func removeCommandFromAllGuilds(s *discordgo.Session) {
}
}
}
}