Browse Source

Added ArgText to commands

tags/docs-0.9
RogueException 9 years ago
parent
commit
edffc914a1
3 changed files with 36 additions and 15 deletions
  1. +19
    -7
      src/Discord.Net.Commands/CommandParser.cs
  2. +4
    -2
      src/Discord.Net.Commands/CommandsPlugin.Events.cs
  3. +13
    -6
      src/Discord.Net.Commands/CommandsPlugin.cs

+ 19
- 7
src/Discord.Net.Commands/CommandParser.cs View File

@@ -2,6 +2,18 @@


namespace Discord.Commands namespace Discord.Commands
{ {
public class CommandPart
{
public string Value { get; }
public int Index { get; }

internal CommandPart(string value, int index)
{
Value = value;
Index = index;
}
}

//TODO: Check support for escaping //TODO: Check support for escaping
public static class CommandParser public static class CommandParser
{ {
@@ -14,24 +26,24 @@ namespace Discord.Commands
DoubleQuotedParameter DoubleQuotedParameter
} }


public static bool Parse(string input, out string command, out string[] args)
public static bool Parse(string input, out string command, out CommandPart[] args)
{ {
return Parse(input, out command, out args, true); return Parse(input, out command, out args, true);
} }
public static bool ParseArgs(string input, out string[] args)
public static bool ParseArgs(string input, out CommandPart[] args)
{ {
string ignored; string ignored;
return Parse(input, out ignored, out args, false); return Parse(input, out ignored, out args, false);
} }


private static bool Parse(string input, out string command, out string[] args, bool parseCommand)
private static bool Parse(string input, out string command, out CommandPart[] args, bool parseCommand)
{ {
CommandParserPart currentPart = parseCommand ? CommandParserPart.CommandName : CommandParserPart.None; CommandParserPart currentPart = parseCommand ? CommandParserPart.CommandName : CommandParserPart.None;
int startPosition = 0; int startPosition = 0;
int endPosition = 0; int endPosition = 0;
int inputLength = input.Length; int inputLength = input.Length;
bool isEscaped = false; bool isEscaped = false;
List<string> argList = new List<string>();
List<CommandPart> argList = new List<CommandPart>();


command = null; command = null;
args = null; args = null;
@@ -84,7 +96,7 @@ namespace Discord.Commands
else else
{ {
currentPart = CommandParserPart.None; currentPart = CommandParserPart.None;
argList.Add(temp);
argList.Add(new CommandPart(temp, startPosition));
startPosition = endPosition; startPosition = endPosition;
} }
} }
@@ -94,7 +106,7 @@ namespace Discord.Commands
{ {
string temp = input.Substring(startPosition, endPosition - startPosition - 1); string temp = input.Substring(startPosition, endPosition - startPosition - 1);
currentPart = CommandParserPart.None; currentPart = CommandParserPart.None;
argList.Add(temp);
argList.Add(new CommandPart(temp, startPosition));
startPosition = endPosition; startPosition = endPosition;
} }
else if (endPosition >= inputLength) else if (endPosition >= inputLength)
@@ -105,7 +117,7 @@ namespace Discord.Commands
{ {
string temp = input.Substring(startPosition, endPosition - startPosition - 1); string temp = input.Substring(startPosition, endPosition - startPosition - 1);
currentPart = CommandParserPart.None; currentPart = CommandParserPart.None;
argList.Add(temp);
argList.Add(new CommandPart(temp, startPosition));
startPosition = endPosition; startPosition = endPosition;
} }
else if (endPosition >= inputLength) else if (endPosition >= inputLength)


+ 4
- 2
src/Discord.Net.Commands/CommandsPlugin.Events.cs View File

@@ -8,6 +8,7 @@ namespace Discord.Commands
public Message Message { get; } public Message Message { get; }
public Command Command { get; } public Command Command { get; }
public string CommandText { get; } public string CommandText { get; }
public string ArgText { get; }
public int? Permissions { get; } public int? Permissions { get; }
public string[] Args { get; } public string[] Args { get; }


@@ -19,11 +20,12 @@ namespace Discord.Commands
public Server Server => Message.Channel.Server; public Server Server => Message.Channel.Server;
public string ServerId => Message.Channel.ServerId; public string ServerId => Message.Channel.ServerId;


public CommandEventArgs(Message message, Command command, string commandText, int? permissions, string[] args)
public CommandEventArgs(Message message, Command command, string commandText, string argText, int? permissions, string[] args)
{ {
Message = message; Message = message;
Command = command; Command = command;
CommandText = commandText; CommandText = commandText;
ArgText = argText;
Permissions = permissions; Permissions = permissions;
Args = args; Args = args;
} }
@@ -33,7 +35,7 @@ namespace Discord.Commands
public Exception Exception { get; } public Exception Exception { get; }


public CommandErrorEventArgs(CommandEventArgs baseArgs, Exception ex) public CommandErrorEventArgs(CommandEventArgs baseArgs, Exception ex)
: base(baseArgs.Message, baseArgs.Command, baseArgs.CommandText, baseArgs.Permissions, baseArgs.Args)
: base(baseArgs.Message, baseArgs.Command, baseArgs.CommandText, baseArgs.ArgText, baseArgs.Permissions, baseArgs.Args)
{ {
Exception = ex; Exception = ex;
} }


+ 13
- 6
src/Discord.Net.Commands/CommandsPlugin.cs View File

@@ -54,7 +54,7 @@ namespace Discord.Commands
return; return;
} }


string[] args;
CommandPart[] args;
if (!CommandParser.ParseArgs(msg, out args)) if (!CommandParser.ParseArgs(msg, out args))
return; return;


@@ -69,7 +69,7 @@ namespace Discord.Commands
bool isValid = true; bool isValid = true;
for (int j = 0; j < cmd.Parts.Length; j++) for (int j = 0; j < cmd.Parts.Length; j++)
{ {
if (!string.Equals(args[j], cmd.Parts[j], StringComparison.OrdinalIgnoreCase))
if (!string.Equals(args[j].Value, cmd.Parts[j], StringComparison.OrdinalIgnoreCase))
{ {
isValid = false; isValid = false;
break; break;
@@ -86,11 +86,18 @@ namespace Discord.Commands
//Clean Args //Clean Args
string[] newArgs = new string[argCount]; string[] newArgs = new string[argCount];
for (int j = 0; j < newArgs.Length; j++) for (int j = 0; j < newArgs.Length; j++)
newArgs[j] = args[j + cmd.Parts.Length];
newArgs[j] = args[j + cmd.Parts.Length].Value;

//Get ArgText
string argText;
if (argCount == 0)
argText = "";
else
argText = msg.Substring(args[cmd.Parts.Length].Index);

//Check Permissions //Check Permissions
int permissions = _getPermissions != null ? _getPermissions(e.Message.User, e.Message.Channel?.Server) : 0;
var eventArgs = new CommandEventArgs(e.Message, cmd, msg, permissions, newArgs);
int permissions = _getPermissions != null ? _getPermissions(e.Message.User, e.Message.Channel?.Server) : 0;
var eventArgs = new CommandEventArgs(e.Message, cmd, msg, argText, permissions, newArgs);
if (permissions < cmd.MinPerms) if (permissions < cmd.MinPerms)
{ {
RaiseCommandError(eventArgs, new PermissionException()); RaiseCommandError(eventArgs, new PermissionException());


Loading…
Cancel
Save