Browse Source

Fixed several command errors

tags/docs-0.9
RogueException 9 years ago
parent
commit
26c603531a
5 changed files with 39 additions and 42 deletions
  1. +1
    -1
      src/Discord.Net.Commands/Command.cs
  2. +3
    -4
      src/Discord.Net.Commands/CommandBuilder.cs
  3. +31
    -33
      src/Discord.Net.Commands/DiscordBotClient.Events.cs
  4. +3
    -3
      src/Discord.Net.Commands/DiscordBotClient.cs
  5. +1
    -1
      src/Discord.Net.Commands/project.json

+ 1
- 1
src/Discord.Net.Commands/Command.cs View File

@@ -10,7 +10,7 @@ namespace Discord.Commands
public int? MaxArgs { get; internal set; }
public int MinPerms { get; internal set; }
internal readonly string[] Parts;
internal Func<DiscordBotClient.CommandEventArgs, Task> Handler;
internal Func<CommandEventArgs, Task> Handler;

internal Command(string text)
{


+ 3
- 4
src/Discord.Net.Commands/CommandBuilder.cs View File

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

public CommandBuilder Do(Func<DiscordBotClient.CommandEventArgs, Task> func)
public CommandBuilder Do(Func<CommandEventArgs, Task> func)
{
_command.Handler = func;
return this;
}
public CommandBuilder Do(Action<DiscordBotClient.CommandEventArgs> func)
public CommandBuilder Do(Action<CommandEventArgs> func)
{
#if DNXCORE50
_command.Handler = e => { func(e); return Task.CompletedTask; };
@@ -82,10 +82,9 @@ namespace Discord.Commands
_defaultMinPermissions = defaultMinPermissions;
}

public CommandGroupBuilder DefaultMinPermissions(int level)
public void DefaultMinPermissions(int level)
{
_defaultMinPermissions = level;
return this;
}

public CommandGroupBuilder CreateCommandGroup(string cmd, Action<CommandGroupBuilder> config = null)


+ 31
- 33
src/Discord.Net.Commands/DiscordBotClient.Events.cs View File

@@ -3,45 +3,43 @@ using System;

namespace Discord
{
public partial class DiscordBotClient : DiscordClient
public class PermissionException : Exception { public PermissionException() : base("User does not have permission to run this command.") { } }
public class CommandEventArgs
{
public class PermissionException : Exception { public PermissionException() : base("User does not have permission to run this command.") { } }
public Message Message { get; }
public Command Command { get; }
public string CommandText { get; }
public int? Permissions { get; }
public string[] Args { get; }

public class CommandEventArgs
{
public Message Message { get; }
public Command Command { get; }
public string CommandText { get; }
public int? Permissions { get; }
public string[] Args { get; }

public User User => Message.User;
public string UserId => Message.UserId;
public Channel Channel => Message.Channel;
public string ChannelId => Message.ChannelId;
public Server Server => Message.Channel.Server;
public string ServerId => Message.Channel.ServerId;
public User User => Message.User;
public string UserId => Message.UserId;
public Channel Channel => Message.Channel;
public string ChannelId => Message.ChannelId;
public Server Server => Message.Channel.Server;
public string ServerId => Message.Channel.ServerId;

public CommandEventArgs(Message message, Command command, string commandText, int? permissions, string[] args)
{
Message = message;
Command = command;
CommandText = commandText;
Permissions = permissions;
Args = args;
}
}
public class CommandErrorEventArgs : CommandEventArgs
public CommandEventArgs(Message message, Command command, string commandText, int? permissions, string[] args)
{
public Exception Exception { get; }

public CommandErrorEventArgs(CommandEventArgs baseArgs, Exception ex)
: base(baseArgs.Message, baseArgs.Command, baseArgs.CommandText, baseArgs.Permissions, baseArgs.Args)
{
Exception = ex;
}
Message = message;
Command = command;
CommandText = commandText;
Permissions = permissions;
Args = args;
}
}
public class CommandErrorEventArgs : CommandEventArgs
{
public Exception Exception { get; }

public CommandErrorEventArgs(CommandEventArgs baseArgs, Exception ex)
: base(baseArgs.Message, baseArgs.Command, baseArgs.CommandText, baseArgs.Permissions, baseArgs.Args)
{
Exception = ex;
}
}
public partial class DiscordBotClient : DiscordClient
{
public event EventHandler<CommandEventArgs> RanCommand;
private void RaiseRanCommand(CommandEventArgs args)
{


+ 3
- 3
src/Discord.Net.Commands/DiscordBotClient.cs View File

@@ -16,7 +16,7 @@ namespace Discord
public bool RequireCommandCharInPublic { get; set; }
public bool RequireCommandCharInPrivate { get; set; }

public DiscordBotClient(DiscordClientConfig config = null, Func<User, int> getPermissions = null)
public DiscordBotClient(DiscordClientConfig config = null, Func<User, Server, int> getPermissions = null)
: base(config)
{
_commands = new List<Command>();
@@ -56,7 +56,7 @@ namespace Discord
Command cmd = _commands[i];

//Check Command Parts
if (args.Length < cmd.Text.Length)
if (args.Length < cmd.Parts.Length)
continue;

bool isValid = true;
@@ -82,7 +82,7 @@ namespace Discord
newArgs[j] = args[j + cmd.Parts.Length];
//Check Permissions
int permissions = getPermissions != null ? getPermissions(e.Message.User) : 0;
int permissions = getPermissions != null ? getPermissions(e.Message.User, e.Message.Channel?.Server) : 0;
var eventArgs = new CommandEventArgs(e.Message, cmd, msg, permissions, newArgs);
if (permissions < cmd.MinPerms)
{


+ 1
- 1
src/Discord.Net.Commands/project.json View File

@@ -1,5 +1,5 @@
{
"version": "0.6.0-beta1",
"version": "0.6.0-beta2",
"description": "A small Discord.Net extension to make bot creation easier.",
"authors": [ "RogueException" ],
"tags": [ "discord", "discordapp" ],


Loading…
Cancel
Save