@@ -46,11 +46,11 @@ | |||||
<Compile Include="..\Discord.Net.Commands\CommandParser.cs"> | <Compile Include="..\Discord.Net.Commands\CommandParser.cs"> | ||||
<Link>CommandParser.cs</Link> | <Link>CommandParser.cs</Link> | ||||
</Compile> | </Compile> | ||||
<Compile Include="..\Discord.Net.Commands\DiscordBotClient.cs"> | |||||
<Link>DiscordBotClient.cs</Link> | |||||
<Compile Include="..\Discord.Net.Commands\CommandsPlugin.cs"> | |||||
<Link>CommandsPlugin.cs</Link> | |||||
</Compile> | </Compile> | ||||
<Compile Include="..\Discord.Net.Commands\DiscordBotClient.Events.cs"> | |||||
<Link>DiscordBotClient.Events.cs</Link> | |||||
<Compile Include="..\Discord.Net.Commands\CommandsPlugin.Events.cs"> | |||||
<Link>CommandsPlugin.Events.cs</Link> | |||||
</Compile> | </Compile> | ||||
<Compile Include="Properties\AssemblyInfo.cs" /> | <Compile Include="Properties\AssemblyInfo.cs" /> | ||||
</ItemGroup> | </ItemGroup> | ||||
@@ -71,13 +71,13 @@ namespace Discord.Commands | |||||
} | } | ||||
public sealed class CommandGroupBuilder | public sealed class CommandGroupBuilder | ||||
{ | { | ||||
private readonly DiscordBotClient _client; | |||||
private readonly CommandsPlugin _plugin; | |||||
private readonly string _prefix; | private readonly string _prefix; | ||||
private int _defaultMinPermissions; | private int _defaultMinPermissions; | ||||
internal CommandGroupBuilder(DiscordBotClient client, string prefix, int defaultMinPermissions) | |||||
internal CommandGroupBuilder(CommandsPlugin plugin, string prefix, int defaultMinPermissions) | |||||
{ | { | ||||
_client = client; | |||||
_plugin = plugin; | |||||
_prefix = prefix; | _prefix = prefix; | ||||
_defaultMinPermissions = defaultMinPermissions; | _defaultMinPermissions = defaultMinPermissions; | ||||
} | } | ||||
@@ -89,7 +89,7 @@ namespace Discord.Commands | |||||
public CommandGroupBuilder CreateCommandGroup(string cmd, Action<CommandGroupBuilder> config = null) | public CommandGroupBuilder CreateCommandGroup(string cmd, Action<CommandGroupBuilder> config = null) | ||||
{ | { | ||||
config(new CommandGroupBuilder(_client, _prefix + ' ' + cmd, _defaultMinPermissions)); | |||||
config(new CommandGroupBuilder(_plugin, _prefix + ' ' + cmd, _defaultMinPermissions)); | |||||
return this; | return this; | ||||
} | } | ||||
public CommandBuilder CreateCommand() | public CommandBuilder CreateCommand() | ||||
@@ -98,7 +98,7 @@ namespace Discord.Commands | |||||
{ | { | ||||
var command = new Command(cmd != "" ? _prefix + ' ' + cmd : _prefix); | var command = new Command(cmd != "" ? _prefix + ' ' + cmd : _prefix); | ||||
command.MinPerms = _defaultMinPermissions; | command.MinPerms = _defaultMinPermissions; | ||||
_client._commands.Add(command); | |||||
_plugin._commands.Add(command); | |||||
return new CommandBuilder(command); | return new CommandBuilder(command); | ||||
} | } | ||||
} | } | ||||
@@ -1,7 +1,6 @@ | |||||
using Discord.Commands; | |||||
using System; | |||||
using System; | |||||
namespace Discord | |||||
namespace Discord.Commands | |||||
{ | { | ||||
public class PermissionException : Exception { public PermissionException() : base("User does not have permission to run this command.") { } } | public class PermissionException : Exception { public PermissionException() : base("User does not have permission to run this command.") { } } | ||||
public class CommandEventArgs | public class CommandEventArgs | ||||
@@ -38,7 +37,7 @@ namespace Discord | |||||
Exception = ex; | Exception = ex; | ||||
} | } | ||||
} | } | ||||
public partial class DiscordBotClient : DiscordClient | |||||
public partial class CommandsPlugin | |||||
{ | { | ||||
public event EventHandler<CommandEventArgs> RanCommand; | public event EventHandler<CommandEventArgs> RanCommand; | ||||
private void RaiseRanCommand(CommandEventArgs args) | private void RaiseRanCommand(CommandEventArgs args) |
@@ -1,13 +1,13 @@ | |||||
using Discord.Commands; | |||||
using System; | |||||
using System; | |||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
namespace Discord | |||||
namespace Discord.Commands | |||||
{ | { | ||||
/// <summary> A Discord.Net client with extensions for handling common bot operations like text commands. </summary> | /// <summary> A Discord.Net client with extensions for handling common bot operations like text commands. </summary> | ||||
public partial class DiscordBotClient : DiscordClient | |||||
public partial class CommandsPlugin | |||||
{ | { | ||||
internal List<Command> _commands; | internal List<Command> _commands; | ||||
private Func<User, Server, int> _getPermissions; | |||||
public IEnumerable<Command> Commands => _commands; | public IEnumerable<Command> Commands => _commands; | ||||
@@ -16,24 +16,27 @@ 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, Server, int> getPermissions = null) | |||||
: base(config) | |||||
public CommandsPlugin(Func<User, Server, int> getPermissions = null) | |||||
{ | { | ||||
_getPermissions = getPermissions; | |||||
_commands = new List<Command>(); | _commands = new List<Command>(); | ||||
CommandChar = '~'; | |||||
UseCommandChar = true; | |||||
CommandChar = '/'; | |||||
UseCommandChar = false; | |||||
RequireCommandCharInPublic = true; | RequireCommandCharInPublic = true; | ||||
RequireCommandCharInPrivate = true; | RequireCommandCharInPrivate = true; | ||||
} | |||||
MessageCreated += async (s, e) => | |||||
public void Install(DiscordClient client) | |||||
{ | |||||
client.MessageCreated += async (s, e) => | |||||
{ | { | ||||
//If commands aren't being used, don't bother processing them | //If commands aren't being used, don't bother processing them | ||||
if (_commands.Count == 0) | if (_commands.Count == 0) | ||||
return; | return; | ||||
//Ignore messages from ourselves | //Ignore messages from ourselves | ||||
if (e.Message.UserId == CurrentUserId) | |||||
if (e.Message.UserId == client.CurrentUserId) | |||||
return; | return; | ||||
//Check for the command character | //Check for the command character | ||||
@@ -87,7 +90,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, e.Message.Channel?.Server) : 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) | ||||
{ | { |