Bläddra i källkod

Added support for slash commands and moved to the official go discord moedule

Thisnthat 1 år sedan
förälder
incheckning
5d49608926
6 ändrade filer med 122 tillägg och 16 borttagningar
  1. 16 5
      bot.go
  2. 97 0
      bot/event/mux/slashRouter.go
  3. 1 0
      go.mod
  4. 3 0
      go.sum
  5. 4 10
      members.go
  6. 1 1
      session.go

+ 16 - 5
bot.go

@@ -8,15 +8,16 @@ import (
 	"syscall"
 
 	"git.mgmcomp.net/thisnthat/discord/bot/event/mux"
-	"git.mgmcomp.net/thisnthat/discordgo"
+	"github.com/bwmarrin/discordgo"
 	"github.com/sirupsen/logrus"
 )
 
 type Bot struct {
-	token   string
-	session *discordgo.Session
-	online  bool
-	running chan os.Signal
+	token       string
+	session     *discordgo.Session
+	online      bool
+	running     chan os.Signal
+	slashRouter *mux.SlashRouter
 }
 
 func (bot *Bot) AddHandler(handler interface{}) error {
@@ -34,6 +35,10 @@ func (bot *Bot) SetEventRouter(router *mux.Router) {
 	bot.AddHandler(router.OnMessageCreate)
 }
 
+func (bot *Bot) SetSlashEventRouter(slashRouter *mux.SlashRouter) {
+	bot.slashRouter = slashRouter
+}
+
 func (bot *Bot) Start(wg *sync.WaitGroup) error {
 	defer wg.Done()
 
@@ -42,12 +47,18 @@ func (bot *Bot) Start(wg *sync.WaitGroup) error {
 		return err
 	}
 
+	err = bot.slashRouter.GenerateCommands(bot.session)
+	if err != nil {
+		return err
+	}
+
 	go logrus.Info("The bot has started")
 	bot.running = make(chan os.Signal, 1)
 	bot.online = true
 	signal.Notify(bot.running, syscall.SIGINT, syscall.SIGTERM)
 	<-bot.running
 
+	bot.slashRouter.CleanCommands(bot.session)
 	bot.session.Close()
 	go logrus.Info("The bot has gone offline")
 	bot.online = false

+ 97 - 0
bot/event/mux/slashRouter.go

@@ -0,0 +1,97 @@
+package mux
+
+import (
+	"github.com/bwmarrin/discordgo"
+	"github.com/sirupsen/logrus"
+)
+
+type SlashRouter struct {
+	registeredCommands []*discordgo.ApplicationCommand
+	commands           []*discordgo.ApplicationCommand
+	commandHandlers    map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate)
+	guildIDs           []string
+}
+
+type SlashRouteOptions struct {
+	Name        string // match name that should trigger this route handler
+	Description string // short description of this route
+	Callback    SlashHandler
+	Options     []*discordgo.ApplicationCommandOption // Array of application command options for the command
+}
+
+// Handler is the function signature required for a message route handler.
+type SlashHandler func(s *discordgo.Session, i *discordgo.InteractionCreate)
+
+func NewSlashRouter() *SlashRouter {
+	r := &SlashRouter{}
+
+	r.commandHandlers = make(map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate))
+
+	return r
+}
+
+func (r *SlashRouter) RegisterGuild(guildID string) {
+	r.guildIDs = append(r.guildIDs, guildID)
+}
+
+func (r *SlashRouter) Register(name string, options SlashRouteOptions) error {
+	if name == "" {
+		return ErrRegisterRouteNameRequired
+	}
+
+	if options.Callback == nil {
+		return ErrRegisterCallbackRequired
+	}
+
+	command := discordgo.ApplicationCommand{
+		Name:        name,
+		Description: options.Description,
+		Options:     options.Options,
+	}
+
+	r.commands = append(r.commands, &command)
+
+	r.commandHandlers[name] = options.Callback
+
+	return nil
+}
+
+func (r *SlashRouter) GenerateCommands(s *discordgo.Session) error {
+	r.registeredCommands = make([]*discordgo.ApplicationCommand, len(r.commands))
+
+	s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
+		if h, ok := r.commandHandlers[i.ApplicationCommandData().Name]; ok {
+			h(s, i)
+		}
+	})
+
+	for _, guildID := range r.guildIDs {
+		for i, v := range r.commands {
+			logrus.Info(v)
+			cmd, err := s.ApplicationCommandCreate(s.State.User.ID, guildID, v)
+			if err != nil {
+				logrus.Errorf("Cannot create '%v' command: %v", v.Name, err)
+				return err
+			}
+
+			r.registeredCommands[i] = cmd
+		}
+	}
+
+	return nil
+}
+
+func (r *SlashRouter) CleanCommands(s *discordgo.Session) error {
+
+	for _, guildID := range r.guildIDs {
+		for _, v := range r.registeredCommands {
+			err := s.ApplicationCommandDelete(s.State.User.ID, guildID, v.ID)
+			if err != nil {
+				logrus.Errorf("Cannot delete '%v' command: %v", v.Name, err)
+				return err
+			}
+		}
+	}
+
+	return nil
+}

+ 1 - 0
go.mod

@@ -4,6 +4,7 @@ go 1.14
 
 require (
 	git.mgmcomp.net/thisnthat/discordgo v0.0.0-20211006203217-5322d32875f4
+	github.com/bwmarrin/discordgo v0.27.1 // indirect
 	github.com/gorilla/websocket v1.4.2 // indirect
 	github.com/sirupsen/logrus v1.8.1
 	golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect

+ 3 - 0
go.sum

@@ -1,5 +1,7 @@
 git.mgmcomp.net/thisnthat/discordgo v0.0.0-20211006203217-5322d32875f4 h1:txbtCCEOuipIB0DgPubeTdw3igP+CR5Kl2gCKnYDf2M=
 git.mgmcomp.net/thisnthat/discordgo v0.0.0-20211006203217-5322d32875f4/go.mod h1:Mld4OR1WOQKeKYSMyYX3T18ilnCQCtwus+A1LoNcC50=
+github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY=
+github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
 github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
@@ -7,6 +9,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
 github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
 github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
 github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
+golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
 golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
 golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
 golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=

+ 4 - 10
members.go

@@ -1,15 +1,9 @@
 package discord
 
 import (
-	"git.mgmcomp.net/thisnthat/discordgo"
+	"github.com/bwmarrin/discordgo"
 )
 
-// MemberUpdate provides the data structure for a member update
-type MemberUpdate struct {
-	Nick  string
-	Roles []string
-}
-
 // GetMember gets a discord member.
 // session  : An active discordgo session
 // guid		: The ID of the Guild
@@ -21,10 +15,10 @@ func GetMember(session *discordgo.Session, guid, uid string) (*discordgo.Member,
 // UpdateMember edits the roles of a member.
 // session  : An active discordgo session
 // guid		: The ID of the Guild
-// uid	    : The ID of a User.
+// template : GuildMemberParams for Role and Nick
 // roles    : A list of role ID's to set on the member.
-func UpdateMember(session *discordgo.Session, guid, uid string, template MemberUpdate) error {
-	err := session.GuildMemberEditCustom(guid, uid, template.Nick, template.Roles)
+func UpdateMember(session *discordgo.Session, guid, uid string, template *discordgo.GuildMemberParams) error {
+	_, err := session.GuildMemberEdit(guid, uid, template)
 
 	return err
 }

+ 1 - 1
session.go

@@ -1,6 +1,6 @@
 package discord
 
-import "git.mgmcomp.net/thisnthat/discordgo"
+import "github.com/bwmarrin/discordgo"
 
 // GetSession - Get a discord session from the provided token
 func GetSession(token string) (*discordgo.Session, error) {