improved codebase a bit
This commit is contained in:
@@ -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{
|
var short_get_tag_command Command = Command{
|
||||||
Definition: discordgo.ApplicationCommand{
|
Definition: discordgo.ApplicationCommand{
|
||||||
@@ -76,7 +106,11 @@ var short_get_tag_command Command = Command{
|
|||||||
Autocomplete: true,
|
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) {
|
func GetTagCommand(s *discordgo.Session, i *discordgo.InteractionCreate, option *discordgo.ApplicationCommandInteractionDataOption) {
|
||||||
AutocompleteTag(s, i)
|
AutocompleteTag(s, i)
|
||||||
@@ -117,38 +151,3 @@ func generateDynamicChoices(guildID string) []*discordgo.ApplicationCommandOptio
|
|||||||
}
|
}
|
||||||
return choices
|
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])
|
|
||||||
}
|
|
||||||
|
47
main.go
47
main.go
@@ -17,10 +17,6 @@ import (
|
|||||||
|
|
||||||
//TODO: make the codebase less garbage
|
//TODO: make the codebase less garbage
|
||||||
|
|
||||||
type Command struct {
|
|
||||||
Definition discordgo.ApplicationCommand
|
|
||||||
}
|
|
||||||
|
|
||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -59,46 +55,3 @@ func main() {
|
|||||||
defer removeCommandFromAllGuilds(discord)
|
defer removeCommandFromAllGuilds(discord)
|
||||||
discord.Close()
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
55
register_commands.go
Normal file
55
register_commands.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user