Skip to content

Commit

Permalink
Add !createrole, fix getconfig and setconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik McClure committed Jan 26, 2018
1 parent 31aaa8b commit 43b6025
Show file tree
Hide file tree
Showing 22 changed files with 331 additions and 165 deletions.
19 changes: 13 additions & 6 deletions README.md
Expand Up @@ -47,25 +47,32 @@ For example: `!setup @Mods #staff-lounge #bot-log`
Additional configuration is optional via `!setconfig` but usually isn't necessary. **DO NOT SET PRESSURE VALUES UNLESS YOU NEED TO CHANGE THEM.** The pressure values are *already set up for you* and setting them incorrectly will result in Sweetie Bot silencing everyone instantly.

### Configuration
Basic configuration parameters can be set with `!setconfig <parameter name> <value>`. To get a list of configuration parameters, use `!getconfig`. To output the current value of a parameter, use `!getconfig <paramater name>`.
Basic configuration parameters can be set with `!setconfig <parameter name> <value>`. To get a list of configuration parameters, use `!getconfig`. To output the current value of a parameter, use `!getconfig <paramater name>`. Do not use quotes on these values if they have spaces.

Certain configuration parameters are more complex. They can either be maps, lists, or maps of lists. This type information is listed when using `!getconfig`. Parameters that are lists simply take multiple values instead of one. Setting a list parameter to a set of values will *replace* the current list of values.
Certain configuration parameters are more complex. They can either be maps, lists, or maps of lists. This type information is listed when using `!getconfig`. Parameters that are lists simply take multiple values instead of one. Setting a list parameter to a set of values will *replace* the current list of values. In list parameters, *all values* must use quotes if they have spaces in them.

!setconfig <list parameter> <value 1> <value 2> <value 3> <etc...>
!setconfig bored.commands !drop "!pick cute"

Maps are a set of key-value pairs. Unlike lists, each invocation of `!setconfig` will set just a single key-value pair and won't affect any others. If a key already exists, the value of that key will be overwritten. If the value is set to "", the key will be deleted.
You may pass no values to a list, which will simply set the list to nothing:

!setconfig bored.commands

Maps are a set of key-value pairs. Unlike lists, each invocation of `!setconfig` will set just a single key-value pair and won't affect any others. If a key already exists, the value of that key will be overwritten.

!setconfig <map parameter> <key> <value>
!setconfig basic.aliases listbucket list
!setconfig basic.aliases listbucket ""

Maps of lists simply map their keys to entire lists of values instead of just one value. The syntax is similar to setting a single map value:
If no value is given, the key will be deleted:

!setconfig basic.aliases listbucket

Maps of lists match keys to entire lists of values instead of just one value. The syntax is similar to setting a single map value:

!setconfig <maplist parameter> <key> <value 1> <value 2> <value 3> <etc...>
!setconfig modules.commandchannels roll #channel1 #channel2

However, to delete a value from a maplist, you simply call `!setconfig modules.commandchannels <key>` with no values at all:
To delete a value, simply provide only the key and no values:

!setconfig modules.commandchannels roll

Expand Down
2 changes: 1 addition & 1 deletion filtermodule/FilterModule.go
Expand Up @@ -282,7 +282,7 @@ func (c *deleteFilterCommand) Process(args []string, msg *discordgo.Message, ind
return "```\nNo filter given. All filters: " + strings.Join(getAllFilters(info), ", ") + "```", false, nil
}
if len(args) > 1 {
return "```\nYou specified more than one argument. This command completely removes an entire filter, use !removefilter to remove a single item.```", false, nil
return "```\nYou specified more than one argument. This command completely removes an entire filter, use " + info.Config.Basic.CommandPrefix + "removefilter to remove a single item.```", false, nil
}

filter := args[0]
Expand Down
8 changes: 4 additions & 4 deletions quotemodule/QuoteModule.go
Expand Up @@ -92,7 +92,7 @@ func (c *quoteCommand) Process(args []string, msg *discordgo.Message, indices []
i = rand.Intn(l)
}
if i >= l || i < 0 {
return "```\nInvalid quote index. Use !searchquote [user] to list a user's quotes and their indexes.```", false, nil
return "```\nInvalid quote index. Use " + info.Config.Basic.CommandPrefix + "searchquote [user] to list a user's quotes and their indexes.```", false, nil
}
return "**" + info.GetUserName(user) + "**: " + q[i], false, nil
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func (c *removequoteCommand) Process(args []string, msg *discordgo.Message, indi
return "```\nMust specify username.```", false, nil
}
if len(args) < 2 {
return "```\nMust specify quote index. Use !searchquote to list them.```", false, nil
return "```\nMust specify quote index. Use " + info.Config.Basic.CommandPrefix + "searchquote to list them.```", false, nil
}

last := len(args) - 1
Expand All @@ -172,12 +172,12 @@ func (c *removequoteCommand) Process(args []string, msg *discordgo.Message, indi
}
index, err := strconv.Atoi(args[last])
if err != nil {
return "```\nError: could not parse quote index. Did you surround your username with quotes? Use !searchquote to find a quote index.```", false, nil
return "```\nError: could not parse quote index. Did you surround your username with quotes? Use " + info.Config.Basic.CommandPrefix + "searchquote to find a quote index.```", false, nil
}

index--
if index >= len(info.Config.Quote.Quotes[user]) || index < 0 {
return "```\nInvalid quote index. Use !searchquote [user] to list a user's quotes and their indexes.```", false, nil
return "```\nInvalid quote index. Use " + info.Config.Basic.CommandPrefix + "searchquote [user] to list a user's quotes and their indexes.```", false, nil
}
info.Config.Quote.Quotes[user] = append(info.Config.Quote.Quotes[user][:index], info.Config.Quote.Quotes[user][index+1:]...)
info.SaveConfig()
Expand Down
56 changes: 52 additions & 4 deletions rolesmodule/RolesModule.go
Expand Up @@ -3,13 +3,15 @@ package rolesmodule
import (
"errors"
"fmt"
"regexp"
"strings"

bot "../sweetiebot"
"github.com/blackhole12/discordgo"
)

var errNotUserAssignable = errors.New("not a user-assignable role")
var pingRegex = regexp.MustCompile("<[@#](!|&)?[0-9]+>")

// RolesModule contains commands for manipulating user-assignable roles.
type RolesModule struct {
Expand All @@ -29,6 +31,7 @@ func (w *RolesModule) Name() string {
func (w *RolesModule) Commands() []bot.Command {
return []bot.Command{
&addRoleCommand{},
&createRoleCommand{},
&joinRoleCommand{},
&listRoleCommand{},
&leaveRoleCommand{},
Expand Down Expand Up @@ -68,7 +71,7 @@ func GetRoleByNameOrPing(role string, info *bot.GuildInfo) (*discordgo.Role, err
if err != nil {
return nil, err
}
if r == bot.RoleEmpty {
if r == bot.RoleEmpty || r == bot.RoleExclusion {
return nil, errNotUserAssignable
}
_, ok := info.Config.Users.Roles[r]
Expand All @@ -90,13 +93,58 @@ func GetRoleByNameOrPing(role string, info *bot.GuildInfo) (*discordgo.Role, err
return GetUserAssignableRole(role, info)
}

type createRoleCommand struct {
}

func (c *createRoleCommand) Info() *bot.CommandInfo {
return &bot.CommandInfo{
Name: "CreateRole",
Usage: "Creates a new user-assignable role.",
Sensitive: true,
}
}

func (c *createRoleCommand) Process(args []string, msg *discordgo.Message, indices []int, info *bot.GuildInfo) (string, bool, *discordgo.MessageEmbed) {
if len(args) < 1 {
return "```\nYou must provide a new role name!```", false, nil
}
g, _ := info.GetGuild()
role := msg.Content[indices[0]:]
if pingRegex.MatchString(role) {
return "```\nDon't do that. Give the role a name that isn't a ping.```", false, nil
}
roles := bot.FindRole(role, g)
if len(roles) > 0 {
return "```\nThat role already exists! Use " + info.Config.Basic.CommandPrefix + "addrole to make it user-assignable if it isn't already.```", false, nil
}

r, err := info.Bot.DG.GuildRoleCreate(info.ID)
if err == nil {
r, err = info.Bot.DG.GuildRoleEdit(info.ID, r.ID, role, 0, false, 0, true)
}
if err != nil {
return "```Could not create role! " + err.Error() + "```", false, nil
}
info.Config.Users.Roles[bot.DiscordRole(r.ID)] = true
info.SaveConfig()
return fmt.Sprintf("```Created the %s role. By default, it has no permissions and can be pinged by users, but you can change these settings if you like. Use "+info.Config.Basic.CommandPrefix+"deleterole to delete it.```", r.Name), false, nil
}
func (c *createRoleCommand) Usage(info *bot.GuildInfo) *bot.CommandUsage {
return &bot.CommandUsage{
Desc: "Creates a new role and makes it user-assignable.",
Params: []bot.CommandUsageParam{
{Name: "name/id", Desc: "Name of a new role that doesn't already exist.", Optional: false},
},
}
}

type addRoleCommand struct {
}

func (c *addRoleCommand) Info() *bot.CommandInfo {
return &bot.CommandInfo{
Name: "AddRole",
Usage: "Creates or sets a role as user-assignable.",
Usage: "Sets a role as user-assignable.",
Sensitive: true,
}
}
Expand All @@ -110,8 +158,8 @@ func (c *addRoleCommand) Process(args []string, msg *discordgo.Message, indices
if err != nil {
return bot.ReturnError(err)
}
if role == bot.RoleEmpty {
return "```\nThat's not a role!```", false, nil
if role == bot.RoleEmpty || role == bot.RoleExclusion {
return "```\nThat's not a role! Use " + info.Config.Basic.CommandPrefix + "createroll to create a new role.```", false, nil
}
if info.Config.Basic.ModRole == role {
return "```\nYou can't make the moderator role user-assignable you maniac!```", false, nil
Expand Down
8 changes: 4 additions & 4 deletions schedulermodule/SchedulerModule.go
Expand Up @@ -70,21 +70,21 @@ func (w *SchedulerModule) OnTick(info *bot.GuildInfo, t time.Time) {
modulename := bot.ModuleID(strings.ToLower(w.Name()))
if len(info.Config.Modules.Channels[modulename]) > 0 {
for k := range info.Config.Modules.Channels[modulename] {
if k != bot.ChannelEmpty {
if k != bot.ChannelEmpty && k != bot.ChannelExclusion {
channel = k
break
}
}
} else if len(info.Config.Modules.Channels["bored"]) > 0 {
for k := range info.Config.Modules.Channels["bored"] {
if k != bot.ChannelEmpty {
if k != bot.ChannelEmpty && k != bot.ChannelExclusion {
channel = k
break
}
}
} else if len(info.Config.Basic.FreeChannels) > 0 {
for k := range info.Config.Basic.FreeChannels {
if k != bot.ChannelEmpty {
if k != bot.ChannelEmpty && k != bot.ChannelExclusion {
channel = k
break
}
Expand Down Expand Up @@ -364,7 +364,7 @@ func (c *addEventCommand) Process(args []string, msg *discordgo.Message, indices
return "```\nError: Invalid type specified.```", false, nil
}
if ty == typeEventReminder {
return "```\nError: You cannot add a reminder event this way. Use !remindme instead.```", false, nil
return "```\nError: You cannot add a reminder event this way. Use " + info.Config.Basic.CommandPrefix + "remindme instead.```", false, nil
}
data := ""
if ty == typeEventRole {
Expand Down
2 changes: 1 addition & 1 deletion spammodule/SpamModule.go
Expand Up @@ -570,5 +570,5 @@ func (c *banRaidCommand) Process(args []string, msg *discordgo.Message, indices
return fmt.Sprintf("```\nBanned %v users. The ban log will reflect who ran this command.```", len(users)), false, nil
}
func (c *banRaidCommand) Usage(info *bot.GuildInfo) *bot.CommandUsage {
return &bot.CommandUsage{Desc: "Bans all users that are considered part of the most recent raid, if there was one. Use !getraid to check who will be banned before using this command."}
return &bot.CommandUsage{Desc: "Bans all users that are considered part of the most recent raid, if there was one. Use " + info.Config.Basic.CommandPrefix + "getraid to check who will be banned before using this command."}
}
2 changes: 1 addition & 1 deletion statusmodule/StatusModule.go
Expand Up @@ -143,7 +143,7 @@ func (c *removeStatusCommand) Process(args []string, msg *discordgo.Message, ind
}
func (c *removeStatusCommand) Usage(info *bot.GuildInfo) *bot.CommandUsage {
return &bot.CommandUsage{
Desc: "Removes a string to the discord status rotation. Use !getconfig status.lines to get a list of all strings currently in rotation.",
Desc: "Removes a string to the discord status rotation. Use " + info.Config.Basic.CommandPrefix + "getconfig status.lines to get a list of all strings currently in rotation.",
Params: []bot.CommandUsageParam{
{Name: "arbitrary string", Desc: "Status string that must exactly match the one you want to remove.", Optional: false},
},
Expand Down

0 comments on commit 43b6025

Please sign in to comment.