started adding sticky messages

This commit is contained in:
2024-02-21 16:10:52 +01:00
parent 06809f2c40
commit 304f93e28d
5 changed files with 120 additions and 5 deletions

64
cmd_sticky.go Normal file
View File

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

13
handle_messages.go Normal file
View File

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

View File

@@ -38,10 +38,11 @@ func main() {
} else { } else {
fmt.Println("Discord session created") fmt.Println("Discord session created")
} }
discord.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuilds discord.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuilds | discordgo.IntentMessageContent
defer removeCommandFromAllGuilds(discord) defer removeCommandFromAllGuilds(discord)
discord.AddHandler(ready) discord.AddHandler(ready)
discord.AddHandler(interactionCreate) discord.AddHandler(interactionCreate)
discord.AddHandler(messageCreate)
err = discord.Open() err = discord.Open()
if err != nil { if err != nil {
fmt.Println("error opening connection,", err) fmt.Println("error opening connection,", err)

View File

@@ -13,7 +13,15 @@ func initTables() {
tag_content TEXT NOT NULL, tag_content TEXT NOT NULL,
guild_id 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) _, err := db.Exec(createTableQuery)
if err != nil { 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) db.QueryRow("SELECT tag_content FROM tags WHERE guild_id = $1 AND tag_id = $2", guildID, tagID).Scan(&tagContent)
return 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) {
}

View File

@@ -15,7 +15,7 @@ type Command struct {
ModalID string 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) { func ready(s *discordgo.Session, event *discordgo.Ready) {
for _, guild := range event.Guilds { 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 { if command.Autocomplete != nil && i.ApplicationCommandData().Name == command.Definition.Name {
command.Autocomplete(s, i) 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) { if command.ModalSubmit != nil && strings.HasPrefix(i.ModalSubmitData().CustomID, command.ModalID) {
command.ModalSubmit(s, i) command.ModalSubmit(s, i)
} }