Skip to content
This repository has been archived by the owner on May 6, 2023. It is now read-only.

Commit

Permalink
Made permissions use regex matching
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlOfDuty committed Aug 8, 2021
1 parent 304181e commit fd691f4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions SCPDiscordBot/ConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
Expand Down Expand Up @@ -103,13 +104,13 @@ public static bool ValidatePermission(CommandContext command)
public static bool HasPermission(DiscordMember member, string permission)
{
// If a specific role is allowed to use the command
if (member.Roles.Any(role => config.permissions.ContainsKey(role.Id) && config.permissions[role.Id].Any(node => permission.StartsWith(node))))
if (member.Roles.Any(role => config.permissions.ContainsKey(role.Id) && config.permissions[role.Id].Any(node => Regex.IsMatch(permission, "^" + node, RegexOptions.IgnoreCase | RegexOptions.Singleline))))
{
return true;
}

// If everyone is allowed to use the command
if (config.permissions.ContainsKey(0) && config.permissions[0].Any(node => permission.StartsWith(node)))
if (config.permissions.ContainsKey(0) && config.permissions[0].Any(node => Regex.IsMatch(permission, node, RegexOptions.IgnoreCase | RegexOptions.Singleline)))
{
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions SCPDiscordBot/default_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ plugin:
# Port to listen on, this has to be the same as the "bot port" in the plugin config and all bots you run have to have different ports or they will clash.
port: 8888

# Set permissions for discord roles here. (Use 0 to allow anyone to use the command)
# The dictionary defines the role ids and the commands they have access to.
# If a role has access to "server aabbcc" all users with that role will be able
# to use the command "+server aabbcc" but also any command starting with it such as "+server aabbccdd"
# Set permissions for discord roles here.
# The permissions are sorted by discord role ids, and the permissions are regex patterns.
# One of the regex patterns associated with one of your roles must match the command you are using for it to be allowed.
# For instance, if you want to allow someone to use the command "test" but not the command "test test" you can use the permission "test$". The "$" signifies the point where the string ends in regex.
permissions:
000000000000000000: # An everyone role example
- list
Expand Down

0 comments on commit fd691f4

Please sign in to comment.