From 8cc0c09db188da76729415b37eb62bc3b9114442 Mon Sep 17 00:00:00 2001 From: Marc Egerton Date: Fri, 18 Sep 2020 12:54:07 +0100 Subject: [PATCH] Features: Add the ability for Admins to create user joinable roles. --- features/customroles.go | 34 ++++++++++++++++++++++++++++++++++ features/handlers.go | 1 + features/misc.go | 1 + 3 files changed, 36 insertions(+) diff --git a/features/customroles.go b/features/customroles.go index 7abed53..341b83e 100644 --- a/features/customroles.go +++ b/features/customroles.go @@ -1,7 +1,9 @@ package features import ( + "errors" discordgo "github.com/bwmarrin/discord.go" + "github.com/foxtrot/scuzzy/models" "strings" ) @@ -114,3 +116,35 @@ func (f *Features) handleLeaveCustomRole(s *discordgo.Session, m *discordgo.Mess return nil } + +func (f *Features) handleAddCustomRole(s *discordgo.Session, m *discordgo.MessageCreate) error { + var err error + + if !f.Permissions.CheckAdminRole(m.Member) { + return errors.New("You do not have permissions to use that command.") + } + + userInput := strings.Split(m.Content, " ") + if len(userInput) < 3 { + return errors.New("Expected Arguments: short_name role_id") + } + + shortName := userInput[1] + shortName = strings.ToLower(shortName) + roleID := userInput[2] + + customRole := models.CustomRole{ + Name: "", + ShortName: shortName, + ID: roleID, + } + + f.Config.CustomRoles = append(f.Config.CustomRoles, customRole) + + err = f.handleSaveConfig(s, m) + if err != nil { + return err + } + + return nil +} diff --git a/features/handlers.go b/features/handlers.go index a797de0..688a503 100644 --- a/features/handlers.go +++ b/features/handlers.go @@ -56,6 +56,7 @@ func (f *Features) RegisterHandlers() { f.RegisterCommand("getconfig", f.handleGetConfig) f.RegisterCommand("saveconfig", f.handleSaveConfig) f.RegisterCommand("reloadconfig", f.handleReloadConfig) + f.RegisterCommand("addrole", f.handleAddCustomRole) } func (f *Features) ProcessCommand(s *discordgo.Session, m *discordgo.MessageCreate) error { diff --git a/features/misc.go b/features/misc.go index 33d36a1..cefd49c 100644 --- a/features/misc.go +++ b/features/misc.go @@ -326,6 +326,7 @@ func (f *Features) handleHelp(s *discordgo.Session, m *discordgo.MessageCreate) desc += "`getconfig` - View the runtime configuration\n" desc += "`reloadconfig` - Reload configuration from disk\n" desc += "`saveconfig` - Save the runtime configuration to disk\n" + desc += "`addrole` - Add a user joinable role\n" } desc += "\n\nAll commands are prefixed with `" + f.Config.CommandKey + "`\n"