Pārlūkot izejas kodu

Added support for forcing prefix bot commands

Mathew Medoff 3 gadi atpakaļ
vecāks
revīzija
14c3558085
3 mainītis faili ar 27 papildinājumiem un 10 dzēšanām
  1. 13 5
      bot/event/mux/help.go
  2. 4 0
      bot/event/mux/message.go
  3. 10 5
      bot/event/mux/router.go

+ 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))
 	for _, route := range r.routes {
 		if route.CategoryID == 0 {
+			if route.Admin && !isAdmin {
+				continue
+			}
+
 			l := len(route.Usage) // TODO: Add the +args part :)
 			if l > maxDisplayLen {
 				maxDisplayLen = l
@@ -88,23 +92,27 @@ func (r *Router) helpCommandHandler(s *discordgo.Session, m *discordgo.Message,
 	maxDisplayLen = maxDisplayLen + 3
 
 	// TODO: Learn more link needs to be configurable
-	resp := "```autoit\n"
+	innerResp := ""
 
 	// Add sorted result to help msg
 	for _, catKey := range cats {
 		if catKey != "" {
-			resp += fmt.Sprintf("\n%s\n", catKey)
+			innerResp += fmt.Sprintf("\n%s\n", catKey)
 		} else {
-			resp += "\n\n"
+			innerResp += "\n\n"
 		}
 
 		routes := catMap[catKey]
 		for _, routeKey := range routes {
 			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)
 }

+ 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 !isCommand {
 		return

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

@@ -71,11 +71,12 @@ const (
 
 // Router is the main struct for all mux methods.
 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
@@ -104,6 +105,10 @@ func (r *Router) SetAdminRouteCallback(callback IsAdminRouteHandler) {
 	r.isAdminRoute = callback
 }
 
+func (r *Router) ForcePrefixMethod(force bool) {
+	r.forcePrefixMethod = force
+}
+
 // ErrRegisterRouteNameRequired is returned when registering a new route without a name
 var ErrRegisterRouteNameRequired = errors.New("all routes must have a name")