From 68a99fbb1005a3404023e93d6e10b54817cca449 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Thu, 4 Apr 2024 20:14:47 +0200 Subject: [PATCH] made getting tags working --- cmd_tag.go | 10 ++++++- handlers.go | 79 +++++++++++++++++++++++++++++++---------------------- main.go | 23 ++++++++-------- tool.go | 17 ++++++------ 4 files changed, 75 insertions(+), 54 deletions(-) diff --git a/cmd_tag.go b/cmd_tag.go index ef6dc0e..85c50c4 100644 --- a/cmd_tag.go +++ b/cmd_tag.go @@ -98,7 +98,7 @@ var cmd_tag_short Command = Command{ } var context_tag Command = Command{ - Definition: discord.SlashCommandCreate{ + Definition: discord.MessageCommandCreate{ Name: "Save as tag", DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageGuild), }, @@ -130,6 +130,14 @@ func AddTagCommand(e *events.ApplicationCommandInteractionCreate, prevalue strin MaxLength: 20, Value: "", }, + discord.TextInputComponent{ + CustomID: "tag_add_modal_content", + Label: "Content", + Style: discord.TextInputStyleParagraph, + Required: true, + MaxLength: 2000, + Value: prevalue, + }, }, }, }) diff --git a/handlers.go b/handlers.go index e0c2823..dce8aa8 100644 --- a/handlers.go +++ b/handlers.go @@ -1,9 +1,12 @@ package main import ( + "fmt" + "os" "slices" "strings" + "github.com/disgoorg/disgo/bot" "github.com/disgoorg/disgo/discord" "github.com/disgoorg/disgo/events" "github.com/disgoorg/snowflake/v2" @@ -11,7 +14,7 @@ import ( ) type Command struct { - Definition discord.SlashCommandCreate + Definition discord.ApplicationCommandCreate Interact func(e *events.ApplicationCommandInteractionCreate) Autocomplete func(e *events.AutocompleteInteractionCreate) ComponentInteract func(e *events.ComponentInteractionCreate) @@ -27,49 +30,57 @@ var commands []Command = []Command{cmd_tag, cmd_tag_short, context_tag /*, cmd_f func ready(e *events.Ready) { logrus.Info("Starting up...") - //findAndDeleteUnusedMessages() - removeOldCommandFromAllGuilds() - /* - var existingCommandNames []string - existingCommands, err := client.Rest().GetGlobalCommands(client.ApplicationID(), false) - if err != nil { - logrus.Errorf("error fetching existing global commands: %v", err) - } else { - for _, existingCommand := range existingCommands { - existingCommandNames = append(existingCommandNames, existingCommand.Name()) - } + findAndDeleteUnusedMessages(e.Client()) + removeOldCommandFromAllGuilds(e.Client()) + var existingCommandNames []string + existingCommands, err := e.Client().Rest().GetGlobalCommands(e.Client().ApplicationID(), false) + if err != nil { + logrus.Errorf("error fetching existing global commands: %v", err) + } else { + for _, existingCommand := range existingCommands { + existingCommandNames = append(existingCommandNames, existingCommand.Name()) } - for _, command := range commands { - if !slices.Contains(existingCommandNames, command.Definition.Name) || slices.Contains(os.Args, "--update="+command.Definition.Name) || slices.Contains(os.Args, "--update=all") || slices.Contains(os.Args, "--clean") { - cmd, err := client.Rest().CreateGlobalCommand(client.ApplicationID(), command.Definition) - if err != nil { - logrus.Errorf("error creating global command '%s': %v", cmd.Name(), err) - } else { - logrus.Infof("Added global command '%s'", cmd.Name()) - } - } + } + globalCommands := []discord.ApplicationCommandCreate{} + for _, command := range commands { + if !slices.Contains(existingCommandNames, command.Definition.CommandName()) || slices.Contains(os.Args, "--update="+command.Definition.CommandName()) || slices.Contains(os.Args, "--update=all") || slices.Contains(os.Args, "--clean") { + globalCommands = append(globalCommands, command.Definition) + logrus.Infof("Appending command \"%s\"", command.Definition.CommandName()) } - logrus.Info("Successfully started the Bot!") - */ + } + logrus.Infof("Attempting to add global commands %s", fmt.Sprint(globalCommands)) + _, err = e.Client().Rest().SetGlobalCommands(e.Client().ApplicationID(), globalCommands) + if err != nil { + logrus.Errorf("error creating global commands '%s'", err) + } else { + logrus.Infof("Added global commands sucessfully!") + } + logrus.Info("Successfully started the Bot!") } func applicationCommandInteractionCreate(e *events.ApplicationCommandInteractionCreate) { + app, err := e.Client().Rest().GetCurrentApplication() + if err != nil { + logrus.Error(err) + } for _, command := range commands { - if command.Interact != nil && e.SlashCommandInteractionData().CommandName() == command.Definition.Name { - if !command.AllowDM && e.SlashCommandInteractionData().GuildID().String() == "" { + if command.Interact != nil && e.SlashCommandInteractionData().CommandName() == command.Definition.CommandName() { + if !command.AllowDM && app.GuildID.String() == "" { e.CreateMessage(discord.NewMessageCreateBuilder(). SetContent("This command is not available in DMs.").SetEphemeral(true). Build()) } else { command.Interact(e) } + } + } } func autocompleteInteractionCreate(e *events.AutocompleteInteractionCreate) { for _, command := range commands { - if command.Autocomplete != nil && e.Data.CommandName == command.Definition.Name { + if command.Autocomplete != nil && e.Data.CommandName == command.Definition.CommandName() { if !command.AllowDM && e.GuildID().String() == "" { err := e.AutocompleteResult(nil) if err != nil { @@ -137,22 +148,24 @@ func modalSubmitInteractionCreate(e *events.ModalSubmitInteractionCreate) { } } -func removeOldCommandFromAllGuilds() { - logrus.Debug(app.Bot.ID.String()) - globalCommands, err := client.Rest().GetGlobalCommands(app.Bot.ID, false) - logrus.Debug("HERE") //doesnt get called +func removeOldCommandFromAllGuilds(c bot.Client) { + app, err := c.Rest().GetCurrentApplication() + if err != nil { + logrus.Error(err) + } + globalCommands, err := c.Rest().GetGlobalCommands(app.Bot.ID, false) if err != nil { logrus.Errorf("error fetching existing global commands: %v", err) return } var commandNames []string for _, command := range commands { - commandNames = append(commandNames, command.Definition.Name) + commandNames = append(commandNames, command.Definition.CommandName()) } for _, existingCommand := range globalCommands { - if slices.Contains(commandNames, existingCommand.Name()) { + if !slices.Contains(commandNames, existingCommand.Name()) { logrus.Infof("Deleting command '%s'", existingCommand.Name()) - err := client.Rest().DeleteGlobalCommand(client.ApplicationID(), existingCommand.ID()) + err := c.Rest().DeleteGlobalCommand(c.ApplicationID(), existingCommand.ID()) if err != nil { logrus.Errorf("error deleting command %s: %v", existingCommand.Name(), err) } diff --git a/main.go b/main.go index 0dd620b..205a56b 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,6 @@ import ( "github.com/disgoorg/disgo" "github.com/disgoorg/disgo/bot" - "github.com/disgoorg/disgo/discord" "github.com/disgoorg/disgo/gateway" "github.com/joho/godotenv" "github.com/sirupsen/logrus" @@ -23,9 +22,7 @@ import ( ) var ( - client bot.Client - app *discord.Application - db *sql.DB + db *sql.DB ) func main() { @@ -47,13 +44,15 @@ func main() { ), ), bot.WithEventListenerFunc(ready), - //bot.WithEventListenerFunc(applicationCommandInteractionCreate), - //bot.WithEventListenerFunc(autocompleteInteractionCreate), - //bot.WithEventListenerFunc(componentInteractionCreate), - //bot.WithEventListenerFunc(modalSubmitInteractionCreate), - //bot.WithEventListenerFunc(messageCreate), - //bot.WithEventListenerFunc(messageDelete), - //bot.WithEventListenerFunc(guildMemberJoin), + bot.WithEventListenerFunc(applicationCommandInteractionCreate), + bot.WithEventListenerFunc(autocompleteInteractionCreate), + bot.WithEventListenerFunc(componentInteractionCreate), + bot.WithEventListenerFunc(modalSubmitInteractionCreate), + /* + bot.WithEventListenerFunc(messageCreate), + bot.WithEventListenerFunc(messageDelete), + bot.WithEventListenerFunc(guildMemberJoin), + */ ) if err != nil { logrus.Fatal("error creating Discord session,", err) @@ -66,7 +65,7 @@ func main() { logrus.Error("error opening connection,", err) return } - app, err = client.Rest().GetCurrentApplication() + app, err := client.Rest().GetCurrentApplication() if err != nil { logrus.Error(err) } diff --git a/tool.go b/tool.go index 13ec791..bf9f9d8 100644 --- a/tool.go +++ b/tool.go @@ -9,6 +9,7 @@ import ( "strconv" "strings" + "github.com/disgoorg/disgo/bot" "github.com/disgoorg/disgo/discord" "github.com/disgoorg/snowflake/v2" "github.com/sirupsen/logrus" @@ -101,12 +102,12 @@ func getModalByFormID(formID string) ModalJson { return modal } -func getHighestRole(guildID string) (*discord.Role, error) { - botmember, err := client.Rest().GetMember(snowflake.MustParse(guildID), client.ApplicationID()) +func getHighestRole(guildID string, c bot.Client) (*discord.Role, error) { + botmember, err := c.Rest().GetMember(snowflake.MustParse(guildID), c.ApplicationID()) if err != nil { return nil, err } - roles, err := client.Rest().GetRoles(snowflake.MustParse(guildID)) + roles, err := c.Rest().GetRoles(snowflake.MustParse(guildID)) if err != nil { return nil, err } @@ -165,21 +166,21 @@ func simpleGetFromAPI(key string, url string) interface{} { return result[key] } -func findAndDeleteUnusedMessages() { +func findAndDeleteUnusedMessages(c bot.Client) { for _, message := range getAllSavedMessages() { - _, err := client.Rest().GetMessage(snowflake.MustParse(message.ChannelID), snowflake.MustParse(message.ID)) + _, err := c.Rest().GetMessage(snowflake.MustParse(message.ChannelID), snowflake.MustParse(message.ID)) if err != nil { tryDeleteUnusedMessage(message.ID) } } } -func isIDRole(guildID snowflake.ID, id snowflake.ID) bool { - _, err1 := client.Rest().GetMember(guildID, id) +func isIDRole(c bot.Client, guildID snowflake.ID, id snowflake.ID) bool { + _, err1 := c.Rest().GetMember(guildID, id) if err1 == nil { return false } - roles, err2 := client.Rest().GetRoles(guildID) + roles, err2 := c.Rest().GetRoles(guildID) if err2 == nil { for _, role := range roles { if role.ID == id {