2025-01-08 13:40:11 -08:00
|
|
|
package discord
|
|
|
|
|
|
|
|
|
|
import (
|
2025-01-13 15:45:07 -08:00
|
|
|
"fmt"
|
2025-01-08 13:40:11 -08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
|
|
|
|
|
|
"gitlab.com/whom/bingobot/internal/confession"
|
|
|
|
|
"gitlab.com/whom/bingobot/internal/logging"
|
|
|
|
|
"gitlab.com/whom/bingobot/internal/state"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
// map of guildID to registeredCommands
|
2025-01-08 15:28:24 -08:00
|
|
|
registeredCommands = make(map[string][]*discordgo.ApplicationCommand)
|
2025-01-08 13:40:11 -08:00
|
|
|
|
|
|
|
|
// all commands
|
|
|
|
|
commandList = []*discordgo.ApplicationCommand{
|
|
|
|
|
// TODO: Limit usage somehow?
|
|
|
|
|
// maybe delete this and use the vote module instead
|
|
|
|
|
&discordgo.ApplicationCommand{
|
|
|
|
|
Name: "confessional",
|
|
|
|
|
Description: "mark a channel as a designated confessional for a guild",
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
&discordgo.ApplicationCommand{
|
|
|
|
|
Name: "confess",
|
|
|
|
|
Description: "anonymously post a confession in configured channel",
|
|
|
|
|
Options: []*discordgo.ApplicationCommandOption{
|
|
|
|
|
&discordgo.ApplicationCommandOption{
|
2025-01-08 15:28:24 -08:00
|
|
|
Type: discordgo.ApplicationCommandOptionString,
|
2025-01-08 13:40:11 -08:00
|
|
|
Name: "confession",
|
|
|
|
|
Description: "A confession to be posted anonymously",
|
|
|
|
|
Required: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commandHandlers = map[string]func(
|
|
|
|
|
s *discordgo.Session,
|
|
|
|
|
i *discordgo.InteractionCreate,
|
|
|
|
|
) {
|
|
|
|
|
"confessional": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
2025-01-13 15:45:07 -08:00
|
|
|
if err := state.PublishEvent(state.ConfessionsChannelLinkEvent{
|
2025-01-08 13:40:11 -08:00
|
|
|
GuildID: i.GuildID,
|
|
|
|
|
ChannelID: i.ChannelID,
|
|
|
|
|
Created: time.Now(),
|
2025-01-13 15:45:07 -08:00
|
|
|
}); err != nil {
|
|
|
|
|
logging.Error(fmt.Sprintf(
|
|
|
|
|
"failed to publish confession channel link: %s",
|
|
|
|
|
err.Error(),
|
|
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
logging.Info("published confession channel link")
|
|
|
|
|
}
|
2025-01-08 13:40:11 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// handle a confession
|
|
|
|
|
"confess": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
|
|
|
for _, v := range i.ApplicationCommandData().Options {
|
|
|
|
|
if v.Name == "confession" {
|
|
|
|
|
confession.MakeConfession(s, i.GuildID, v.StringValue())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func handleCommand(s *discordgo.Session, e *discordgo.InteractionCreate) {
|
|
|
|
|
name := e.ApplicationCommandData().Name
|
|
|
|
|
// TODO: audit log
|
|
|
|
|
if h, ok := commandHandlers[name]; ok {
|
|
|
|
|
h(s, e)
|
|
|
|
|
} else {
|
|
|
|
|
logging.Debug("no handler for command: %s", name)
|
|
|
|
|
}
|
2025-01-13 15:45:07 -08:00
|
|
|
|
|
|
|
|
s.InteractionRespond(e.Interaction, &discordgo.InteractionResponse{
|
2025-02-17 20:24:09 -08:00
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
|
Flags: discordgo.MessageFlagsEphemeral,
|
|
|
|
|
Content: "Your cauldron bubbles...",
|
|
|
|
|
},
|
2025-01-13 15:45:07 -08:00
|
|
|
})
|
2025-01-08 13:40:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func registerCommands(s *discordgo.Session) {
|
|
|
|
|
for _, guild := range s.State.Guilds {
|
|
|
|
|
cmds, err := s.ApplicationCommandBulkOverwrite(
|
|
|
|
|
s.State.Application.ID,
|
|
|
|
|
guild.ID,
|
|
|
|
|
commandList,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2025-01-13 15:45:07 -08:00
|
|
|
logging.Error(fmt.Sprintf(
|
|
|
|
|
"Failed to register commands for guild %s: %s",
|
|
|
|
|
guild.ID, err.Error(),
|
|
|
|
|
))
|
2025-01-08 13:40:11 -08:00
|
|
|
} else {
|
2025-01-13 15:45:07 -08:00
|
|
|
logging.Info(fmt.Sprintf("Registered commands for guild %s", guild.ID))
|
2025-01-08 13:40:11 -08:00
|
|
|
registeredCommands[guild.ID] = cmds
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func deregisterCommands(s *discordgo.Session) {
|
|
|
|
|
for guild, commands := range registeredCommands {
|
|
|
|
|
for _, cmd := range commands {
|
|
|
|
|
if err := s.ApplicationCommandDelete(
|
|
|
|
|
s.State.Application.ID,
|
|
|
|
|
guild,
|
|
|
|
|
cmd.ID,
|
|
|
|
|
); err != nil {
|
2025-01-13 15:45:07 -08:00
|
|
|
logging.Error(fmt.Sprintf(
|
|
|
|
|
"Failed to delete %s command (id: %s) from guild %s",
|
|
|
|
|
cmd.Name, cmd.ID, guild,
|
|
|
|
|
))
|
2025-01-08 13:40:11 -08:00
|
|
|
} else {
|
2025-01-13 15:45:07 -08:00
|
|
|
logging.Info(fmt.Sprintf(
|
|
|
|
|
"Deregistered command %s (id: %s) from guild %s",
|
|
|
|
|
cmd.Name, cmd.ID, guild,
|
|
|
|
|
))
|
2025-01-08 13:40:11 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|