mirror of https://github.com/velour/catbase.git
discord: add slash command structure
This commit is contained in:
parent
22658949a8
commit
ba99b2113c
|
@ -6,6 +6,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -135,8 +137,14 @@ func New(config *config.Config, connector Connector) Bot {
|
||||||
|
|
||||||
func (b *bot) ListenAndServe() {
|
func (b *bot) ListenAndServe() {
|
||||||
addr := b.config.Get("HttpAddr", "127.0.0.1:1337")
|
addr := b.config.Get("HttpAddr", "127.0.0.1:1337")
|
||||||
|
stop := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(stop, os.Interrupt)
|
||||||
|
go func() {
|
||||||
log.Debug().Msgf("starting web service at %s", addr)
|
log.Debug().Msgf("starting web service at %s", addr)
|
||||||
log.Fatal().Err(http.ListenAndServe(addr, b.router)).Msg("bot killed")
|
log.Fatal().Err(http.ListenAndServe(addr, b.router)).Msg("bot killed")
|
||||||
|
}()
|
||||||
|
<-stop
|
||||||
|
b.DefaultConnector().Shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *bot) RegisterWeb(r http.Handler, root string) {
|
func (b *bot) RegisterWeb(r http.Handler, root string) {
|
||||||
|
|
|
@ -191,6 +191,9 @@ type Connector interface {
|
||||||
// Serve starts a connector's connection routine
|
// Serve starts a connector's connection routine
|
||||||
Serve() error
|
Serve() error
|
||||||
|
|
||||||
|
// Shutdown cleans up after the connection
|
||||||
|
Shutdown()
|
||||||
|
|
||||||
// Who returns a user list for a channel
|
// Who returns a user list for a channel
|
||||||
Who(string) []string
|
Who(string) []string
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,8 @@ type Discord struct {
|
||||||
|
|
||||||
// store IDs -> nick and vice versa for quick conversion
|
// store IDs -> nick and vice versa for quick conversion
|
||||||
uidCache map[string]string
|
uidCache map[string]string
|
||||||
|
|
||||||
|
registeredCmds []*discordgo.ApplicationCommand
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(config *config.Config) *Discord {
|
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)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -359,3 +359,5 @@ func (i Irc) GetRoles() ([]bot.Role, error) {
|
||||||
func (i Irc) SetRole(userID, roleID string) error {
|
func (i Irc) SetRole(userID, roleID string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i Irc) Shutdown() {}
|
||||||
|
|
|
@ -749,3 +749,5 @@ func (s *SlackApp) GetRoles() ([]bot.Role, error) {
|
||||||
func (s *SlackApp) SetRole(userID, roleID string) error {
|
func (s *SlackApp) SetRole(userID, roleID string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SlackApp) Shutdown() {}
|
||||||
|
|
|
@ -43,6 +43,8 @@ func (p *CliPlugin) registerWeb() {
|
||||||
p.bot.RegisterWebName(r, "/cli", "CLI")
|
p.bot.RegisterWebName(r, "/cli", "CLI")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *CliPlugin) Shutdown() {}
|
||||||
|
|
||||||
func (p *CliPlugin) GetRouter() (http.Handler, string) {
|
func (p *CliPlugin) GetRouter() (http.Handler, string) {
|
||||||
return nil, ""
|
return nil, ""
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package cowboy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/velour/catbase/connectors/discord"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
@ -28,6 +29,10 @@ func New(b bot.Bot) *Cowboy {
|
||||||
}
|
}
|
||||||
c.register()
|
c.register()
|
||||||
c.registerWeb()
|
c.registerWeb()
|
||||||
|
switch conn := b.DefaultConnector().(type) {
|
||||||
|
case *discord.Discord:
|
||||||
|
c.registerCmds(conn)
|
||||||
|
}
|
||||||
return &c
|
return &c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,3 +62,7 @@ func (p *Cowboy) makeCowboy(r bot.Request) {
|
||||||
Height: 64,
|
Height: 64,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Cowboy) registerCmds(d *discord.Discord) {
|
||||||
|
//d.RegisterSlashCmd()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue