Browse Source

Merge branch 'master' of https://git.mgmcomp.net/thisnthat/discord

Thisnthat 2 years ago
parent
commit
2f28fdb99a
7 changed files with 39 additions and 13 deletions
  1. 5 2
      bot.go
  2. 13 5
      bot/event/mux/help.go
  3. 4 0
      bot/event/mux/message.go
  4. 10 5
      bot/event/mux/router.go
  5. 1 0
      go.mod
  6. 6 0
      go.sum
  7. 0 1
      session.go

+ 5 - 2
bot.go

@@ -9,6 +9,7 @@ import (
 
 
 	"git.mgmcomp.net/thisnthat/discord/bot/event/mux"
 	"git.mgmcomp.net/thisnthat/discord/bot/event/mux"
 	"git.mgmcomp.net/thisnthat/discordgo"
 	"git.mgmcomp.net/thisnthat/discordgo"
+	"github.com/sirupsen/logrus"
 )
 )
 
 
 type Bot struct {
 type Bot struct {
@@ -21,7 +22,7 @@ type Bot struct {
 func (bot *Bot) AddHandler(handler interface{}) error {
 func (bot *Bot) AddHandler(handler interface{}) error {
 	// If the bot is online, we can not add a handler
 	// If the bot is online, we can not add a handler
 	if bot.online {
 	if bot.online {
-		err := fmt.Errorf("Unable to add handler to a bot that is already online")
+		err := fmt.Errorf("unable to add handler to a bot that is already online")
 		return err
 		return err
 	}
 	}
 
 
@@ -41,12 +42,14 @@ func (bot *Bot) Start(wg *sync.WaitGroup) error {
 		return err
 		return err
 	}
 	}
 
 
+	go logrus.Info("The bot has started")
 	bot.running = make(chan os.Signal, 1)
 	bot.running = make(chan os.Signal, 1)
 	bot.online = true
 	bot.online = true
-	signal.Notify(bot.running, syscall.SIGINT, os.Kill)
+	signal.Notify(bot.running, syscall.SIGINT, syscall.SIGTERM)
 	<-bot.running
 	<-bot.running
 
 
 	bot.session.Close()
 	bot.session.Close()
+	go logrus.Info("The bot has gone offline")
 	bot.online = false
 	bot.online = false
 
 
 	return nil
 	return nil

+ 13 - 5
bot/event/mux/help.go

@@ -59,6 +59,10 @@ func (r *Router) helpCommandHandler(s *discordgo.Session, m *discordgo.Message,
 	keys := make([]string, 0, len(r.routes))
 	keys := make([]string, 0, len(r.routes))
 	for _, route := range r.routes {
 	for _, route := range r.routes {
 		if route.CategoryID == 0 {
 		if route.CategoryID == 0 {
+			if route.Admin && !isAdmin {
+				continue
+			}
+
 			l := len(route.Usage) // TODO: Add the +args part :)
 			l := len(route.Usage) // TODO: Add the +args part :)
 			if l > maxDisplayLen {
 			if l > maxDisplayLen {
 				maxDisplayLen = l
 				maxDisplayLen = l
@@ -88,23 +92,27 @@ func (r *Router) helpCommandHandler(s *discordgo.Session, m *discordgo.Message,
 	maxDisplayLen = maxDisplayLen + 3
 	maxDisplayLen = maxDisplayLen + 3
 
 
 	// TODO: Learn more link needs to be configurable
 	// TODO: Learn more link needs to be configurable
-	resp := "```autoit\n"
+	innerResp := ""
 
 
 	// Add sorted result to help msg
 	// Add sorted result to help msg
 	for _, catKey := range cats {
 	for _, catKey := range cats {
 		if catKey != "" {
 		if catKey != "" {
-			resp += fmt.Sprintf("\n%s\n", catKey)
+			innerResp += fmt.Sprintf("\n%s\n", catKey)
 		} else {
 		} else {
-			resp += "\n\n"
+			innerResp += "\n\n"
 		}
 		}
 
 
 		routes := catMap[catKey]
 		routes := catMap[catKey]
 		for _, routeKey := range routes {
 		for _, routeKey := range routes {
 			route := cmdmap[routeKey]
 			route := cmdmap[routeKey]
-			resp += fmt.Sprintf("%s%-"+strconv.Itoa(maxDisplayLen)+"s # %s\n", displayPrefix, route.Usage, route.Description)
+			innerResp += fmt.Sprintf("%s%-"+strconv.Itoa(maxDisplayLen)+"s # %s\n", displayPrefix, route.Usage, route.Description)
 		}
 		}
 	}
 	}
 
 
-	resp += "```\n"
+	if innerResp == "" {
+		return
+	}
+
+	resp := "```autoit\n" + innerResp + "```\n"
 	s.ChannelMessageSend(m.ChannelID, resp)
 	s.ChannelMessageSend(m.ChannelID, resp)
 }
 }

+ 4 - 0
bot/event/mux/message.go

@@ -73,6 +73,10 @@ func (r *Router) OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreat
 		}
 		}
 	}
 	}
 
 
+	if r.forcePrefixMethod && ctx.Method != PrefixMethod {
+		return
+	}
+
 	// If this is not a command do nothing
 	// If this is not a command do nothing
 	if !isCommand {
 	if !isCommand {
 		return
 		return

+ 10 - 5
bot/event/mux/router.go

@@ -71,11 +71,12 @@ const (
 
 
 // Router is the main struct for all mux methods.
 // Router is the main struct for all mux methods.
 type Router struct {
 type Router struct {
-	routes       []*route
-	helpRoute    *route
-	prefix       string
-	categories   []*category
-	isAdminRoute IsAdminRouteHandler // the admin check handler func to call
+	routes            []*route
+	helpRoute         *route
+	prefix            string
+	categories        []*category
+	isAdminRoute      IsAdminRouteHandler // the admin check handler func to call
+	forcePrefixMethod bool
 }
 }
 
 
 // New returns a new Discord message route mux
 // New returns a new Discord message route mux
@@ -104,6 +105,10 @@ func (r *Router) SetAdminRouteCallback(callback IsAdminRouteHandler) {
 	r.isAdminRoute = callback
 	r.isAdminRoute = callback
 }
 }
 
 
+func (r *Router) ForcePrefixMethod(force bool) {
+	r.forcePrefixMethod = force
+}
+
 // ErrRegisterRouteNameRequired is returned when registering a new route without a name
 // ErrRegisterRouteNameRequired is returned when registering a new route without a name
 var ErrRegisterRouteNameRequired = errors.New("all routes must have a name")
 var ErrRegisterRouteNameRequired = errors.New("all routes must have a name")
 
 

+ 1 - 0
go.mod

@@ -5,5 +5,6 @@ go 1.14
 require (
 require (
 	git.mgmcomp.net/thisnthat/discordgo v0.0.0-20211006203217-5322d32875f4
 	git.mgmcomp.net/thisnthat/discordgo v0.0.0-20211006203217-5322d32875f4
 	github.com/gorilla/websocket v1.4.2 // 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
 	golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
 )
 )

+ 6 - 0
go.sum

@@ -1,10 +1,16 @@
 git.mgmcomp.net/thisnthat/discordgo v0.0.0-20211006203217-5322d32875f4 h1:txbtCCEOuipIB0DgPubeTdw3igP+CR5Kl2gCKnYDf2M=
 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=
 git.mgmcomp.net/thisnthat/discordgo v0.0.0-20211006203217-5322d32875f4/go.mod h1:Mld4OR1WOQKeKYSMyYX3T18ilnCQCtwus+A1LoNcC50=
+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 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
 github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
 github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+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-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
 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/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=
 golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
+golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
 golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
 golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

+ 0 - 1
session.go

@@ -5,7 +5,6 @@ import "git.mgmcomp.net/thisnthat/discordgo"
 // GetSession - Get a discord session from the provided token
 // GetSession - Get a discord session from the provided token
 func GetSession(token string) (*discordgo.Session, error) {
 func GetSession(token string) (*discordgo.Session, error) {
 	session, err := discordgo.New("Bot " + token)
 	session, err := discordgo.New("Bot " + token)
-	session.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAll)
 
 
 	// If we can not create the client then just fatal.
 	// If we can not create the client then just fatal.
 	if err != nil {
 	if err != nil {