From bb9c61684447433e1f167d0a3f8aeebea71fc762 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Wed, 10 Apr 2024 18:15:08 +0200 Subject: [PATCH] added partial userinstall support --- cmd_ask.go | 10 ++++- cmd_autojoinroles.go | 5 +++ cmd_autopublish.go | 5 +++ cmd_cat.go | 10 ++++- cmd_dadjoke.go | 10 ++++- cmd_form.go | 10 ++++- cmd_info.go | 12 +++++- cmd_ping.go | 10 ++++- cmd_sticky.go | 10 +++++ cmd_tag.go | 16 ++++++++ handlers.go | 92 +++++++++++++++----------------------------- 11 files changed, 120 insertions(+), 70 deletions(-) diff --git a/cmd_ask.go b/cmd_ask.go index cfef599..902393c 100644 --- a/cmd_ask.go +++ b/cmd_ask.go @@ -10,6 +10,15 @@ var cmd_ask = Command{ Definition: discord.SlashCommandCreate{ Name: "ask", Description: "Ask anything and get a gif as response!", + Contexts: []discord.InteractionContextType{ + discord.InteractionContextTypeGuild, + discord.InteractionContextTypePrivateChannel, + discord.InteractionContextTypeBotDM, + }, + IntegrationTypes: []discord.ApplicationIntegrationType{ + discord.ApplicationIntegrationTypeGuildInstall, + discord.ApplicationIntegrationTypeUserInstall, + }, Options: []discord.ApplicationCommandOption{ &discord.ApplicationCommandOptionString{ Name: "question", @@ -26,5 +35,4 @@ var cmd_ask = Command{ logrus.Error(err) } }, - AllowDM: true, } diff --git a/cmd_autojoinroles.go b/cmd_autojoinroles.go index 1a69a6d..fdfcd99 100644 --- a/cmd_autojoinroles.go +++ b/cmd_autojoinroles.go @@ -10,6 +10,11 @@ var cmd_autojoinroles Command = Command{ Definition: discord.SlashCommandCreate{ Name: "autojoinroles", Description: "Give users a role when they join", + Contexts: []discord.InteractionContextType{ + discord.InteractionContextTypeGuild, + discord.InteractionContextTypePrivateChannel}, + IntegrationTypes: []discord.ApplicationIntegrationType{ + discord.ApplicationIntegrationTypeGuildInstall}, Options: []discord.ApplicationCommandOption{ &discord.ApplicationCommandOptionSubCommand{ Name: "bot", diff --git a/cmd_autopublish.go b/cmd_autopublish.go index 43ae0db..35ee438 100644 --- a/cmd_autopublish.go +++ b/cmd_autopublish.go @@ -10,6 +10,11 @@ var cmd_autopublish Command = Command{ Definition: discord.SlashCommandCreate{ Name: "autopublish", Description: "Toggle automatically publishing every post in a announcement channel", + Contexts: []discord.InteractionContextType{ + discord.InteractionContextTypeGuild, + discord.InteractionContextTypePrivateChannel}, + IntegrationTypes: []discord.ApplicationIntegrationType{ + discord.ApplicationIntegrationTypeGuildInstall}, }, Interact: func(e *events.ApplicationCommandInteractionCreate) { channel := e.Channel() diff --git a/cmd_cat.go b/cmd_cat.go index a125f57..3b23bce 100644 --- a/cmd_cat.go +++ b/cmd_cat.go @@ -14,6 +14,15 @@ var cmd_cat = Command{ Definition: discord.SlashCommandCreate{ Name: "cat", Description: "Random cat pictures", + Contexts: []discord.InteractionContextType{ + discord.InteractionContextTypeGuild, + discord.InteractionContextTypePrivateChannel, + discord.InteractionContextTypeBotDM, + }, + IntegrationTypes: []discord.ApplicationIntegrationType{ + discord.ApplicationIntegrationTypeGuildInstall, + discord.ApplicationIntegrationTypeUserInstall, + }, }, Interact: func(e *events.ApplicationCommandInteractionCreate) { cat, err := GetCatImageURL() @@ -26,7 +35,6 @@ var cmd_cat = Command{ } } }, - AllowDM: true, } type CatImage struct { diff --git a/cmd_dadjoke.go b/cmd_dadjoke.go index 4dd67de..c0b8dd8 100644 --- a/cmd_dadjoke.go +++ b/cmd_dadjoke.go @@ -10,6 +10,15 @@ var cmd_dadjoke = Command{ Definition: discord.SlashCommandCreate{ Name: "dadjoke", Description: "Gives you a random joke that is as bad as your dad would tell them", + Contexts: []discord.InteractionContextType{ + discord.InteractionContextTypeGuild, + discord.InteractionContextTypePrivateChannel, + discord.InteractionContextTypeBotDM, + }, + IntegrationTypes: []discord.ApplicationIntegrationType{ + discord.ApplicationIntegrationTypeGuildInstall, + discord.ApplicationIntegrationTypeUserInstall, + }, }, Interact: func(e *events.ApplicationCommandInteractionCreate) { joke := simpleGetFromAPI("joke", "https://icanhazdadjoke.com/").(string) @@ -20,5 +29,4 @@ var cmd_dadjoke = Command{ logrus.Error(err) } }, - AllowDM: true, } diff --git a/cmd_form.go b/cmd_form.go index d66637c..186f979 100644 --- a/cmd_form.go +++ b/cmd_form.go @@ -19,6 +19,11 @@ var cmd_form Command = Command{ Name: "form", DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageChannels), Description: "Create custom forms right inside Discord", + Contexts: []discord.InteractionContextType{ + discord.InteractionContextTypeGuild, + discord.InteractionContextTypePrivateChannel}, + IntegrationTypes: []discord.ApplicationIntegrationType{ + discord.ApplicationIntegrationTypeGuildInstall}, Options: []discord.ApplicationCommandOption{ &discord.ApplicationCommandOptionSubCommand{ Name: "help", @@ -250,7 +255,6 @@ var cmd_form Command = Command{ Build()) } } else { - logrus.Debug(result.AcceptChannelID) if result.AcceptChannelID == "" { _, err := e.Client().Rest().CreateMessage(snowflake.MustParse(result.ResultChannelID), discord.NewMessageCreateBuilder(). SetEmbeds(discord.NewEmbedBuilder(). @@ -270,7 +274,6 @@ var cmd_form Command = Command{ } } } else { - logrus.Debug("HEERE") var buttons []discord.InteractiveComponent if result.CommentCategoryID != "" { buttons = []discord.InteractiveComponent{discord. @@ -335,6 +338,9 @@ var cmd_ticket_form Command = Command{ Name: "ticket", DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageChannels), Description: "A quick command to create Ticketpanels. (/form for more)", + Contexts: []discord.InteractionContextType{ + discord.InteractionContextTypeGuild, + discord.InteractionContextTypePrivateChannel}, Options: []discord.ApplicationCommandOption{ &discord.ApplicationCommandOptionString{ Name: "title", diff --git a/cmd_info.go b/cmd_info.go index 0c9b2d5..2c81bb4 100644 --- a/cmd_info.go +++ b/cmd_info.go @@ -42,6 +42,15 @@ var cmd_userinfo Command = Command{ Definition: discord.SlashCommandCreate{ Name: "info", Description: "Gives you information about a user or this bot.", + Contexts: []discord.InteractionContextType{ + discord.InteractionContextTypeGuild, + discord.InteractionContextTypePrivateChannel, + discord.InteractionContextTypeBotDM, + }, + IntegrationTypes: []discord.ApplicationIntegrationType{ + discord.ApplicationIntegrationTypeGuildInstall, + discord.ApplicationIntegrationTypeUserInstall, + }, Options: []discord.ApplicationCommandOption{ &discord.ApplicationCommandOptionSubCommand{ Name: "user", @@ -92,7 +101,7 @@ var cmd_userinfo Command = Command{ embedBuilder.AddField("Discriminator", user.Discriminator, false) } if user.AccentColor != nil { - embedBuilder.AddField("Accent color", strconv.Itoa(*user.AccentColor), true) + embedBuilder.AddField("Accent color", "#"+strconv.FormatInt(int64(*user.AccentColor), 16), true) } if user.AvatarDecorationURL() != nil { value := fmt.Sprintf("[PNG (animated)](%s)\n[PNG](%s)", *user.AvatarDecorationURL(), *user.AvatarDecorationURL()+"?passthrough=false") @@ -119,7 +128,6 @@ var cmd_userinfo Command = Command{ } }, - AllowDM: true, } func checkDefaultPb(user discord.User) string { diff --git a/cmd_ping.go b/cmd_ping.go index 233adf8..9c39c32 100644 --- a/cmd_ping.go +++ b/cmd_ping.go @@ -14,6 +14,15 @@ var cmd_ping Command = Command{ Definition: discord.SlashCommandCreate{ Name: "ping", Description: "Returns the ping of the bot", + Contexts: []discord.InteractionContextType{ + discord.InteractionContextTypeGuild, + discord.InteractionContextTypePrivateChannel, + discord.InteractionContextTypeBotDM, + }, + IntegrationTypes: []discord.ApplicationIntegrationType{ + discord.ApplicationIntegrationTypeGuildInstall, + discord.ApplicationIntegrationTypeUserInstall, + }, }, Interact: func(e *events.ApplicationCommandInteractionCreate) { start := time.Now() @@ -51,5 +60,4 @@ var cmd_ping Command = Command{ logrus.Error(err) } }, - AllowDM: true, } diff --git a/cmd_sticky.go b/cmd_sticky.go index f1aa818..a9e8728 100644 --- a/cmd_sticky.go +++ b/cmd_sticky.go @@ -13,6 +13,11 @@ var cmd_sticky Command = Command{ Name: "sticky", Description: "Stick or unstick messages to the bottom of the current channel", DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageMessages), + Contexts: []discord.InteractionContextType{ + discord.InteractionContextTypeGuild, + discord.InteractionContextTypePrivateChannel}, + IntegrationTypes: []discord.ApplicationIntegrationType{ + discord.ApplicationIntegrationTypeGuildInstall}, Options: []discord.ApplicationCommandOption{ &discord.ApplicationCommandOptionString{ Name: "message", @@ -53,6 +58,11 @@ var context_sticky Command = Command{ Definition: discord.MessageCommandCreate{ Name: "Stick to channel", DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageMessages), + Contexts: []discord.InteractionContextType{ + discord.InteractionContextTypeGuild, + discord.InteractionContextTypePrivateChannel}, + IntegrationTypes: []discord.ApplicationIntegrationType{ + discord.ApplicationIntegrationTypeGuildInstall}, }, Interact: func(e *events.ApplicationCommandInteractionCreate) { inputStickyMessage(e) diff --git a/cmd_tag.go b/cmd_tag.go index 634926f..c33c598 100644 --- a/cmd_tag.go +++ b/cmd_tag.go @@ -7,11 +7,17 @@ import ( "github.com/sirupsen/logrus" ) +// TODO: make user installable tag command using userIDs instead of guildIDs var cmd_tag Command = Command{ Definition: discord.SlashCommandCreate{ Name: "tag", DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageGuild), Description: "A command to show and edit saved presaved messages.", + Contexts: []discord.InteractionContextType{ + discord.InteractionContextTypeGuild, + discord.InteractionContextTypePrivateChannel}, + IntegrationTypes: []discord.ApplicationIntegrationType{ + discord.ApplicationIntegrationTypeGuildInstall}, Options: []discord.ApplicationCommandOption{ discord.ApplicationCommandOptionSubCommand{ Name: "get", @@ -80,6 +86,11 @@ var cmd_tag_short Command = Command{ Definition: discord.SlashCommandCreate{ Name: "g", Description: "A short command to get presaved messages.", + Contexts: []discord.InteractionContextType{ + discord.InteractionContextTypeGuild, + discord.InteractionContextTypePrivateChannel}, + IntegrationTypes: []discord.ApplicationIntegrationType{ + discord.ApplicationIntegrationTypeGuildInstall}, Options: []discord.ApplicationCommandOption{ discord.ApplicationCommandOptionString{ Name: "tag", @@ -101,6 +112,11 @@ var context_tag Command = Command{ Definition: discord.MessageCommandCreate{ Name: "Save as tag", DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageGuild), + Contexts: []discord.InteractionContextType{ + discord.InteractionContextTypeGuild, + discord.InteractionContextTypePrivateChannel}, + IntegrationTypes: []discord.ApplicationIntegrationType{ + discord.ApplicationIntegrationTypeGuildInstall}, }, Interact: func(e *events.ApplicationCommandInteractionCreate) { AddTagCommand(e) diff --git a/handlers.go b/handlers.go index ab31cb5..9071f28 100644 --- a/handlers.go +++ b/handlers.go @@ -23,7 +23,6 @@ type Command struct { ModalIDs []string DynamicModalIDs func() []string DynamicComponentIDs func() []string - AllowDM bool } var commands []Command = []Command{cmd_tag, cmd_tag_short, context_tag, cmd_sticky, context_sticky, cmd_ping, cmd_userinfo, cmd_form, cmd_ask, cmd_cat, cmd_dadjoke, cmd_ticket_form, cmd_autopublish, cmd_autojoinroles} @@ -63,56 +62,33 @@ func ready(e *events.Ready) { func applicationCommandInteractionCreate(e *events.ApplicationCommandInteractionCreate) { for _, command := range commands { if command.Interact != nil && e.Data.CommandName() == command.Definition.CommandName() { - if !command.AllowDM && e.ApplicationCommandInteraction.GuildID().String() == "" { - err := e.CreateMessage(discord.NewMessageCreateBuilder(). - SetContent("This command is not available in DMs.").SetEphemeral(true). - Build()) - if err != nil { - logrus.Error(err) - } - } else { - command.Interact(e) - } - + command.Interact(e) } - } + } func autocompleteInteractionCreate(e *events.AutocompleteInteractionCreate) { for _, command := range commands { if command.Autocomplete != nil && e.Data.CommandName == command.Definition.CommandName() { - if !command.AllowDM && e.AutocompleteInteraction.GuildID().String() == "" { - err := e.AutocompleteResult(nil) - if err != nil { - logrus.Error(err) - } - } else { - command.Autocomplete(e) - } + command.Autocomplete(e) } } } func componentInteractionCreate(e *events.ComponentInteractionCreate) { for _, command := range commands { - if !command.AllowDM && e.ComponentInteraction.GuildID().String() == "" { - e.CreateMessage(discord.NewMessageCreateBuilder(). - SetContent("This component is not available in DMs.").SetEphemeral(true). - Build()) - } else { - if command.ComponentInteract != nil { - if slices.Contains(command.ComponentIDs, e.Data.CustomID()) || slices.ContainsFunc(command.DynamicComponentIDs(), func(id string) bool { - var customID string - if strings.ContainsAny(e.Data.CustomID(), ";") { - customID = strings.TrimSuffix(e.Data.CustomID(), ";"+strings.Split(e.Data.CustomID(), ";")[1]) - } else { - customID = e.Data.CustomID() - } - return id == customID - }) { - command.ComponentInteract(e) + if command.ComponentInteract != nil { + if slices.Contains(command.ComponentIDs, e.Data.CustomID()) || slices.ContainsFunc(command.DynamicComponentIDs(), func(id string) bool { + var customID string + if strings.ContainsAny(e.Data.CustomID(), ";") { + customID = strings.TrimSuffix(e.Data.CustomID(), ";"+strings.Split(e.Data.CustomID(), ";")[1]) + } else { + customID = e.Data.CustomID() } + return id == customID + }) { + command.ComponentInteract(e) } } } @@ -120,31 +96,25 @@ func componentInteractionCreate(e *events.ComponentInteractionCreate) { func modalSubmitInteractionCreate(e *events.ModalSubmitInteractionCreate) { for _, command := range commands { - if !command.AllowDM && e.ModalSubmitInteraction.GuildID().String() == "" { - e.CreateMessage(discord.NewMessageCreateBuilder(). - SetContent("This modal is not available in DMs.").SetEphemeral(true). - Build()) - } else { - if command.ModalSubmit != nil { - var hasID bool = false - var modalIDs []string - if command.ModalIDs != nil { - modalIDs = command.ModalIDs - } - if command.DynamicModalIDs != nil { - modalIDs = append(command.ModalIDs, command.DynamicModalIDs()...) - } - for _, modalID := range modalIDs { - if strings.HasPrefix(e.Data.CustomID, modalID) { - hasID = true - break - } - } - if hasID { - command.ModalSubmit(e) - return // I have no idea why it crashes without that return + if command.ModalSubmit != nil { + var hasID bool = false + var modalIDs []string + if command.ModalIDs != nil { + modalIDs = command.ModalIDs + } + if command.DynamicModalIDs != nil { + modalIDs = append(command.ModalIDs, command.DynamicModalIDs()...) + } + for _, modalID := range modalIDs { + if strings.HasPrefix(e.Data.CustomID, modalID) { + hasID = true + break } } + if hasID { + command.ModalSubmit(e) + return // I have no idea why it crashes without that return + } } } } @@ -201,7 +171,6 @@ func messageCreate(e *events.MessageCreate) { logrus.Error(err) } if channel != nil && channel.Type() == discord.ChannelTypeGuildNews { - logrus.Debug("HERE") if isAutopublishEnabled(e.GuildID.String(), e.ChannelID.String()) { _, err := e.Client().Rest().CrosspostMessage(e.ChannelID, e.MessageID) if err != nil { @@ -217,7 +186,6 @@ func messageDelete(e *events.MessageDelete) { //TODO: also clear on bot start wh } func guildMemberJoin(e *events.GuildMemberJoin) { - logrus.Debug("TESSST") role := getAutoJoinRole(e.GuildID.String(), e.Member.User.Bot) if role != "" { err := e.Client().Rest().AddMemberRole(e.GuildID, e.Member.User.ID, snowflake.MustParse(role))