From c90e2d59fc3f09cd816b32fd3618fbeeb1d109c2 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Tue, 5 Mar 2024 14:05:17 +0100 Subject: [PATCH] renamed command variables and added autopublishing --- cmd_ask.go | 2 +- cmd_autojoinroles.go | 2 +- cmd_autopublish.go | 40 ++++++++++++++++++++++++++++++++++++++++ cmd_cat.go | 2 +- cmd_dadjoke.go | 2 +- cmd_form.go | 2 +- cmd_ping.go | 2 +- cmd_sticky.go | 2 +- cmd_tag.go | 4 ++-- handlers.go | 8 +++++++- manage_data.go | 34 ++++++++++++++++++++++++++++++++++ 11 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 cmd_autopublish.go diff --git a/cmd_ask.go b/cmd_ask.go index 8fc3e62..7d6e218 100644 --- a/cmd_ask.go +++ b/cmd_ask.go @@ -2,7 +2,7 @@ package main import "github.com/bwmarrin/discordgo" -var ask_command Command = Command{ +var cmd_ask Command = Command{ Definition: discordgo.ApplicationCommand{ Name: "ask", Description: "Ask anything and get a gif as response!", diff --git a/cmd_autojoinroles.go b/cmd_autojoinroles.go index 2e48912..4315750 100644 --- a/cmd_autojoinroles.go +++ b/cmd_autojoinroles.go @@ -2,7 +2,7 @@ package main import "github.com/bwmarrin/discordgo" -var autojoinroles_command Command = Command{ +var cmd_autojoinroles Command = Command{ Definition: discordgo.ApplicationCommand{ Name: "autojoinroles", Description: "Give users a role when they join", diff --git a/cmd_autopublish.go b/cmd_autopublish.go new file mode 100644 index 0000000..33157aa --- /dev/null +++ b/cmd_autopublish.go @@ -0,0 +1,40 @@ +package main + +import "github.com/bwmarrin/discordgo" + +var cmd_autopublish Command = Command{ + Definition: discordgo.ApplicationCommand{ + Name: "autopublish", + Description: "Toggle automatically publishing every post in a announcement channel", + }, + Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + channel, _ := s.State.Channel(i.ChannelID) + if channel.Type == discordgo.ChannelTypeGuildNews { + if toggleAutoPublish(i.GuildID, i.ChannelID) { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Autopublishing is now disabled on <#" + i.ChannelID + ">", + Flags: discordgo.MessageFlagsEphemeral, + }, + }) + } else { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Autopublishing is now enabled on <#" + i.ChannelID + ">", + Flags: discordgo.MessageFlagsEphemeral, + }, + }) + } + } else { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "This is not an announcement channel!", + Flags: discordgo.MessageFlagsEphemeral, + }, + }) + } + }, +} diff --git a/cmd_cat.go b/cmd_cat.go index d1d8e6b..2d26996 100644 --- a/cmd_cat.go +++ b/cmd_cat.go @@ -9,7 +9,7 @@ import ( "github.com/bwmarrin/discordgo" ) -var cat_command Command = Command{ +var cmd_cat Command = Command{ Definition: discordgo.ApplicationCommand{ Name: "cat", Description: "Random cat pictures", diff --git a/cmd_dadjoke.go b/cmd_dadjoke.go index 42b7898..b3f15e3 100644 --- a/cmd_dadjoke.go +++ b/cmd_dadjoke.go @@ -4,7 +4,7 @@ import ( "github.com/bwmarrin/discordgo" ) -var dadjoke_command Command = Command{ +var cmd_dadjoke Command = Command{ Definition: discordgo.ApplicationCommand{ Name: "dadjoke", Description: "Gives you a random joke that is as bad as your dad would tell them", diff --git a/cmd_form.go b/cmd_form.go index 6f9d64c..0870783 100644 --- a/cmd_form.go +++ b/cmd_form.go @@ -11,7 +11,7 @@ import ( var fileData []byte -var form_command Command = Command{ +var cmd_form Command = Command{ Definition: discordgo.ApplicationCommand{ Name: "form", Description: "Create custom forms right inside Discord", diff --git a/cmd_ping.go b/cmd_ping.go index 7d229dd..4c9d142 100644 --- a/cmd_ping.go +++ b/cmd_ping.go @@ -8,7 +8,7 @@ import ( "github.com/bwmarrin/discordgo" ) -var ping_command Command = Command{ +var cmd_ping Command = Command{ Definition: discordgo.ApplicationCommand{ Name: "ping", Description: "Returns the ping of the bot", diff --git a/cmd_sticky.go b/cmd_sticky.go index 0aadb58..0ad1816 100644 --- a/cmd_sticky.go +++ b/cmd_sticky.go @@ -6,7 +6,7 @@ import ( "github.com/bwmarrin/discordgo" ) -var sticky_command Command = Command{ +var cmd_sticky Command = Command{ Definition: discordgo.ApplicationCommand{ Name: "sticky", Description: "Stick messages to the bottom of the current channel", diff --git a/cmd_tag.go b/cmd_tag.go index d41dc47..e5ca55f 100644 --- a/cmd_tag.go +++ b/cmd_tag.go @@ -6,7 +6,7 @@ import ( "github.com/bwmarrin/discordgo" ) -var tag_command Command = Command{ +var cmd_tag Command = Command{ Definition: discordgo.ApplicationCommand{ Name: "tag", DefaultMemberPermissions: int64Ptr(discordgo.PermissionManageServer), @@ -115,7 +115,7 @@ var tag_command Command = Command{ }, } -var short_get_tag_command Command = Command{ +var cmd_tag_short Command = Command{ Definition: discordgo.ApplicationCommand{ Name: "g", Description: "A short command to get presaved messages.", diff --git a/handlers.go b/handlers.go index e49945b..b7b88e1 100644 --- a/handlers.go +++ b/handlers.go @@ -20,7 +20,7 @@ type Command struct { ModalIDs []string } -var commands []Command = []Command{form_command, tag_command, short_get_tag_command, dadjoke_command, ping_command, ask_command, sticky_command, cat_command, autojoinroles_command} +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} func ready(s *discordgo.Session, event *discordgo.Ready) { fmt.Print("\nStarting up...") @@ -123,6 +123,12 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { updateStickyMessageID(m.GuildID, m.ChannelID, stickyMessage.ID) } } + channel, _ := s.Channel(m.ChannelID) + if channel.Type == discordgo.ChannelTypeGuildNews { + if isAutopublishEnabled(m.GuildID, m.ChannelID) { + s.ChannelMessageCrosspost(m.ChannelID, m.ID) + } + } } func guildMemberJoin(s *discordgo.Session, m *discordgo.GuildMemberAdd) { diff --git a/manage_data.go b/manage_data.go index e20401c..1d4009b 100644 --- a/manage_data.go +++ b/manage_data.go @@ -45,6 +45,11 @@ func initTables() { bot_role TEXT, user_role TEXT, PRIMARY KEY (guild_id) + ); + CREATE TABLE IF NOT EXISTS autopublish ( + guild_id TEXT NOT NULL, + news_channel_id TEXT NOT NULL, + PRIMARY KEY (guild_id, news_channel_id) ) ` @@ -282,3 +287,32 @@ func getAutoJoinRole(guildID string, isBot bool) string { } return role } + +func toggleAutoPublish(guildID string, newsChannelID string) bool { + var exists bool + err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM autopublish WHERE guild_id = $1 AND news_channel_id = $2)", guildID, newsChannelID).Scan(&exists) + if err != nil { + log.Print(err) + } + if exists { + _, err := db.Exec("DELETE FROM autopublish WHERE guild_id = $1 AND news_channel_id = $2", guildID, newsChannelID) + if err != nil { + log.Print(err) + } + } else { + _, err := db.Exec("INSERT INTO autopublish (guild_id, news_channel_id) VALUES ($1, $2)", guildID, newsChannelID) + if err != nil { + log.Print(err) + } + } + return exists +} + +func isAutopublishEnabled(guildID string, newsChannelID string) bool { + var enabled bool + err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM autopublish WHERE guild_id = $1 AND news_channel_id = $2)", guildID, newsChannelID).Scan(&enabled) + if err != nil { + log.Print(err) + } + return enabled +}