@@ -39,6 +39,9 @@ | |||||
<Reference Include="System" /> | <Reference Include="System" /> | ||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<Compile Include="..\Discord.Net.Commands\ActivationMode.cs"> | |||||
<Link>ActivationMode.cs</Link> | |||||
</Compile> | |||||
<Compile Include="..\Discord.Net.Commands\Command.cs"> | <Compile Include="..\Discord.Net.Commands\Command.cs"> | ||||
<Link>Command.cs</Link> | <Link>Command.cs</Link> | ||||
</Compile> | </Compile> | ||||
@@ -69,6 +72,9 @@ | |||||
<Compile Include="..\Discord.Net.Commands\CommandServiceConfig.cs"> | <Compile Include="..\Discord.Net.Commands\CommandServiceConfig.cs"> | ||||
<Link>CommandServiceConfig.cs</Link> | <Link>CommandServiceConfig.cs</Link> | ||||
</Compile> | </Compile> | ||||
<Compile Include="..\Discord.Net.Commands\HelpMode.cs"> | |||||
<Link>HelpMode.cs</Link> | |||||
</Compile> | |||||
<Compile Include="..\Discord.Net.Commands\Permissions\GenericPermissionChecker.cs"> | <Compile Include="..\Discord.Net.Commands\Permissions\GenericPermissionChecker.cs"> | ||||
<Link>Permissions\GenericPermissionChecker.cs</Link> | <Link>Permissions\GenericPermissionChecker.cs</Link> | ||||
</Compile> | </Compile> | ||||
@@ -74,53 +74,33 @@ namespace Discord.Commands | |||||
string msg = e.Message.RawText; | string msg = e.Message.RawText; | ||||
if (msg.Length == 0) return; | 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 | //Parse command | ||||
IEnumerable<Command> commands; | IEnumerable<Command> commands; | ||||
@@ -225,18 +205,9 @@ namespace Discord.Commands | |||||
else | else | ||||
{ | { | ||||
output.Append("\n\n"); | 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 <command>` can tell you more about how to use a command."); | |||||
} | |||||
else | |||||
output.AppendLine($"`help <command>` 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 <command>` can tell you more about how to use a command."); | |||||
} | } | ||||
return (replyChannel ?? channel).SendMessage(output.ToString()); | return (replyChannel ?? channel).SendMessage(output.ToString()); | ||||
@@ -322,10 +293,10 @@ namespace Discord.Commands | |||||
output.Append($" [{param.Name}]"); | output.Append($" [{param.Name}]"); | ||||
break; | break; | ||||
case ParameterType.Multiple: | case ParameterType.Multiple: | ||||
output.Append($" [{param.Name}]"); | |||||
output.Append($" [{param.Name}...]"); | |||||
break; | break; | ||||
case ParameterType.Unparsed: | case ParameterType.Unparsed: | ||||
output.Append($" {param.Name}"); | |||||
output.Append($" [-]"); | |||||
break; | break; | ||||
} | } | ||||
} | } | ||||
@@ -2,56 +2,30 @@ | |||||
namespace Discord.Commands | namespace Discord.Commands | ||||
{ | { | ||||
public enum HelpMode | |||||
{ | |||||
/// <summary> Disable the automatic help command. </summary> | |||||
Disable, | |||||
/// <summary> Use the automatic help command and respond in the channel the command is used. </summary> | |||||
Public, | |||||
/// <summary> Use the automatic help command and respond in a private message. </summary> | |||||
Private | |||||
} | |||||
[Flags] | |||||
public enum ActivationMode | |||||
{ | |||||
// All of these probably need to be changed | |||||
/// <summary> Enable command activation by char. </summary> | |||||
Char = 0x1, | |||||
/// <summary> Enable command activation when mentioned. </summary> | |||||
Mention = 0x2, | |||||
/// <summary> Enable command activation by custom function. </summary> | |||||
Custom = 0x4 | |||||
} | |||||
public class CommandServiceConfig | 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<Message, int> CustomActivator { get { return _customActivator; } set { SetValue(ref _customActivator, value); } } | |||||
private Func<Message, int> _customActivator = null; | |||||
{ | |||||
/// <summary> Gets or sets the prefix character used to trigger commands, if ActivationMode has the Char flag set. </summary> | |||||
public char? PrefixChar { get { return _prefixChar; } set { SetValue(ref _prefixChar, value); } } | |||||
private char? _prefixChar = null; | |||||
/// <summary> Gets or sets whether a message beginning with a mention to the logged-in user should be treated as a command. </summary> | |||||
public bool AllowMentionPrefix { get { return _allowMentionPrefix; } set { SetValue(ref _allowMentionPrefix, value); } } | |||||
private bool _allowMentionPrefix = true; | |||||
/// <summary> | |||||
/// 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. | |||||
/// </summary> | |||||
public Func<Message, int> CustomPrefixHandler { get { return _customPrefixHandler; } set { SetValue(ref _customPrefixHandler, value); } } | |||||
private Func<Message, int> _customPrefixHandler = null; | |||||
/// <summary> Gets or sets whether a help function should be automatically generated. </summary> | |||||
public HelpMode HelpMode { get { return _helpMode; } set { SetValue(ref _helpMode, value); } } | public HelpMode HelpMode { get { return _helpMode; } set { SetValue(ref _helpMode, value); } } | ||||
private HelpMode _helpMode = HelpMode.Disable; | 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; } | internal void Lock() { _isLocked = true; } | ||||
protected void SetValue<T>(ref T storage, T value) | protected void SetValue<T>(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."); | throw new InvalidOperationException("Unable to modify a service's configuration after it has been created."); | ||||
storage = value; | storage = value; | ||||
} | } | ||||
} | |||||
} | |||||
} | } |
@@ -0,0 +1,12 @@ | |||||
namespace Discord.Commands | |||||
{ | |||||
public enum HelpMode | |||||
{ | |||||
/// <summary> Disable the automatic help command. </summary> | |||||
Disable, | |||||
/// <summary> Use the automatic help command and respond in the channel the command is used. </summary> | |||||
Public, | |||||
/// <summary> Use the automatic help command and respond in a private message. </summary> | |||||
Private | |||||
} | |||||
} |