@@ -10,7 +10,7 @@ namespace Discord.Commands | |||||
public int? MaxArgs { get; internal set; } | public int? MaxArgs { get; internal set; } | ||||
public int MinPerms { get; internal set; } | public int MinPerms { get; internal set; } | ||||
internal readonly string[] Parts; | internal readonly string[] Parts; | ||||
internal Func<DiscordBotClient.CommandEventArgs, Task> Handler; | |||||
internal Func<CommandEventArgs, Task> Handler; | |||||
internal Command(string text) | internal Command(string text) | ||||
{ | { | ||||
@@ -54,12 +54,12 @@ namespace Discord.Commands | |||||
return this; | return this; | ||||
} | } | ||||
public CommandBuilder Do(Func<DiscordBotClient.CommandEventArgs, Task> func) | |||||
public CommandBuilder Do(Func<CommandEventArgs, Task> func) | |||||
{ | { | ||||
_command.Handler = func; | _command.Handler = func; | ||||
return this; | return this; | ||||
} | } | ||||
public CommandBuilder Do(Action<DiscordBotClient.CommandEventArgs> func) | |||||
public CommandBuilder Do(Action<CommandEventArgs> func) | |||||
{ | { | ||||
#if DNXCORE50 | #if DNXCORE50 | ||||
_command.Handler = e => { func(e); return Task.CompletedTask; }; | _command.Handler = e => { func(e); return Task.CompletedTask; }; | ||||
@@ -82,10 +82,9 @@ namespace Discord.Commands | |||||
_defaultMinPermissions = defaultMinPermissions; | _defaultMinPermissions = defaultMinPermissions; | ||||
} | } | ||||
public CommandGroupBuilder DefaultMinPermissions(int level) | |||||
public void DefaultMinPermissions(int level) | |||||
{ | { | ||||
_defaultMinPermissions = level; | _defaultMinPermissions = level; | ||||
return this; | |||||
} | } | ||||
public CommandGroupBuilder CreateCommandGroup(string cmd, Action<CommandGroupBuilder> config = null) | public CommandGroupBuilder CreateCommandGroup(string cmd, Action<CommandGroupBuilder> config = null) | ||||
@@ -3,45 +3,43 @@ using System; | |||||
namespace Discord | 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; | public event EventHandler<CommandEventArgs> RanCommand; | ||||
private void RaiseRanCommand(CommandEventArgs args) | private void RaiseRanCommand(CommandEventArgs args) | ||||
{ | { | ||||
@@ -16,7 +16,7 @@ namespace Discord | |||||
public bool RequireCommandCharInPublic { get; set; } | public bool RequireCommandCharInPublic { get; set; } | ||||
public bool RequireCommandCharInPrivate { 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) | : base(config) | ||||
{ | { | ||||
_commands = new List<Command>(); | _commands = new List<Command>(); | ||||
@@ -56,7 +56,7 @@ namespace Discord | |||||
Command cmd = _commands[i]; | Command cmd = _commands[i]; | ||||
//Check Command Parts | //Check Command Parts | ||||
if (args.Length < cmd.Text.Length) | |||||
if (args.Length < cmd.Parts.Length) | |||||
continue; | continue; | ||||
bool isValid = true; | bool isValid = true; | ||||
@@ -82,7 +82,7 @@ namespace Discord | |||||
newArgs[j] = args[j + cmd.Parts.Length]; | newArgs[j] = args[j + cmd.Parts.Length]; | ||||
//Check Permissions | //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); | var eventArgs = new CommandEventArgs(e.Message, cmd, msg, permissions, newArgs); | ||||
if (permissions < cmd.MinPerms) | if (permissions < cmd.MinPerms) | ||||
{ | { | ||||
@@ -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.", | "description": "A small Discord.Net extension to make bot creation easier.", | ||||
"authors": [ "RogueException" ], | "authors": [ "RogueException" ], | ||||
"tags": [ "discord", "discordapp" ], | "tags": [ "discord", "discordapp" ], | ||||