package discord import ( "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 registeredCommands = make(map[string][]*discordgo.ApplicationCommand) // 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{ Type: discordgo.ApplicationCommandOptionString, 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) { state.PublishEvent(state.ConfessionsChannelLinkEvent{ GuildID: i.GuildID, ChannelID: i.ChannelID, Created: time.Now(), }) }, // 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) } } 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 { logging.Error( "Failed to register commands for guild", guild.ID, ": ", err.Error(), ) } else { logging.Info("Registered commands for guild ", guild.ID) 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 { logging.Error( "Failed to delete", cmd.Name, "command (id:", cmd.ID, ") from guild ", guild, ) } else { logging.Info( "Deregistered command ", cmd.Name,"(id:", cmd.ID, ") from guild ", guild, ) } } } }