added sticky messages
This commit is contained in:
@@ -11,35 +11,75 @@ var sticky_command Command = Command{
|
|||||||
Name: "sticky",
|
Name: "sticky",
|
||||||
Description: "Stick messages to the bottom of the current channel",
|
Description: "Stick messages to the bottom of the current channel",
|
||||||
DefaultMemberPermissions: int64Ptr(discordgo.PermissionManageMessages),
|
DefaultMemberPermissions: int64Ptr(discordgo.PermissionManageMessages),
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "add",
|
||||||
|
Description: "Stick messages to the bottom of the current channel",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "remove",
|
||||||
|
Description: "Remove the sticky message of the current channel",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
switch i.ApplicationCommandData().Options[0].Name {
|
||||||
Type: discordgo.InteractionResponseModal,
|
case "add":
|
||||||
Data: &discordgo.InteractionResponseData{
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
CustomID: "sticky_modal",
|
Type: discordgo.InteractionResponseModal,
|
||||||
Title: "Sticky message",
|
Data: &discordgo.InteractionResponseData{
|
||||||
Components: []discordgo.MessageComponent{
|
CustomID: "sticky_modal",
|
||||||
discordgo.ActionsRow{
|
Title: "Sticky message",
|
||||||
Components: []discordgo.MessageComponent{
|
Components: []discordgo.MessageComponent{
|
||||||
discordgo.TextInput{
|
discordgo.ActionsRow{
|
||||||
CustomID: "sticky_modal_text",
|
Components: []discordgo.MessageComponent{
|
||||||
Label: "Text",
|
discordgo.TextInput{
|
||||||
Style: discordgo.TextInputParagraph,
|
CustomID: "sticky_modal_text",
|
||||||
Placeholder: "The message you want to stick to the bottom of this channel",
|
Label: "Text",
|
||||||
Required: true,
|
Style: discordgo.TextInputParagraph,
|
||||||
MaxLength: 2000,
|
Placeholder: "The message you want to stick to the bottom of this channel",
|
||||||
Value: "",
|
Required: true,
|
||||||
|
MaxLength: 2000,
|
||||||
|
Value: "",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
case "remove":
|
||||||
|
if hasSticky(i.GuildID, i.ChannelID) {
|
||||||
|
s.ChannelMessageDelete(i.ChannelID, getStickyMessageID(i.GuildID, i.ChannelID))
|
||||||
|
removeSticky(i.GuildID, i.ChannelID)
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "The sticky message was removed from this channel!",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "This channel has no sticky message!",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
ModalID: "sticky_modal",
|
ModalID: "sticky_modal",
|
||||||
ModalSubmit: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
ModalSubmit: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
text := i.ModalSubmitData().Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value
|
text := i.ModalSubmitData().Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value
|
||||||
message, err := s.ChannelMessageSend(i.ChannelID, text)
|
message, err := s.ChannelMessageSendEmbed(i.ChannelID, &discordgo.MessageEmbed{
|
||||||
|
Type: discordgo.EmbedTypeArticle,
|
||||||
|
Title: ":pushpin: Sticky message",
|
||||||
|
Color: hexToDecimal(color["primary"]),
|
||||||
|
Description: text,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
@@ -1,13 +1,20 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
if m.Author.ID != s.State.User.ID {
|
if m.Author.ID != s.State.User.ID {
|
||||||
fmt.Print(m.Content)
|
if hasSticky(m.GuildID, m.ChannelID) {
|
||||||
|
s.ChannelMessageDelete(m.ChannelID, getStickyMessageID(m.GuildID, m.ChannelID))
|
||||||
|
stickyMessage, _ := s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{
|
||||||
|
Type: discordgo.EmbedTypeArticle,
|
||||||
|
Title: ":pushpin: Sticky message",
|
||||||
|
Color: hexToDecimal(color["primary"]),
|
||||||
|
Description: getStickyMessageContent(m.GuildID, m.ChannelID),
|
||||||
|
})
|
||||||
|
updateStickyMessageID(m.GuildID, m.ChannelID, stickyMessage.ID)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -122,6 +122,42 @@ func hasSticky(guildID string, channelID string) bool {
|
|||||||
return exists
|
return exists
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateStickyMessageID(guildID string, channelID string, messageID string) {
|
func getStickyMessageID(guildID string, channelID string) string {
|
||||||
|
var messageID string
|
||||||
|
exists := hasSticky(guildID, channelID)
|
||||||
|
if exists {
|
||||||
|
err := db.QueryRow("SELECT message_id FROM sticky WHERE guild_id = $1 AND channel_id = $2", guildID, channelID).Scan(&messageID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return messageID
|
||||||
|
}
|
||||||
|
func getStickyMessageContent(guildID string, channelID string) string {
|
||||||
|
var messageID string
|
||||||
|
exists := hasSticky(guildID, channelID)
|
||||||
|
if exists {
|
||||||
|
err := db.QueryRow("SELECT message_content FROM sticky WHERE guild_id = $1 AND channel_id = $2", guildID, channelID).Scan(&messageID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return messageID
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateStickyMessageID(guildID string, channelID string, messageID string) {
|
||||||
|
exists := hasSticky(guildID, channelID)
|
||||||
|
if exists {
|
||||||
|
_, err := db.Exec("UPDATE sticky SET message_id = $1 WHERE guild_id = $2 AND channel_id = $3", messageID, guildID, channelID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeSticky(guildID string, channelID string) {
|
||||||
|
_, err := db.Exec("DELETE FROM sticky WHERE guild_id = $1 AND channel_id = $2", guildID, channelID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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, sticky_command, ask_command}
|
var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command, ask_command, sticky_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 {
|
||||||
|
Reference in New Issue
Block a user