Cleaned up a few parts, combined all errors into one event.

This commit is contained in:
RogueException
2015-10-29 20:29:51 -03:00
parent 89eb8f168b
commit ecc6ee648e
2 changed files with 55 additions and 85 deletions

View File

@@ -2,43 +2,40 @@
namespace Discord.Commands namespace Discord.Commands
{ {
public class PermissionException : Exception { public PermissionException() : base("User does not have permission to run this command.") { } }
public class ArgumentException : Exception { public ArgumentException() : base("This command requires more arguments.") { } }
public class CommandEventArgs public class CommandEventArgs
{ {
public Message Message { get; } public Message Message { get; }
public Command Command { get; } public Command Command { get; }
public string MessageText { get; } public int? UserPermissions { get; }
public string CommandText { get; }
public string ArgText { get; }
public int? Permissions { get; }
public string[] Args { get; } public string[] Args { get; }
public User User => Message.User; public User User => Message.User;
public Channel Channel => Message.Channel; public Channel Channel => Message.Channel;
public Server Server => Message.Channel.Server; public Server Server => Message.Channel.Server;
public CommandEventArgs(Message message, Command command, string messageText, string commandText, string argText, int? permissions, string[] args) public CommandEventArgs(Message message, Command command, int? userPermissions, string[] args)
{ {
Message = message; Message = message;
Command = command; Command = command;
MessageText = messageText; UserPermissions = userPermissions;
CommandText = commandText;
ArgText = argText;
Permissions = permissions;
Args = args; Args = args;
} }
} }
public enum CommandErrorType { Exception, UnknownCommand, BadPermissions, BadArgCount }
public class CommandErrorEventArgs : CommandEventArgs public class CommandErrorEventArgs : CommandEventArgs
{ {
public CommandErrorType ErrorType { get; }
public Exception Exception { get; } public Exception Exception { get; }
public CommandErrorEventArgs(CommandEventArgs baseArgs, Exception ex) public CommandErrorEventArgs(CommandErrorType errorType, CommandEventArgs baseArgs, Exception ex)
: base(baseArgs.Message, baseArgs.Command, baseArgs.MessageText, baseArgs.CommandText, baseArgs.ArgText, baseArgs.Permissions, baseArgs.Args) : base(baseArgs.Message, baseArgs.Command, baseArgs.UserPermissions, baseArgs.Args)
{ {
Exception = ex; Exception = ex;
} ErrorType = errorType;
}
} }
public partial class CommandsPlugin public partial class CommandsPlugin
{ {
public event EventHandler<CommandEventArgs> RanCommand; public event EventHandler<CommandEventArgs> RanCommand;
@@ -47,17 +44,11 @@ namespace Discord.Commands
if (RanCommand != null) if (RanCommand != null)
RanCommand(this, args); RanCommand(this, args);
} }
public event EventHandler<CommandEventArgs> UnknownCommand;
private void RaiseUnknownCommand(CommandEventArgs args)
{
if (UnknownCommand != null)
UnknownCommand(this, args);
}
public event EventHandler<CommandErrorEventArgs> CommandError; public event EventHandler<CommandErrorEventArgs> CommandError;
private void RaiseCommandError(CommandEventArgs args, Exception ex) private void RaiseCommandError(CommandErrorType errorType, CommandEventArgs args, Exception ex = null)
{ {
if (CommandError != null) if (CommandError != null)
CommandError(this, new CommandErrorEventArgs(args, ex)); CommandError(this, new CommandErrorEventArgs(errorType, args, ex));
} }
} }
} }

View File

@@ -46,9 +46,7 @@ namespace Discord.Commands
.Do(async e => .Do(async e =>
{ {
if (e.Command.Text != "help") if (e.Command.Text != "help")
{
await Reply(e, CommandDetails(e.Command)); await Reply(e, CommandDetails(e.Command));
}
else else
{ {
if (e.Args == null) if (e.Args == null)
@@ -95,16 +93,11 @@ namespace Discord.Commands
client.MessageReceived += async (s, e) => client.MessageReceived += async (s, e) =>
{ {
// This will need to be changed once a built in help command is made // This will need to be changed once a built in help command is made
if (_commands.Count == 0) if (_commands.Count == 0) return;
return; if (e.Message.IsAuthor) return;
if (e.Message.IsAuthor)
return;
string msg = e.Message.Text; string msg = e.Message.Text;
if (msg.Length == 0) return;
if (msg.Length == 0)
return;
if (UseCommandChar) if (UseCommandChar)
{ {
@@ -127,17 +120,10 @@ namespace Discord.Commands
if (_commands.ContainsKey(cmd)) if (_commands.ContainsKey(cmd))
{ {
Command comm = _commands[cmd]; Command comm = _commands[cmd];
//Get ArgText //Clean args
int argCount = args.Length; int argCount = args.Length;
string argText; string[] newArgs = null;
if (argCount == 0)
argText = "";
else
argText = msg.Substring(args[0].Index);
//Clean Args
string[] newArgs = null;
if (comm.MaxArgs != null && argCount > 0) if (comm.MaxArgs != null && argCount > 0)
{ {
@@ -152,74 +138,67 @@ namespace Discord.Commands
newArgs[j] = args[j].Value; newArgs[j] = args[j].Value;
} }
// Check permissions here int userPermissions = _getPermissions != null ? _getPermissions(e.Message.User) : 0;
int permissions = _getPermissions != null ? _getPermissions(e.Message.User) : 0; var eventArgs = new CommandEventArgs(e.Message, comm, userPermissions, newArgs);
var eventArgs = new CommandEventArgs(e.Message, comm, msg, cmd, argText, permissions, newArgs);
if (permissions < comm.MinPerms)
{
RaiseCommandError(eventArgs, new PermissionException());
return;
}
//Check Arg Count
if (argCount < comm.MinArgs)
{
RaiseCommandError(eventArgs, new ArgumentException());
if (builtInHelp)
await _commands["help"].Handler(eventArgs);
return;
}
// Actually run the command here // Check permissions
RaiseRanCommand(eventArgs); if (userPermissions < comm.MinPerms)
try {
{ RaiseCommandError(CommandErrorType.BadPermissions, eventArgs);
return;
}
//Check arg count
if (argCount < comm.MinArgs)
{
RaiseCommandError(CommandErrorType.BadArgCount, eventArgs);
return;
}
// Run the command
try
{
RaiseRanCommand(eventArgs);
var task = comm.Handler(eventArgs); var task = comm.Handler(eventArgs);
if (task != null) if (task != null)
await task.ConfigureAwait(false); await task.ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)
{ {
RaiseCommandError(eventArgs, ex); RaiseCommandError(CommandErrorType.Exception, eventArgs, ex);
} }
} }
else else
{ {
CommandEventArgs eventArgs = new CommandEventArgs(e.Message, null, msg, cmd, null, null, null); CommandEventArgs eventArgs = new CommandEventArgs(e.Message, null, null, null);
RaiseUnknownCommand(eventArgs); RaiseCommandError(CommandErrorType.UnknownCommand, eventArgs);
if (builtInHelp)
await Reply(eventArgs, $"The command `{cmd}` does not exist.");
return; return;
} }
}; };
} }
internal string CommandDetails(Command comm) private string CommandDetails(Command command)
{ {
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
output.Append($"`{comm.Text}`"); output.Append($"`{command.Text}`");
if (comm.MinArgs != null && comm.MaxArgs != null) if (command.MinArgs != null && command.MaxArgs != null)
{ {
if (comm.MinArgs == comm.MaxArgs) if (command.MinArgs == command.MaxArgs)
{ {
if (comm.MaxArgs != 0) if (command.MaxArgs != 0)
output.Append($" {comm.MinArgs.ToString()} Args"); output.Append($" {command.MinArgs.ToString()} Args");
} }
else else
output.Append($" {comm.MinArgs.ToString()} - {comm.MaxArgs.ToString()} Args"); output.Append($" {command.MinArgs.ToString()} - {command.MaxArgs.ToString()} Args");
}
else if (comm.MinArgs != null && comm.MaxArgs == null)
{
output.Append($" ≥{comm.MinArgs.ToString()} Args");
}
else if (comm.MinArgs == null && comm.MaxArgs != null)
{
output.Append($" ≤{comm.MaxArgs.ToString()} Args");
} }
else if (command.MinArgs != null && command.MaxArgs == null)
output.Append($" ≥{command.MinArgs.ToString()} Args");
else if (command.MinArgs == null && command.MaxArgs != null)
output.Append($" ≤{command.MaxArgs.ToString()} Args");
output.Append($": {comm.Description}"); output.Append($": {command.Description}");
return output.ToString(); return output.ToString();
} }