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 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
}

View File

@ -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)

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 {
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"
}