From 1538b3b65ce33e1f782e3869176050dc1c8d55fc Mon Sep 17 00:00:00 2001 From: Marc Date: Fri, 28 May 2021 19:39:40 +0100 Subject: [PATCH] h --- features/features.go | 10 ++++++---- features/handlers.go | 12 +++++++++++- features/misc.go | 4 ++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/features/features.go b/features/features.go index 2a9769d..86ce720 100644 --- a/features/features.go +++ b/features/features.go @@ -9,6 +9,7 @@ import ( type ScuzzyHandler func(session *discordgo.Session, m *discordgo.MessageCreate) error type ScuzzyCommand struct { + Index int Name string Description string AdminOnly bool @@ -16,8 +17,9 @@ type ScuzzyCommand struct { } type Features struct { - Token string - Permissions *permissions.Permissions - Config *models.Configuration - ScuzzyCommands map[string]ScuzzyCommand + Token string + Permissions *permissions.Permissions + Config *models.Configuration + ScuzzyCommands map[string]ScuzzyCommand + ScuzzyCommandsByIndex map[int]ScuzzyCommand } diff --git a/features/handlers.go b/features/handlers.go index 8b211de..86dec91 100644 --- a/features/handlers.go +++ b/features/handlers.go @@ -3,6 +3,7 @@ package features import ( "errors" "log" + "sort" "strconv" "strings" @@ -12,16 +13,19 @@ import ( func (f *Features) RegisterCommand(name string, description string, adminonly bool, handler ScuzzyHandler) { log.Printf("[*] Registering Command '%s'\n", name) c := ScuzzyCommand{ + Index: len(f.ScuzzyCommands) + 1, Name: name, Description: description, AdminOnly: adminonly, Handler: handler, } f.ScuzzyCommands[name] = c + f.ScuzzyCommandsByIndex[c.Index] = c } func (f *Features) RegisterHandlers() { f.ScuzzyCommands = make(map[string]ScuzzyCommand) + f.ScuzzyCommandsByIndex = make(map[int]ScuzzyCommand) // Misc Commands f.RegisterCommand("help", "Show Help Text", false, f.handleHelp) @@ -64,6 +68,11 @@ func (f *Features) RegisterHandlers() { f.RegisterCommand("saveconfig", "Save Configuration to Disk", true, f.handleSaveConfig) f.RegisterCommand("reloadconfig", "Reload Configuration", true, f.handleReloadConfig) f.RegisterCommand("addrole", "Add a joinable role", true, f.handleAddCustomRole) + + sort.Slice(f.ScuzzyCommandsByIndex, func(i, j int) bool { + return f.ScuzzyCommandsByIndex[i].Index > f.ScuzzyCommandsByIndex[j].Index + }) + } func (f *Features) ProcessCommand(s *discordgo.Session, m *discordgo.MessageCreate) error { @@ -95,7 +104,8 @@ func (f *Features) ProcessCommand(s *discordgo.Session, m *discordgo.MessageCrea if cmd, ok := f.ScuzzyCommands[cName]; ok { if cmd.AdminOnly && !f.Permissions.CheckAdminRole(m.Member) { - return errors.New("You do not have permissions to use this command.") + log.Printf("[*] User %s tried to run admin command %s\n", m.Author.Username, cName) + return nil } log.Printf("[*] Running command %s (Requested by %s)\n", cName, m.Author.Username) diff --git a/features/misc.go b/features/misc.go index 8a8cd16..cf0cf0e 100644 --- a/features/misc.go +++ b/features/misc.go @@ -289,7 +289,7 @@ func (f *Features) handleInfo(s *discordgo.Session, m *discordgo.MessageCreate) func (f *Features) handleHelp(s *discordgo.Session, m *discordgo.MessageCreate) error { desc := "**Available Commands**\n" - for _, command := range f.ScuzzyCommands { + for _, command := range f.ScuzzyCommandsByIndex { if !command.AdminOnly && command.Description != "" { desc += "`" + command.Name + "` - " + command.Description + "\n" } @@ -298,7 +298,7 @@ func (f *Features) handleHelp(s *discordgo.Session, m *discordgo.MessageCreate) if f.Permissions.CheckAdminRole(m.Member) { desc += "\n" desc += "**Admin Commands**\n" - for _, command := range f.ScuzzyCommands { + for _, command := range f.ScuzzyCommandsByIndex { if command.AdminOnly { desc += "`" + command.Name + "` - " + command.Description + "\n" }