From 01ac1ec36386d81161989a8f979189084657d987 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Sun, 7 Apr 2024 18:00:08 +0200 Subject: [PATCH] added sticky command back with disgo --- cmd_sticky.go | 87 ++++++++++++++++++++++++++------------------------- handlers.go | 23 ++++++++------ main.go | 2 +- 3 files changed, 59 insertions(+), 53 deletions(-) diff --git a/cmd_sticky.go b/cmd_sticky.go index 8d9901f..f1aa818 100644 --- a/cmd_sticky.go +++ b/cmd_sticky.go @@ -1,103 +1,106 @@ package main -/* import ( - "github.com/bwmarrin/discordgo" + "github.com/disgoorg/disgo/discord" + "github.com/disgoorg/disgo/events" + "github.com/disgoorg/json" + "github.com/disgoorg/snowflake/v2" "github.com/sirupsen/logrus" ) var cmd_sticky Command = Command{ - Definition: discordgo.ApplicationCommand{ + Definition: discord.SlashCommandCreate{ Name: "sticky", Description: "Stick or unstick messages to the bottom of the current channel", - DefaultMemberPermissions: int64Ptr(discordgo.PermissionManageMessages), - Options: []*discordgo.ApplicationCommandOption{ - { - Type: discordgo.ApplicationCommandOptionString, + DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageMessages), + Options: []discord.ApplicationCommandOption{ + &discord.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) { - if len(i.ApplicationCommandData().Options) == 0 { - if hasSticky(i.GuildID, i.ChannelID) { - err := s.ChannelMessageDelete(i.ChannelID, getStickyMessageID(i.GuildID, i.ChannelID)) + Interact: func(e *events.ApplicationCommandInteractionCreate) { + if len(e.SlashCommandInteractionData().Options) == 0 { + if hasSticky(e.GuildID().String(), e.Channel().ID().String()) { + err := e.Client().Rest().DeleteMessage(e.Channel().ID(), snowflake.MustParse(getStickyMessageID(e.GuildID().String(), e.Channel().ID().String()))) if err != nil { logrus.Error(err) } - removeSticky(i.GuildID, i.ChannelID) - err = respond(i.Interaction, "The sticky message was removed from this channel!", true) + removeSticky(e.GuildID().String(), e.Channel().ID().String()) + err = e.CreateMessage(discord.NewMessageCreateBuilder(). + SetContent("The sticky message was removed from this channel!").SetEphemeral(true). + Build()) if err != nil { logrus.Error(err) } } else { - err := respond(i.Interaction, "This channel has no sticky message!", true) + err := e.CreateMessage(discord.NewMessageCreateBuilder(). + SetContent("This channel has no sticky message!").SetEphemeral(true). + Build()) if err != nil { logrus.Error(err) } } } else { - inputStickyMessage(i) + inputStickyMessage(e) } }, } var context_sticky Command = Command{ - Definition: discordgo.ApplicationCommand{ + Definition: discord.MessageCommandCreate{ Name: "Stick to channel", - Type: discordgo.MessageApplicationCommand, - DefaultMemberPermissions: int64Ptr(discordgo.PermissionManageMessages), + DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageMessages), }, - Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { - inputStickyMessage(i) + Interact: func(e *events.ApplicationCommandInteractionCreate) { + inputStickyMessage(e) }, } -func inputStickyMessage(i *discordgo.InteractionCreate) { +func inputStickyMessage(e *events.ApplicationCommandInteractionCreate) { 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 + if e.ApplicationCommandInteraction.Data.Type() == discord.ApplicationCommandTypeMessage { + messageText = e.MessageCommandInteractionData().TargetMessage().Content //TODO add more data then just content } else { - messageText = i.ApplicationCommandData().Options[0].StringValue() + messageText = e.SlashCommandInteractionData().String("message") } if messageText == "" { - err := respond(i.Interaction, "Can't add empty sticky messages!", true) + err := e.CreateMessage(discord.NewMessageCreateBuilder(). + SetContent("Can't add empty sticky messages!").SetEphemeral(true). + Build()) if err != nil { logrus.Error(err) } } else { - message, err := bot.ChannelMessageSendEmbed(i.ChannelID, &discordgo.MessageEmbed{ - Type: discordgo.EmbedTypeArticle, - Footer: &discordgo.MessageEmbedFooter{ - Text: "📌 Sticky message", - }, - Color: hexToDecimal(color["primary"]), - Description: messageText, - }) + message, err := e.Client().Rest().CreateMessage(e.Channel().ID(), discord.MessageCreate{Embeds: []discord.Embed{ + {Description: messageText, Footer: &discord.EmbedFooter{Text: "📌 Sticky message"}, Color: hexToDecimal(color["primary"])}}}) if err != nil { logrus.Error(err) } - if hasSticky(i.GuildID, i.ChannelID) { - err = bot.ChannelMessageDelete(i.ChannelID, getStickyMessageID(i.GuildID, i.ChannelID)) + if hasSticky(e.GuildID().String(), e.Channel().ID().String()) { + err = e.Client().Rest().DeleteMessage(e.Channel().ID(), snowflake.MustParse(getStickyMessageID(e.GuildID().String(), e.Channel().ID().String()))) if err != nil { - logrus.Error(err, getStickyMessageID(i.GuildID, i.ChannelID)) + logrus.Error(err, getStickyMessageID(e.GuildID().String(), e.Channel().ID().String())) } - 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) + removeSticky(e.GuildID().String(), e.Channel().ID().String()) + addSticky(e.GuildID().String(), e.Channel().ID().String(), messageText, message.ID.String()) + err = e.CreateMessage(discord.NewMessageCreateBuilder(). + SetContent("Sticky message in this channel was updated!").SetEphemeral(true). + Build()) 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) + addSticky(e.GuildID().String(), e.Channel().ID().String(), messageText, message.ID.String()) + err := e.CreateMessage(discord.NewMessageCreateBuilder(). + SetContent("Message sticked to the channel!").SetEphemeral(true). + Build()) if err != nil { logrus.Error(err) } } } } -*/ diff --git a/handlers.go b/handlers.go index b481a0a..066213d 100644 --- a/handlers.go +++ b/handlers.go @@ -26,7 +26,7 @@ type Command struct { AllowDM bool } -var commands []Command = []Command{cmd_tag, cmd_tag_short, context_tag /*, cmd_form, cmd_ticket_form, cmd_dadjoke, cmd_ping, cmd_ask, cmd_sticky, cmd_cat, cmd_autojoinroles, cmd_autopublish, context_sticky, cmd_userinfo*/} +var commands []Command = []Command{cmd_tag, cmd_tag_short, context_tag, cmd_sticky, context_sticky /*, cmd_form, cmd_ticket_form, cmd_dadjoke, cmd_ping, cmd_ask, cmd_cat, cmd_autojoinroles, cmd_autopublish, cmd_userinfo*/} func ready(e *events.Ready) { logrus.Info("Starting up...") @@ -196,15 +196,18 @@ func messageCreate(e *events.MessageCreate) { updateStickyMessageID(e.Message.GuildID.String(), e.Message.ChannelID.String(), stickyMessage.ID.String()) } } - channel, _ := e.Channel() - if channel.Type() == discord.ChannelTypeGuildNews { - if isAutopublishEnabled(e.GuildID.String(), e.ChannelID.String()) { - _, err := e.Client().Rest().CrosspostMessage(e.ChannelID, e.MessageID) - if err != nil { - logrus.Error(err) - } - } - } + /* + channel, _ := e.Channel() + + if channel.Type() == discord.ChannelTypeGuildNews { + if isAutopublishEnabled(e.GuildID.String(), e.ChannelID.String()) { + _, err := e.Client().Rest().CrosspostMessage(e.ChannelID, e.MessageID) + if err != nil { + logrus.Error(err) + } + } + } + */ } func messageDelete(e *events.MessageDelete) { //TODO: also clear on bot start when message doesn't exist diff --git a/main.go b/main.go index 205a56b..4f2430f 100644 --- a/main.go +++ b/main.go @@ -48,8 +48,8 @@ func main() { bot.WithEventListenerFunc(autocompleteInteractionCreate), bot.WithEventListenerFunc(componentInteractionCreate), bot.WithEventListenerFunc(modalSubmitInteractionCreate), + bot.WithEventListenerFunc(messageCreate), /* - bot.WithEventListenerFunc(messageCreate), bot.WithEventListenerFunc(messageDelete), bot.WithEventListenerFunc(guildMemberJoin), */