improved codebase a bit

This commit is contained in:
2024-02-19 14:45:18 +01:00
parent 4f8c33e880
commit 07f4de78e4
3 changed files with 91 additions and 84 deletions

55
register_commands.go Normal file
View File

@@ -0,0 +1,55 @@
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
)
type Command struct {
Definition discordgo.ApplicationCommand
Interact func(s *discordgo.Session, i *discordgo.InteractionCreate)
}
func ready(s *discordgo.Session, event *discordgo.Ready) {
commands := []*discordgo.ApplicationCommand{
&tag_command.Definition,
&short_get_tag_command.Definition,
}
for _, guild := range event.Guilds {
for _, command := range commands {
_, err := s.ApplicationCommandCreate(s.State.User.ID, guild.ID, command)
if err != nil {
fmt.Println("error creating command,", err)
continue // Continue to the next guild
}
}
}
}
func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.ApplicationCommandData().Name {
case "tag":
tag_command.Interact(s, i)
case "g":
short_get_tag_command.Interact(s, i)
}
}
func removeCommandFromAllGuilds(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
}
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)
}
}
}
}