From 304f93e28d06adffe43ebcde7f465a0e2eb225ac Mon Sep 17 00:00:00 2001 From: vaporvee Date: Wed, 21 Feb 2024 16:10:52 +0100 Subject: [PATCH] started adding sticky messages --- cmd_sticky.go | 64 ++++++++++++++++++++++++++++++++++++++++++++ handle_messages.go | 13 +++++++++ main.go | 3 ++- manage_data.go | 41 ++++++++++++++++++++++++++-- register_commands.go | 4 +-- 5 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 cmd_sticky.go create mode 100644 handle_messages.go diff --git a/cmd_sticky.go b/cmd_sticky.go new file mode 100644 index 0000000..383be9d --- /dev/null +++ b/cmd_sticky.go @@ -0,0 +1,64 @@ +package main + +import ( + "log" + + "github.com/bwmarrin/discordgo" +) + +var sticky_command Command = Command{ + Definition: discordgo.ApplicationCommand{ + Name: "sticky", + Description: "Stick messages to the bottom of the current channel", + DefaultMemberPermissions: int64Ptr(discordgo.PermissionManageMessages), + }, + Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + 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: "", + }, + }, + }, + }, + }, + }) + }, + ModalID: "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.ChannelMessageSend(i.ChannelID, text) + if err != nil { + log.Println(err) + } + if addSticky(i.GuildID, i.ChannelID, text, message.ID) { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Sticky message in this channel was updated!", + Flags: discordgo.MessageFlagsEphemeral, + }, + }) + } else { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Message sticked to the channel!", + Flags: discordgo.MessageFlagsEphemeral, + }, + }) + } + }, +} diff --git a/handle_messages.go b/handle_messages.go new file mode 100644 index 0000000..d961ee6 --- /dev/null +++ b/handle_messages.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + + "github.com/bwmarrin/discordgo" +) + +func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { + if m.Author.ID != s.State.User.ID { + fmt.Print(m.Content) + } +} diff --git a/main.go b/main.go index 48d36eb..c1ae5fa 100644 --- a/main.go +++ b/main.go @@ -38,10 +38,11 @@ func main() { } else { fmt.Println("Discord session created") } - discord.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuilds + discord.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuilds | discordgo.IntentMessageContent defer removeCommandFromAllGuilds(discord) discord.AddHandler(ready) discord.AddHandler(interactionCreate) + discord.AddHandler(messageCreate) err = discord.Open() if err != nil { fmt.Println("error opening connection,", err) diff --git a/manage_data.go b/manage_data.go index 815b8ba..6fb90bd 100644 --- a/manage_data.go +++ b/manage_data.go @@ -12,8 +12,16 @@ func initTables() { tag_name TEXT NOT NULL, tag_content TEXT NOT NULL, guild_id TEXT NOT NULL, - PRIMARY KEY (tag_id,guild_id) - );` + PRIMARY KEY (tag_id, guild_id) + ); + CREATE TABLE IF NOT EXISTS sticky ( + message_id TEXT NOT NULL, + channel_id TEXT NOT NULL, + message_content TEXT NOT NULL, + guild_id TEXT NOT NULL, + PRIMARY KEY (channel_id, guild_id) + ); + ` _, err := db.Exec(createTableQuery) if err != nil { @@ -88,3 +96,32 @@ func getTagContent(guildID string, tagID string) string { db.QueryRow("SELECT tag_content FROM tags WHERE guild_id = $1 AND tag_id = $2", guildID, tagID).Scan(&tagContent) 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 { + log.Println(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 { + log.Println(err) + } + } + return exists +} + +func hasSticky(guildID string, channelID string) bool { + var exists bool + err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM sticky WHERE guild_id = $1 AND channel_id = $2)", guildID, channelID).Scan(&exists) + if err != nil { + log.Println(err) + } + return exists +} + +func updateStickyMessageID(guildID string, channelID string, messageID string) { + +} diff --git a/register_commands.go b/register_commands.go index 615f2a5..e9c60d8 100644 --- a/register_commands.go +++ b/register_commands.go @@ -15,7 +15,7 @@ type Command struct { ModalID string } -var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command, ask_command} +var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command, sticky_command, ask_command} func ready(s *discordgo.Session, event *discordgo.Ready) { for _, guild := range event.Guilds { @@ -40,7 +40,7 @@ func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) { if command.Autocomplete != nil && i.ApplicationCommandData().Name == command.Definition.Name { command.Autocomplete(s, i) } - case discordgo.InteractionModalSubmit: //g has no modal so it crashes + case discordgo.InteractionModalSubmit: if command.ModalSubmit != nil && strings.HasPrefix(i.ModalSubmitData().CustomID, command.ModalID) { command.ModalSubmit(s, i) }