Browse Source

Custom activation methods

tags/docs-0.9
Googie2149 9 years ago
parent
commit
00171c68ac
2 changed files with 58 additions and 8 deletions
  1. +41
    -8
      src/Discord.Net.Commands/CommandService.cs
  2. +17
    -0
      src/Discord.Net.Commands/CommandServiceConfig.cs

+ 41
- 8
src/Discord.Net.Commands/CommandService.cs View File

@@ -74,17 +74,50 @@ namespace Discord.Commands
string msg = e.Message.RawText; string msg = e.Message.RawText;
if (msg.Length == 0) return; if (msg.Length == 0) return;


//Check for command char if one is provided
var chars = Config.CommandChars;
if (chars.Length > 0)
bool activated = false; // Either this or nested if statements
// Also needs a clearer name

if ((Config.ActivationMode & ActivationMode.Char) != 0)
{ {
if (!chars.Contains(msg[0]))
return;
msg = msg.Substring(1);
//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 (!activated && (Config.ActivationMode & ActivationMode.Mention) != 0)
{
if (e.Message.IsMentioningMe() && msg.StartsWith(e.Server.CurrentUser.Mention))
{
msg = msg.Substring(e.Server.CurrentUser.Mention.Length);
activated = true;
}
}

// 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)
{
int index = Config.CustomActivator(e.Message);
if (index > 0)
{
msg = msg.Substring(index);
activated = true;
}
} }


//Parse command
IEnumerable<Command> commands;
// This kills trying to parse messages when you don't have a command char set or anything else
if (!activated)
return;

//Parse command
IEnumerable<Command> commands;
int argPos; int argPos;
CommandParser.ParseCommand(msg, _map, out commands, out argPos); CommandParser.ParseCommand(msg, _map, out commands, out argPos);
if (commands == null) if (commands == null)


+ 17
- 0
src/Discord.Net.Commands/CommandServiceConfig.cs View File

@@ -11,6 +11,17 @@ namespace Discord.Commands
/// <summary> Use the automatic help command and respond in a private message. </summary> /// <summary> Use the automatic help command and respond in a private message. </summary>
Private 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 public char? CommandChar
@@ -29,10 +40,16 @@ namespace Discord.Commands
} }
public char[] CommandChars { get { return _commandChars; } set { SetValue(ref _commandChars, value); } } public char[] CommandChars { get { return _commandChars; } set { SetValue(ref _commandChars, value); } }
private char[] _commandChars = new char[] { '!' }; private char[] _commandChars = new char[] { '!' };
public Func<Message, int> CustomActivator { get { return _customActivator; } set { SetValue(ref _customActivator, value); } }
private Func<Message, int> _customActivator = null;


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 //Lock
protected bool _isLocked; protected bool _isLocked;
internal void Lock() { _isLocked = true; } internal void Lock() { _isLocked = true; }


Loading…
Cancel
Save