Marc 2021-05-28 19:39:40 +01:00
parent 98ccc962ea
commit 1538b3b65c
No known key found for this signature in database
GPG Key ID: 0657563F705ACAAE
3 changed files with 19 additions and 7 deletions

View File

@ -9,6 +9,7 @@ import (
type ScuzzyHandler func(session *discordgo.Session, m *discordgo.MessageCreate) error type ScuzzyHandler func(session *discordgo.Session, m *discordgo.MessageCreate) error
type ScuzzyCommand struct { type ScuzzyCommand struct {
Index int
Name string Name string
Description string Description string
AdminOnly bool AdminOnly bool
@ -16,8 +17,9 @@ type ScuzzyCommand struct {
} }
type Features struct { type Features struct {
Token string Token string
Permissions *permissions.Permissions Permissions *permissions.Permissions
Config *models.Configuration Config *models.Configuration
ScuzzyCommands map[string]ScuzzyCommand ScuzzyCommands map[string]ScuzzyCommand
ScuzzyCommandsByIndex map[int]ScuzzyCommand
} }

View File

@ -3,6 +3,7 @@ package features
import ( import (
"errors" "errors"
"log" "log"
"sort"
"strconv" "strconv"
"strings" "strings"
@ -12,16 +13,19 @@ import (
func (f *Features) RegisterCommand(name string, description string, adminonly bool, handler ScuzzyHandler) { func (f *Features) RegisterCommand(name string, description string, adminonly bool, handler ScuzzyHandler) {
log.Printf("[*] Registering Command '%s'\n", name) log.Printf("[*] Registering Command '%s'\n", name)
c := ScuzzyCommand{ c := ScuzzyCommand{
Index: len(f.ScuzzyCommands) + 1,
Name: name, Name: name,
Description: description, Description: description,
AdminOnly: adminonly, AdminOnly: adminonly,
Handler: handler, Handler: handler,
} }
f.ScuzzyCommands[name] = c f.ScuzzyCommands[name] = c
f.ScuzzyCommandsByIndex[c.Index] = c
} }
func (f *Features) RegisterHandlers() { func (f *Features) RegisterHandlers() {
f.ScuzzyCommands = make(map[string]ScuzzyCommand) f.ScuzzyCommands = make(map[string]ScuzzyCommand)
f.ScuzzyCommandsByIndex = make(map[int]ScuzzyCommand)
// Misc Commands // Misc Commands
f.RegisterCommand("help", "Show Help Text", false, f.handleHelp) 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("saveconfig", "Save Configuration to Disk", true, f.handleSaveConfig)
f.RegisterCommand("reloadconfig", "Reload Configuration", true, f.handleReloadConfig) f.RegisterCommand("reloadconfig", "Reload Configuration", true, f.handleReloadConfig)
f.RegisterCommand("addrole", "Add a joinable role", true, f.handleAddCustomRole) 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 { 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, ok := f.ScuzzyCommands[cName]; ok {
if cmd.AdminOnly && !f.Permissions.CheckAdminRole(m.Member) { 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) log.Printf("[*] Running command %s (Requested by %s)\n", cName, m.Author.Username)

View File

@ -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 { func (f *Features) handleHelp(s *discordgo.Session, m *discordgo.MessageCreate) error {
desc := "**Available Commands**\n" desc := "**Available Commands**\n"
for _, command := range f.ScuzzyCommands { for _, command := range f.ScuzzyCommandsByIndex {
if !command.AdminOnly && command.Description != "" { if !command.AdminOnly && command.Description != "" {
desc += "`" + command.Name + "` - " + command.Description + "\n" 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) { if f.Permissions.CheckAdminRole(m.Member) {
desc += "\n" desc += "\n"
desc += "**Admin Commands**\n" desc += "**Admin Commands**\n"
for _, command := range f.ScuzzyCommands { for _, command := range f.ScuzzyCommandsByIndex {
if command.AdminOnly { if command.AdminOnly {
desc += "`" + command.Name + "` - " + command.Description + "\n" desc += "`" + command.Name + "` - " + command.Description + "\n"
} }