From ba99b2113cf323264657b835f4650ab87db268df Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Fri, 22 Jul 2022 17:04:51 -0400 Subject: [PATCH] discord: add slash command structure --- bot/bot.go | 12 ++++++++++-- bot/interfaces.go | 3 +++ connectors/discord/discord.go | 22 ++++++++++++++++++++++ connectors/irc/irc.go | 2 ++ connectors/slackapp/slackApp.go | 2 ++ plugins/cli/cli.go | 2 ++ plugins/cowboy/cowboy.go | 9 +++++++++ 7 files changed, 50 insertions(+), 2 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index d3cfecf..d6f19ec 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -6,6 +6,8 @@ import ( "fmt" "math/rand" "net/http" + "os" + "os/signal" "reflect" "regexp" "strings" @@ -135,8 +137,14 @@ func New(config *config.Config, connector Connector) Bot { func (b *bot) ListenAndServe() { addr := b.config.Get("HttpAddr", "127.0.0.1:1337") - log.Debug().Msgf("starting web service at %s", addr) - log.Fatal().Err(http.ListenAndServe(addr, b.router)).Msg("bot killed") + stop := make(chan os.Signal, 1) + signal.Notify(stop, os.Interrupt) + go func() { + log.Debug().Msgf("starting web service at %s", addr) + log.Fatal().Err(http.ListenAndServe(addr, b.router)).Msg("bot killed") + }() + <-stop + b.DefaultConnector().Shutdown() } func (b *bot) RegisterWeb(r http.Handler, root string) { diff --git a/bot/interfaces.go b/bot/interfaces.go index 6234d11..73ac9d2 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -191,6 +191,9 @@ type Connector interface { // Serve starts a connector's connection routine Serve() error + // Shutdown cleans up after the connection + Shutdown() + // Who returns a user list for a channel Who(string) []string diff --git a/connectors/discord/discord.go b/connectors/discord/discord.go index 61d4efe..9e77267 100644 --- a/connectors/discord/discord.go +++ b/connectors/discord/discord.go @@ -27,6 +27,8 @@ type Discord struct { // store IDs -> nick and vice versa for quick conversion uidCache map[string]string + + registeredCmds []*discordgo.ApplicationCommand } func New(config *config.Config) *Discord { @@ -390,3 +392,23 @@ func (d *Discord) SetRole(userID, roleID string) error { } return d.client.GuildMemberRoleAdd(guildID, userID, roleID) } + +func (d *Discord) RegisterSlashCmd(c discordgo.ApplicationCommand) error { + guildID := d.config.Get("discord.guildid", "") + cmd, err := d.client.ApplicationCommandCreate(d.client.State.User.ID, guildID, &c) + if err != nil { + return err + } + d.registeredCmds = append(d.registeredCmds, cmd) + return nil +} + +func (d *Discord) Shutdown() { + log.Debug().Msgf("Shutting down and deleting %d slash commands", len(d.registeredCmds)) + guildID := d.config.Get("discord.guildid", "") + for _, c := range d.registeredCmds { + if err := d.client.ApplicationCommandDelete(d.client.State.User.ID, guildID, c.ID); err != nil { + log.Error().Err(err).Msgf("could not delete command %s", c.Name) + } + } +} diff --git a/connectors/irc/irc.go b/connectors/irc/irc.go index d850690..ce97fdb 100644 --- a/connectors/irc/irc.go +++ b/connectors/irc/irc.go @@ -359,3 +359,5 @@ func (i Irc) GetRoles() ([]bot.Role, error) { func (i Irc) SetRole(userID, roleID string) error { return nil } + +func (i Irc) Shutdown() {} diff --git a/connectors/slackapp/slackApp.go b/connectors/slackapp/slackApp.go index 13327eb..7ab5212 100644 --- a/connectors/slackapp/slackApp.go +++ b/connectors/slackapp/slackApp.go @@ -749,3 +749,5 @@ func (s *SlackApp) GetRoles() ([]bot.Role, error) { func (s *SlackApp) SetRole(userID, roleID string) error { return nil } + +func (s *SlackApp) Shutdown() {} diff --git a/plugins/cli/cli.go b/plugins/cli/cli.go index c3d7454..b87b08b 100644 --- a/plugins/cli/cli.go +++ b/plugins/cli/cli.go @@ -43,6 +43,8 @@ func (p *CliPlugin) registerWeb() { p.bot.RegisterWebName(r, "/cli", "CLI") } +func (p *CliPlugin) Shutdown() {} + func (p *CliPlugin) GetRouter() (http.Handler, string) { return nil, "" } diff --git a/plugins/cowboy/cowboy.go b/plugins/cowboy/cowboy.go index a8ad362..b224d33 100644 --- a/plugins/cowboy/cowboy.go +++ b/plugins/cowboy/cowboy.go @@ -2,6 +2,7 @@ package cowboy import ( "fmt" + "github.com/velour/catbase/connectors/discord" "regexp" "github.com/rs/zerolog/log" @@ -28,6 +29,10 @@ func New(b bot.Bot) *Cowboy { } c.register() c.registerWeb() + switch conn := b.DefaultConnector().(type) { + case *discord.Discord: + c.registerCmds(conn) + } return &c } @@ -57,3 +62,7 @@ func (p *Cowboy) makeCowboy(r bot.Request) { Height: 64, }) } + +func (p *Cowboy) registerCmds(d *discord.Discord) { + //d.RegisterSlashCmd() +}