sticky messages now work without a modal and also with a context menu

This commit is contained in:
2024-03-19 00:40:52 +01:00
parent d681b167ea
commit 3b86f60426
4 changed files with 62 additions and 87 deletions

View File

@@ -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)
}
}
} else {
inputStickyMessage(i)
}
},
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{
}
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: text,
Description: messageText,
})
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 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)
}
}
},
}

View File

@@ -1,12 +0,0 @@
{
"form_type": "template_general",
"title": "Add a Sticky message",
"form": [
{
"label": "Content",
"is_paragraph": true,
"required": true,
"max_length": 2000
}
]
}

View File

@@ -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...")

View File

@@ -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 {
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 {