renamed command variables and added autopublishing

This commit is contained in:
2024-03-05 14:05:17 +01:00
parent 4d02026f25
commit c90e2d59fc
11 changed files with 90 additions and 10 deletions

View File

@@ -2,7 +2,7 @@ package main
import "github.com/bwmarrin/discordgo" import "github.com/bwmarrin/discordgo"
var ask_command Command = Command{ var cmd_ask Command = Command{
Definition: discordgo.ApplicationCommand{ Definition: discordgo.ApplicationCommand{
Name: "ask", Name: "ask",
Description: "Ask anything and get a gif as response!", Description: "Ask anything and get a gif as response!",

View File

@@ -2,7 +2,7 @@ package main
import "github.com/bwmarrin/discordgo" import "github.com/bwmarrin/discordgo"
var autojoinroles_command Command = Command{ var cmd_autojoinroles Command = Command{
Definition: discordgo.ApplicationCommand{ Definition: discordgo.ApplicationCommand{
Name: "autojoinroles", Name: "autojoinroles",
Description: "Give users a role when they join", Description: "Give users a role when they join",

40
cmd_autopublish.go Normal file
View File

@@ -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,
},
})
}
},
}

View File

@@ -9,7 +9,7 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
var cat_command Command = Command{ var cmd_cat Command = Command{
Definition: discordgo.ApplicationCommand{ Definition: discordgo.ApplicationCommand{
Name: "cat", Name: "cat",
Description: "Random cat pictures", Description: "Random cat pictures",

View File

@@ -4,7 +4,7 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
var dadjoke_command Command = Command{ var cmd_dadjoke Command = Command{
Definition: discordgo.ApplicationCommand{ Definition: discordgo.ApplicationCommand{
Name: "dadjoke", Name: "dadjoke",
Description: "Gives you a random joke that is as bad as your dad would tell them", Description: "Gives you a random joke that is as bad as your dad would tell them",

View File

@@ -11,7 +11,7 @@ import (
var fileData []byte var fileData []byte
var form_command Command = Command{ var cmd_form Command = Command{
Definition: discordgo.ApplicationCommand{ Definition: discordgo.ApplicationCommand{
Name: "form", Name: "form",
Description: "Create custom forms right inside Discord", Description: "Create custom forms right inside Discord",

View File

@@ -8,7 +8,7 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
var ping_command Command = Command{ var cmd_ping Command = Command{
Definition: discordgo.ApplicationCommand{ Definition: discordgo.ApplicationCommand{
Name: "ping", Name: "ping",
Description: "Returns the ping of the bot", Description: "Returns the ping of the bot",

View File

@@ -6,7 +6,7 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
var sticky_command Command = Command{ var cmd_sticky Command = Command{
Definition: discordgo.ApplicationCommand{ Definition: discordgo.ApplicationCommand{
Name: "sticky", Name: "sticky",
Description: "Stick messages to the bottom of the current channel", Description: "Stick messages to the bottom of the current channel",

View File

@@ -6,7 +6,7 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
var tag_command Command = Command{ var cmd_tag Command = Command{
Definition: discordgo.ApplicationCommand{ Definition: discordgo.ApplicationCommand{
Name: "tag", Name: "tag",
DefaultMemberPermissions: int64Ptr(discordgo.PermissionManageServer), 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{ Definition: discordgo.ApplicationCommand{
Name: "g", Name: "g",
Description: "A short command to get presaved messages.", Description: "A short command to get presaved messages.",

View File

@@ -20,7 +20,7 @@ type Command struct {
ModalIDs []string 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) { func ready(s *discordgo.Session, event *discordgo.Ready) {
fmt.Print("\nStarting up...") fmt.Print("\nStarting up...")
@@ -123,6 +123,12 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
updateStickyMessageID(m.GuildID, m.ChannelID, stickyMessage.ID) 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) { func guildMemberJoin(s *discordgo.Session, m *discordgo.GuildMemberAdd) {

View File

@@ -45,6 +45,11 @@ func initTables() {
bot_role TEXT, bot_role TEXT,
user_role TEXT, user_role TEXT,
PRIMARY KEY (guild_id) 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 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
}