diff --git a/command_tag.go b/command_tag.go index 4f7722d..4bce44f 100644 --- a/command_tag.go +++ b/command_tag.go @@ -61,7 +61,37 @@ var tag_command Command = Command{ }, }, }, - }} + }, + Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + switch i.ApplicationCommandData().Options[0].Name { + case "get": + GetTagCommand(s, i, i.ApplicationCommandData().Options[0].Options[0]) + case "add": + option := i.ApplicationCommandData().Options[0] + addTag(i.GuildID, strcase.ToSnake(option.Options[0].StringValue()) /*TODO: tag regex*/, option.Options[1].StringValue()) + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Tag added!", + Flags: discordgo.MessageFlagsEphemeral, + }, + }) + case "remove": + AutocompleteTag(s, i) + if i.Type == discordgo.InteractionApplicationCommand { + fmt.Println("Trying to remove " + i.ApplicationCommandData().Options[0].Options[0].StringValue()) // so now it returns the content so wee reeeeaally need to start using UUIDs + removeTag(i.GuildID, i.ApplicationCommandData().Options[0].Options[0].StringValue()) + } + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Tag removed!", + Flags: discordgo.MessageFlagsEphemeral, + }, + }) + } + }, +} var short_get_tag_command Command = Command{ Definition: discordgo.ApplicationCommand{ @@ -76,7 +106,11 @@ var short_get_tag_command Command = Command{ Autocomplete: true, }, }, - }} + }, + Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + GetTagCommand(s, i, i.ApplicationCommandData().Options[0]) + }, +} func GetTagCommand(s *discordgo.Session, i *discordgo.InteractionCreate, option *discordgo.ApplicationCommandInteractionDataOption) { AutocompleteTag(s, i) @@ -117,38 +151,3 @@ func generateDynamicChoices(guildID string) []*discordgo.ApplicationCommandOptio } return choices } - -// Yeeeahh the codebase sucks rn -func (tag_command Command) Interaction(s *discordgo.Session, i *discordgo.InteractionCreate) { - switch i.ApplicationCommandData().Options[0].Name { - case "get": - GetTagCommand(s, i, i.ApplicationCommandData().Options[0].Options[0]) - case "add": - option := i.ApplicationCommandData().Options[0] - addTag(i.GuildID, strcase.ToSnake(option.Options[0].StringValue()) /*TODO: tag regex*/, option.Options[1].StringValue()) - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: "Tag added!", - Flags: discordgo.MessageFlagsEphemeral, - }, - }) - case "remove": - AutocompleteTag(s, i) - if i.Type == discordgo.InteractionApplicationCommand { - fmt.Println("Trying to remove " + i.ApplicationCommandData().Options[0].Options[0].StringValue()) // so now it returns the content so wee reeeeaally need to start using UUIDs - removeTag(i.GuildID, i.ApplicationCommandData().Options[0].Options[0].StringValue()) - } - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: "Tag removed!", - Flags: discordgo.MessageFlagsEphemeral, - }, - }) - } -} - -func (short_get_tag_command Command) tInteraction(s *discordgo.Session, i *discordgo.InteractionCreate) { - GetTagCommand(s, i, i.ApplicationCommandData().Options[0]) -} diff --git a/main.go b/main.go index 5153641..f38c923 100644 --- a/main.go +++ b/main.go @@ -17,10 +17,6 @@ import ( //TODO: make the codebase less garbage -type Command struct { - Definition discordgo.ApplicationCommand -} - var db *sql.DB func main() { @@ -59,46 +55,3 @@ func main() { defer removeCommandFromAllGuilds(discord) discord.Close() } - -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.Interaction(s, i) - case "g": - short_get_tag_command.tInteraction(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) - } - } - } -} diff --git a/register_commands.go b/register_commands.go new file mode 100644 index 0000000..4df1281 --- /dev/null +++ b/register_commands.go @@ -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) + } + } + } +}