From 3b86f604265d9becb53eccef2963a7fdfb7e3b62 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Tue, 19 Mar 2024 00:40:52 +0100 Subject: [PATCH] sticky messages now work without a modal and also with a context menu --- cmd_sticky.go | 117 +++++++++++++++---------------- form_templates/sticky_modal.json | 12 ---- handlers.go | 2 +- manage_data.go | 18 ++--- 4 files changed, 62 insertions(+), 87 deletions(-) delete mode 100644 form_templates/sticky_modal.json diff --git a/cmd_sticky.go b/cmd_sticky.go index 93065ba..f6157d3 100644 --- a/cmd_sticky.go +++ b/cmd_sticky.go @@ -12,46 +12,15 @@ var cmd_sticky Command = Command{ DefaultMemberPermissions: int64Ptr(discordgo.PermissionManageMessages), Options: []*discordgo.ApplicationCommandOption{ { - Type: discordgo.ApplicationCommandOptionSubCommand, - Name: "add", - Description: "Stick messages to the bottom of the current channel", - }, - { - Type: discordgo.ApplicationCommandOptionSubCommand, - Name: "remove", - Description: "Remove the sticky message of the current channel", + Type: discordgo.ApplicationCommandOptionString, + Name: "message", + Description: "The message you want to stick to the bottom of this channel", + Required: false, }, }, }, Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { - switch i.ApplicationCommandData().Options[0].Name { - case "add": - err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseModal, - Data: &discordgo.InteractionResponseData{ - CustomID: "sticky_modal", - Title: "Sticky message", - Components: []discordgo.MessageComponent{ - discordgo.ActionsRow{ - Components: []discordgo.MessageComponent{ - discordgo.TextInput{ - CustomID: "sticky_modal_text", - Label: "Text", - Style: discordgo.TextInputParagraph, - Placeholder: "The message you want to stick to the bottom of this channel", - Required: true, - MaxLength: 2000, - Value: "", - }, - }, - }, - }, - }, - }) - if err != nil { - logrus.Error(err) - } - case "remove": + if len(i.ApplicationCommandData().Options) == 0 { if hasSticky(i.GuildID, i.ChannelID) { err := s.ChannelMessageDelete(i.ChannelID, getStickyMessageID(i.GuildID, i.ChannelID)) if err != nil { @@ -68,32 +37,58 @@ var cmd_sticky Command = Command{ logrus.Error(err) } } - } - }, - ModalIDs: []string{"sticky_modal"}, - ModalSubmit: func(s *discordgo.Session, i *discordgo.InteractionCreate) { - text := i.ModalSubmitData().Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value - message, err := s.ChannelMessageSendEmbed(i.ChannelID, &discordgo.MessageEmbed{ - Type: discordgo.EmbedTypeArticle, - Footer: &discordgo.MessageEmbedFooter{ - Text: "📌 Sticky message", - }, - Color: hexToDecimal(color["primary"]), - Description: text, - }) - if err != nil { - logrus.Error(err) - } - if addSticky(i.GuildID, i.ChannelID, text, message.ID) { - err := respond(i.Interaction, "Sticky message in this channel was updated!", true) - if err != nil { - logrus.Error(err) - } } else { - err := respond(i.Interaction, "Message sticked to the channel!", true) - if err != nil { - logrus.Error(err) - } + inputStickyMessage(i) } }, } + +var context_sticky Command = Command{ + Definition: discordgo.ApplicationCommand{ + Name: "Stick to channel", + Type: discordgo.MessageApplicationCommand, + DefaultMemberPermissions: int64Ptr(discordgo.PermissionManageMessages), + }, + Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + inputStickyMessage(i) + }, +} + +func inputStickyMessage(i *discordgo.InteractionCreate) { + var messageText string + if len(i.ApplicationCommandData().Options) == 0 { + messageText = i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content //TODO add more data then just content + } else { + messageText = i.ApplicationCommandData().Options[0].StringValue() + } + message, err := bot.ChannelMessageSendEmbed(i.ChannelID, &discordgo.MessageEmbed{ + Type: discordgo.EmbedTypeArticle, + Footer: &discordgo.MessageEmbedFooter{ + Text: "📌 Sticky message", + }, + Color: hexToDecimal(color["primary"]), + Description: messageText, + }) + if err != nil { + logrus.Error(err) + } + + if hasSticky(i.GuildID, i.ChannelID) { + err = bot.ChannelMessageDelete(i.ChannelID, getStickyMessageID(i.GuildID, i.ChannelID)) + if err != nil { + logrus.Error(err, getStickyMessageID(i.GuildID, i.ChannelID)) + } + removeSticky(i.GuildID, i.ChannelID) + addSticky(i.GuildID, i.ChannelID, messageText, message.ID) + err = respond(i.Interaction, "Sticky message in this channel was updated!", true) + if err != nil { + logrus.Error(err) + } + } else { + addSticky(i.GuildID, i.ChannelID, messageText, message.ID) + err := respond(i.Interaction, "Message sticked to the channel!", true) + if err != nil { + logrus.Error(err) + } + } +} diff --git a/form_templates/sticky_modal.json b/form_templates/sticky_modal.json deleted file mode 100644 index 039e357..0000000 --- a/form_templates/sticky_modal.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "form_type": "template_general", - "title": "Add a Sticky message", - "form": [ - { - "label": "Content", - "is_paragraph": true, - "required": true, - "max_length": 2000 - } - ] -} diff --git a/handlers.go b/handlers.go index 41f730d..252afc0 100644 --- a/handlers.go +++ b/handlers.go @@ -21,7 +21,7 @@ type Command struct { DynamicModalIDs func() []string } -var commands []Command = []Command{cmd_form, cmd_tag, cmd_tag_short, cmd_dadjoke, cmd_ping, cmd_ask, cmd_sticky, cmd_cat, cmd_autojoinroles, cmd_autopublish} +var commands []Command = []Command{cmd_form, cmd_tag, cmd_tag_short, cmd_dadjoke, cmd_ping, cmd_ask, cmd_sticky, cmd_cat, cmd_autojoinroles, cmd_autopublish, context_sticky} func ready(s *discordgo.Session, event *discordgo.Ready) { logrus.Info("Starting up...") diff --git a/manage_data.go b/manage_data.go index 7e450fa..ae8c3d5 100644 --- a/manage_data.go +++ b/manage_data.go @@ -131,20 +131,12 @@ func getTagContent(guildID string, tagID string) string { return tagContent } -func addSticky(guildID string, channelID string, messageContent string, messageID string) bool { - exists := hasSticky(guildID, channelID) - if exists { - _, err := db.Exec("UPDATE sticky SET message_content = $1 WHERE guild_id = $2 AND channel_id = $3", messageContent, guildID, channelID) - if err != nil { - logrus.Error(err) - } - } else { - _, err := db.Exec("INSERT INTO sticky (guild_id, channel_id, message_id, message_content) VALUES ($1, $2, $3, $4)", guildID, channelID, messageID, messageContent) - if err != nil { - logrus.Error(err) - } +func addSticky(guildID string, channelID string, messageContent string, messageID string) { + _, err := db.Exec("INSERT INTO sticky (guild_id, channel_id, message_id, message_content) VALUES ($1, $2, $3, $4)", guildID, channelID, messageID, messageContent) + if err != nil { + logrus.Error(err) } - return exists + } func hasSticky(guildID string, channelID string) bool {