|
@@ -15,6 +15,8 @@ type RouteOptions struct {
|
|
Category string // The category to put the route under
|
|
Category string // The category to put the route under
|
|
Type RouteType // The type of route this is
|
|
Type RouteType // The type of route this is
|
|
Callback Handler // route handler function to call
|
|
Callback Handler // route handler function to call
|
|
|
|
+ Hidden bool // The route is hidden from the help menu'
|
|
|
|
+ Admin bool // The route is only visible to admins
|
|
}
|
|
}
|
|
|
|
|
|
type route struct {
|
|
type route struct {
|
|
@@ -24,6 +26,8 @@ type route struct {
|
|
CategoryID int
|
|
CategoryID int
|
|
Type RouteType
|
|
Type RouteType
|
|
Run Handler
|
|
Run Handler
|
|
|
|
+ Hidden bool
|
|
|
|
+ Admin bool
|
|
}
|
|
}
|
|
|
|
|
|
type category struct {
|
|
type category struct {
|
|
@@ -42,15 +46,15 @@ const (
|
|
|
|
|
|
// Handler is the function signature required for a message route handler.
|
|
// Handler is the function signature required for a message route handler.
|
|
type Handler func(*discordgo.Session, *discordgo.Message, *Context)
|
|
type Handler func(*discordgo.Session, *discordgo.Message, *Context)
|
|
|
|
+type IsAdminRouteHandler func(*discordgo.Session, *discordgo.Message, *Context) bool
|
|
|
|
|
|
// Context holds a bit of extra data we pass along to route handlers
|
|
// Context holds a bit of extra data we pass along to route handlers
|
|
// This way processing some of this only needs to happen once.
|
|
// This way processing some of this only needs to happen once.
|
|
type Context struct {
|
|
type Context struct {
|
|
- Fields []string
|
|
|
|
- Args []string
|
|
|
|
- Content string
|
|
|
|
- isDirectMessage bool // Indicates the message was sent via direct message
|
|
|
|
- Method Method
|
|
|
|
|
|
+ Fields []string
|
|
|
|
+ Args []string
|
|
|
|
+ Content string
|
|
|
|
+ Method Method
|
|
}
|
|
}
|
|
|
|
|
|
// Method is the method a command was received
|
|
// Method is the method a command was received
|
|
@@ -67,16 +71,19 @@ 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
|
|
|
|
|
|
+ routes []*route
|
|
|
|
+ helpRoute *route
|
|
|
|
+ prefix string
|
|
|
|
+ categories []*category
|
|
|
|
+ isAdminRoute IsAdminRouteHandler // the admin check handler func to call
|
|
}
|
|
}
|
|
|
|
|
|
// New returns a new Discord message route mux
|
|
// New returns a new Discord message route mux
|
|
func New() *Router {
|
|
func New() *Router {
|
|
r := &Router{}
|
|
r := &Router{}
|
|
|
|
|
|
|
|
+ r.isAdminRoute = isAdminRouteDefault
|
|
|
|
+
|
|
helpRoute := &route{}
|
|
helpRoute := &route{}
|
|
helpRoute.Name = "help"
|
|
helpRoute.Name = "help"
|
|
helpRoute.Usage = "help"
|
|
helpRoute.Usage = "help"
|
|
@@ -93,14 +100,18 @@ func (r *Router) SetCommandPrefix(cmdPrefix string) {
|
|
r.prefix = cmdPrefix + " "
|
|
r.prefix = cmdPrefix + " "
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (r *Router) SetAdminRouteCallback(callback IsAdminRouteHandler) {
|
|
|
|
+ r.isAdminRoute = callback
|
|
|
|
+}
|
|
|
|
+
|
|
// 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")
|
|
|
|
|
|
// ErrRegisterInvalidRouteType is returned when registering a new route with an invalid type
|
|
// ErrRegisterInvalidRouteType is returned when registering a new route with an invalid type
|
|
-var ErrRegisterInvalidRouteType = errors.New("Invalid route type")
|
|
|
|
|
|
+var ErrRegisterInvalidRouteType = errors.New("invalid route type")
|
|
|
|
|
|
// ErrRegisterCallbackRequired is returned when registering a new route without a callback
|
|
// ErrRegisterCallbackRequired is returned when registering a new route without a callback
|
|
-var ErrRegisterCallbackRequired = errors.New("A valid callback must be provided")
|
|
|
|
|
|
+var ErrRegisterCallbackRequired = errors.New("a valid callback must be provided")
|
|
|
|
|
|
// Register allows you to register a route
|
|
// Register allows you to register a route
|
|
//func (r *Router) Register(routeType RouteType, name, desc string, callback Handler) (*Route, error) {
|
|
//func (r *Router) Register(routeType RouteType, name, desc string, callback Handler) (*Route, error) {
|
|
@@ -123,6 +134,8 @@ func (r *Router) Register(name string, options RouteOptions) error {
|
|
route.Description = options.Description
|
|
route.Description = options.Description
|
|
route.Usage = options.Usage
|
|
route.Usage = options.Usage
|
|
route.Run = options.Callback
|
|
route.Run = options.Callback
|
|
|
|
+ route.Hidden = options.Hidden
|
|
|
|
+ route.Admin = options.Admin
|
|
|
|
|
|
if options.Category != "" {
|
|
if options.Category != "" {
|
|
for _, category := range r.categories {
|
|
for _, category := range r.categories {
|
|
@@ -139,7 +152,7 @@ func (r *Router) Register(name string, options RouteOptions) error {
|
|
}
|
|
}
|
|
|
|
|
|
// ErrAddCategoryNameRequired is returned when registering a new route without a name
|
|
// ErrAddCategoryNameRequired is returned when registering a new route without a name
|
|
-var ErrAddCategoryNameRequired = errors.New("All categories must have a name")
|
|
|
|
|
|
+var ErrAddCategoryNameRequired = errors.New("all categories must have a name")
|
|
|
|
|
|
// AddCategory allows you to a a router category
|
|
// AddCategory allows you to a a router category
|
|
func (r *Router) AddCategory(name, description string) error {
|
|
func (r *Router) AddCategory(name, description string) error {
|
|
@@ -179,10 +192,21 @@ func (r *Router) findRoute(searchString string, routeType RouteType) (*route, []
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
|
|
|
|
- if strings.ToLower(commandKey) == strings.ToLower(route.Name) {
|
|
|
|
|
|
+ if strings.EqualFold(commandKey, route.Name) {
|
|
return route, fields[0:], fields[1:]
|
|
return route, fields[0:], fields[1:]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return nil, nil, nil
|
|
return nil, nil, nil
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// Default callback for determining if the caller is an admin
|
|
|
|
+func isAdminRouteDefault(s *discordgo.Session, m *discordgo.Message, ctx *Context) bool {
|
|
|
|
+ channelPermissions, err := s.UserChannelPermissions(m.Author.ID, m.ChannelID)
|
|
|
|
+
|
|
|
|
+ if err != nil {
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return (channelPermissions&discordgo.PermissionAdministrator > 0)
|
|
|
|
+}
|