From 2849acb005236fbc28f3e9c28b159021d5f3b44c Mon Sep 17 00:00:00 2001 From: RogueException Date: Tue, 9 Feb 2016 15:07:59 -0400 Subject: [PATCH] Cleaned up new CommandService activations and help args. --- .../Discord.Net.Commands.csproj | 6 ++ src/Discord.Net.Commands/CommandService.cs | 77 ++++++------------- .../CommandServiceConfig.cs | 66 +++++----------- src/Discord.Net.Commands/HelpMode.cs | 12 +++ 4 files changed, 62 insertions(+), 99 deletions(-) create mode 100644 src/Discord.Net.Commands/HelpMode.cs diff --git a/src/Discord.Net.Commands.Net45/Discord.Net.Commands.csproj b/src/Discord.Net.Commands.Net45/Discord.Net.Commands.csproj index 64c8edf7e..1b5c91262 100644 --- a/src/Discord.Net.Commands.Net45/Discord.Net.Commands.csproj +++ b/src/Discord.Net.Commands.Net45/Discord.Net.Commands.csproj @@ -39,6 +39,9 @@ + + ActivationMode.cs + Command.cs @@ -69,6 +72,9 @@ CommandServiceConfig.cs + + HelpMode.cs + Permissions\GenericPermissionChecker.cs diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index 8263d2339..cc1137e09 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -74,53 +74,33 @@ namespace Discord.Commands string msg = e.Message.RawText; if (msg.Length == 0) return; - bool activated = false; // Either this or nested if statements - // Also needs a clearer name + string cmdMsg = null; - if ((Config.ActivationMode & ActivationMode.Char) != 0) + //Check for command char + if (Config.PrefixChar.HasValue) { - //Check for command char if one is provided - var chars = Config.CommandChars; - if (chars.Length > 0) - { - if (chars.Contains(msg[0])) - { - msg = msg.Substring(1); - activated = true; - } - } + if (msg[0] == Config.PrefixChar.Value) + cmdMsg = msg.Substring(1); } - if (!activated && (Config.ActivationMode & ActivationMode.Mention) != 0) + //Check for mention + if (cmdMsg == null && Config.AllowMentionPrefix) { - if (e.Message.IsMentioningMe() && msg.StartsWith(e.Server.CurrentUser.Mention)) - { - msg = msg.Substring(e.Server.CurrentUser.Mention.Length); - activated = true; - } - else if (e.Channel.IsPrivate && msg.StartsWith($"@{client.CurrentUser.Name}")) - { - msg = msg.Substring(client.CurrentUser.Name.Length); - activated = true; - } + if (msg.StartsWith(e.Server.CurrentUser.Mention)) + cmdMsg = msg.Substring(e.Server.CurrentUser.Mention.Length); + else if (msg.StartsWith($"@{client.CurrentUser.Name}")) + cmdMsg = msg.Substring(client.CurrentUser.Name.Length); } - - // Checking if that's null with the Custom flag set on launch and throwing an - // exception (similar to commands with several unparsed parameters) would probably be better than the null check here - if (!activated && (Config.ActivationMode & ActivationMode.Custom) != 0 && Config.CustomActivator != null) + + //Check using custom activator + if (cmdMsg == null && Config.CustomPrefixHandler != null) { - int index = Config.CustomActivator(e.Message); - if (index > 0) - { - msg = msg.Substring(index); - activated = true; - } + int index = Config.CustomPrefixHandler(e.Message); + if (index >= 0) + cmdMsg = msg.Substring(index); } - - // This kills trying to parse messages when you don't have a command char set, - // but also keeps it from trying to parse everything when you *do* have something set - if (!activated) - return; + + if (cmdMsg == null) return; //Parse command IEnumerable commands; @@ -225,18 +205,9 @@ namespace Discord.Commands else { output.Append("\n\n"); - - var chars = Config.CommandChars; - if (chars.Length > 0) - { - if (chars.Length == 1) - output.AppendLine($"You can use `{chars[0]}` to call a command."); - else - output.AppendLine($"You can use `{string.Join(" ", chars.Take(chars.Length - 1))}` or `{chars.Last()}` to call a command."); - output.AppendLine($"`{chars[0]}help ` can tell you more about how to use a command."); - } - else - output.AppendLine($"`help ` can tell you more about how to use a command."); + + output.AppendLine($"You can use `{Config.PrefixChar}` to call a command."); + output.AppendLine($"`{Config.PrefixChar}help ` can tell you more about how to use a command."); } return (replyChannel ?? channel).SendMessage(output.ToString()); @@ -322,10 +293,10 @@ namespace Discord.Commands output.Append($" [{param.Name}]"); break; case ParameterType.Multiple: - output.Append($" [{param.Name}]"); + output.Append($" [{param.Name}...]"); break; case ParameterType.Unparsed: - output.Append($" {param.Name}"); + output.Append($" [-]"); break; } } diff --git a/src/Discord.Net.Commands/CommandServiceConfig.cs b/src/Discord.Net.Commands/CommandServiceConfig.cs index d8ba31fc2..982ce2cd9 100644 --- a/src/Discord.Net.Commands/CommandServiceConfig.cs +++ b/src/Discord.Net.Commands/CommandServiceConfig.cs @@ -2,56 +2,30 @@ namespace Discord.Commands { - public enum HelpMode - { - /// Disable the automatic help command. - Disable, - /// Use the automatic help command and respond in the channel the command is used. - Public, - /// Use the automatic help command and respond in a private message. - Private - } - [Flags] - public enum ActivationMode - { - // All of these probably need to be changed - /// Enable command activation by char. - Char = 0x1, - /// Enable command activation when mentioned. - Mention = 0x2, - /// Enable command activation by custom function. - Custom = 0x4 - } public class CommandServiceConfig - { - public char? CommandChar - { - get - { - return _commandChars.Length > 0 ? _commandChars[0] : (char?)null; - } - set - { - if (value != null) - CommandChars = new char[] { value.Value }; - else - CommandChars = new char[0]; - } - } - public char[] CommandChars { get { return _commandChars; } set { SetValue(ref _commandChars, value); } } - private char[] _commandChars = new char[] { '!' }; - - public Func CustomActivator { get { return _customActivator; } set { SetValue(ref _customActivator, value); } } - private Func _customActivator = null; + { + /// Gets or sets the prefix character used to trigger commands, if ActivationMode has the Char flag set. + public char? PrefixChar { get { return _prefixChar; } set { SetValue(ref _prefixChar, value); } } + private char? _prefixChar = null; + /// Gets or sets whether a message beginning with a mention to the logged-in user should be treated as a command. + public bool AllowMentionPrefix { get { return _allowMentionPrefix; } set { SetValue(ref _allowMentionPrefix, value); } } + private bool _allowMentionPrefix = true; + + /// + /// Gets or sets a custom function used to detect messages that should be treated as commands. + /// This function should a positive one indicating the index of where the in the message's RawText the command begins, + /// and a negative value if the message should be ignored. + /// + public Func CustomPrefixHandler { get { return _customPrefixHandler; } set { SetValue(ref _customPrefixHandler, value); } } + private Func _customPrefixHandler = null; + + /// Gets or sets whether a help function should be automatically generated. public HelpMode HelpMode { get { return _helpMode; } set { SetValue(ref _helpMode, value); } } private HelpMode _helpMode = HelpMode.Disable; - public ActivationMode ActivationMode { get { return _activationMode; } set { SetValue(ref _activationMode, value); } } - private ActivationMode _activationMode = ActivationMode.Char; // Set char as default, not sure if it's the best method of doing it - - //Lock - protected bool _isLocked; + //Lock + protected bool _isLocked; internal void Lock() { _isLocked = true; } protected void SetValue(ref T storage, T value) { @@ -59,5 +33,5 @@ namespace Discord.Commands throw new InvalidOperationException("Unable to modify a service's configuration after it has been created."); storage = value; } - } + } } diff --git a/src/Discord.Net.Commands/HelpMode.cs b/src/Discord.Net.Commands/HelpMode.cs new file mode 100644 index 000000000..27f5c444f --- /dev/null +++ b/src/Discord.Net.Commands/HelpMode.cs @@ -0,0 +1,12 @@ +namespace Discord.Commands +{ + public enum HelpMode + { + /// Disable the automatic help command. + Disable, + /// Use the automatic help command and respond in the channel the command is used. + Public, + /// Use the automatic help command and respond in a private message. + Private + } +}