Initial 1.0-alpha1 design changes
This commit is contained in:
@@ -52,7 +52,7 @@ namespace Discord.Commands
|
||||
_checks = checks;
|
||||
}
|
||||
|
||||
internal bool CanRun(User user, Channel channel, out string error)
|
||||
internal bool CanRun(User user, ITextChannel channel, out string error)
|
||||
{
|
||||
for (int i = 0; i < _checks.Length; i++)
|
||||
{
|
||||
|
||||
@@ -79,7 +79,7 @@ namespace Discord.Commands
|
||||
_checks.Add(check);
|
||||
return this;
|
||||
}
|
||||
public CommandBuilder AddCheck(Func<Command, User, Channel, bool> checkFunc, string errorMsg = null)
|
||||
public CommandBuilder AddCheck(Func<Command, User, ITextChannel, bool> checkFunc, string errorMsg = null)
|
||||
{
|
||||
_checks.Add(new GenericPermissionChecker(checkFunc, errorMsg));
|
||||
return this;
|
||||
@@ -145,7 +145,7 @@ namespace Discord.Commands
|
||||
{
|
||||
_checks.Add(checker);
|
||||
}
|
||||
public void AddCheck(Func<Command, User, Channel, bool> checkFunc, string errorMsg = null)
|
||||
public void AddCheck(Func<Command, User, ITextChannel, bool> checkFunc, string errorMsg = null)
|
||||
{
|
||||
_checks.Add(new GenericPermissionChecker(checkFunc, errorMsg));
|
||||
}
|
||||
|
||||
@@ -10,8 +10,7 @@ namespace Discord.Commands
|
||||
public Command Command { get; }
|
||||
|
||||
public User User => Message.User;
|
||||
public Channel Channel => Message.Channel;
|
||||
public Server Server => Message.Channel.Server;
|
||||
public ITextChannel Channel => Message.Channel;
|
||||
|
||||
public CommandEventArgs(Message message, Command command, string[] args)
|
||||
{
|
||||
|
||||
@@ -116,7 +116,7 @@ namespace Discord.Commands
|
||||
}
|
||||
}
|
||||
|
||||
public bool CanRun(User user, Channel channel, out string error)
|
||||
public bool CanRun(User user, ITextChannel channel, out string error)
|
||||
{
|
||||
error = null;
|
||||
if (_commands.Count > 0)
|
||||
|
||||
@@ -63,7 +63,7 @@ namespace Discord.Commands
|
||||
.Description("Returns information about commands.")
|
||||
.Do(async e =>
|
||||
{
|
||||
Channel replyChannel = Config.HelpMode == HelpMode.Public ? e.Channel : await e.User.CreatePMChannel().ConfigureAwait(false);
|
||||
ITextChannel replyChannel = Config.HelpMode == HelpMode.Public ? e.Channel : await e.User.CreatePMChannel().ConfigureAwait(false);
|
||||
if (e.Args.Length > 0) //Show command help
|
||||
{
|
||||
var map = _map.GetItem(string.Join(" ", e.Args));
|
||||
@@ -175,7 +175,7 @@ namespace Discord.Commands
|
||||
};
|
||||
}
|
||||
|
||||
public Task ShowGeneralHelp(User user, Channel channel, Channel replyChannel = null)
|
||||
public Task ShowGeneralHelp(User user, ITextChannel channel, ITextChannel replyChannel = null)
|
||||
{
|
||||
StringBuilder output = new StringBuilder();
|
||||
bool isFirstCategory = true;
|
||||
@@ -219,32 +219,12 @@ namespace Discord.Commands
|
||||
if (output.Length == 0)
|
||||
output.Append("There are no commands you have permission to run.");
|
||||
else
|
||||
{
|
||||
output.Append("\n\n");
|
||||
|
||||
//TODO: Should prefix be stated in the help message or not?
|
||||
/*StringBuilder builder = new StringBuilder();
|
||||
if (Config.PrefixChar != null)
|
||||
{
|
||||
builder.Append('`');
|
||||
builder.Append(Config.PrefixChar.Value);
|
||||
builder.Append('`');
|
||||
}
|
||||
if (Config.AllowMentionPrefix)
|
||||
{
|
||||
if (builder.Length > 0)
|
||||
builder.Append(" or ");
|
||||
builder.Append(Client.CurrentUser.Mention);
|
||||
}
|
||||
if (builder.Length > 0)
|
||||
output.AppendLine($"Start your message with {builder.ToString()} to run a command.");*/
|
||||
output.AppendLine($"Run `help <command>` for more information.");
|
||||
}
|
||||
output.AppendLine("\n\nRun `help <command>` for more information.");
|
||||
|
||||
return (replyChannel ?? channel).SendMessage(output.ToString());
|
||||
}
|
||||
|
||||
private Task ShowCommandHelp(CommandMap map, User user, Channel channel, Channel replyChannel = null)
|
||||
private Task ShowCommandHelp(CommandMap map, User user, ITextChannel channel, ITextChannel replyChannel = null)
|
||||
{
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
@@ -255,9 +235,7 @@ namespace Discord.Commands
|
||||
{
|
||||
foreach (var cmd in cmds)
|
||||
{
|
||||
if (!cmd.CanRun(user, channel, out error)) { }
|
||||
//output.AppendLine(error ?? DefaultPermissionError);
|
||||
else
|
||||
if (cmd.CanRun(user, channel, out error))
|
||||
{
|
||||
if (isFirstCmd)
|
||||
isFirstCmd = false;
|
||||
@@ -299,7 +277,7 @@ namespace Discord.Commands
|
||||
|
||||
return (replyChannel ?? channel).SendMessage(output.ToString());
|
||||
}
|
||||
public Task ShowCommandHelp(Command command, User user, Channel channel, Channel replyChannel = null)
|
||||
public Task ShowCommandHelp(Command command, User user, ITextChannel channel, ITextChannel replyChannel = null)
|
||||
{
|
||||
StringBuilder output = new StringBuilder();
|
||||
string error;
|
||||
@@ -309,7 +287,7 @@ namespace Discord.Commands
|
||||
ShowCommandHelpInternal(command, user, channel, output);
|
||||
return (replyChannel ?? channel).SendMessage(output.ToString());
|
||||
}
|
||||
private void ShowCommandHelpInternal(Command command, User user, Channel channel, StringBuilder output)
|
||||
private void ShowCommandHelpInternal(Command command, User user, ITextChannel channel, StringBuilder output)
|
||||
{
|
||||
output.Append('`');
|
||||
output.Append(command.Text);
|
||||
|
||||
@@ -4,16 +4,16 @@ namespace Discord.Commands.Permissions
|
||||
{
|
||||
internal class GenericPermissionChecker : IPermissionChecker
|
||||
{
|
||||
private readonly Func<Command, User, Channel, bool> _checkFunc;
|
||||
private readonly Func<Command, User, ITextChannel, bool> _checkFunc;
|
||||
private readonly string _error;
|
||||
|
||||
public GenericPermissionChecker(Func<Command, User, Channel, bool> checkFunc, string error = null)
|
||||
public GenericPermissionChecker(Func<Command, User, ITextChannel, bool> checkFunc, string error = null)
|
||||
{
|
||||
_checkFunc = checkFunc;
|
||||
_error = error;
|
||||
}
|
||||
|
||||
public bool CanRun(Command command, User user, Channel channel, out string error)
|
||||
public bool CanRun(Command command, User user, ITextChannel channel, out string error)
|
||||
{
|
||||
error = _error;
|
||||
return _checkFunc(command, user, channel);
|
||||
@@ -2,6 +2,6 @@
|
||||
{
|
||||
public interface IPermissionChecker
|
||||
{
|
||||
bool CanRun(Command command, User user, Channel channel, out string error);
|
||||
bool CanRun(Command command, User user, ITextChannel channel, out string error);
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
namespace Discord.Commands.Permissions.Levels
|
||||
{
|
||||
public class PermissionLevelChecker : IPermissionChecker
|
||||
{
|
||||
private readonly PermissionLevelService _service;
|
||||
private readonly int _minPermissions;
|
||||
|
||||
public PermissionLevelService Service => _service;
|
||||
public int MinPermissions => _minPermissions;
|
||||
|
||||
internal PermissionLevelChecker(DiscordClient client, int minPermissions)
|
||||
{
|
||||
_service = client.GetService<PermissionLevelService>(true);
|
||||
_minPermissions = minPermissions;
|
||||
}
|
||||
|
||||
public bool CanRun(Command command, User user, Channel channel, out string error)
|
||||
{
|
||||
error = null; //Use default error text.
|
||||
int permissions = _service.GetPermissionLevel(user, channel);
|
||||
return permissions >= _minPermissions;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Discord.Commands.Permissions.Levels
|
||||
{
|
||||
public static class PermissionLevelExtensions
|
||||
{
|
||||
public static DiscordClient UsingPermissionLevels(this DiscordClient client, Func<User, Channel, int> permissionResolver)
|
||||
{
|
||||
client.AddService(new PermissionLevelService(permissionResolver));
|
||||
return client;
|
||||
}
|
||||
|
||||
public static CommandBuilder MinPermissions(this CommandBuilder builder, int minPermissions)
|
||||
{
|
||||
builder.AddCheck(new PermissionLevelChecker(builder.Service.Client, minPermissions));
|
||||
return builder;
|
||||
}
|
||||
public static CommandGroupBuilder MinPermissions(this CommandGroupBuilder builder, int minPermissions)
|
||||
{
|
||||
builder.AddCheck(new PermissionLevelChecker(builder.Service.Client, minPermissions));
|
||||
return builder;
|
||||
}
|
||||
public static CommandService MinPermissions(this CommandService service, int minPermissions)
|
||||
{
|
||||
service.Root.AddCheck(new PermissionLevelChecker(service.Client, minPermissions));
|
||||
return service;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Discord.Commands.Permissions.Levels
|
||||
{
|
||||
public class PermissionLevelService : IService
|
||||
{
|
||||
private readonly Func<User, Channel, int> _getPermissionsFunc;
|
||||
|
||||
private DiscordClient _client;
|
||||
public DiscordClient Client => _client;
|
||||
|
||||
public PermissionLevelService(Func<User, Channel, int> getPermissionsFunc)
|
||||
{
|
||||
_getPermissionsFunc = getPermissionsFunc;
|
||||
}
|
||||
|
||||
public void Install(DiscordClient client)
|
||||
{
|
||||
_client = client;
|
||||
}
|
||||
public int GetPermissionLevel(User user, Channel channel) => _getPermissionsFunc(user, channel);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
namespace Discord.Commands.Permissions.Userlist
|
||||
{
|
||||
public class BlacklistChecker : IPermissionChecker
|
||||
{
|
||||
private readonly BlacklistService _service;
|
||||
|
||||
internal BlacklistChecker(DiscordClient client)
|
||||
{
|
||||
_service = client.GetService<BlacklistService>(true);
|
||||
}
|
||||
|
||||
public bool CanRun(Command command, User user, Channel channel, out string error)
|
||||
{
|
||||
error = null; //Use default error text.
|
||||
return _service.CanRun(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Discord.Commands.Permissions.Userlist
|
||||
{
|
||||
public static class BlacklistExtensions
|
||||
{
|
||||
public static DiscordClient UsingGlobalBlacklist(this DiscordClient client, params ulong[] initialUserIds)
|
||||
{
|
||||
client.AddService(new BlacklistService(initialUserIds));
|
||||
return client;
|
||||
}
|
||||
|
||||
public static CommandBuilder UseGlobalBlacklist(this CommandBuilder builder)
|
||||
{
|
||||
builder.AddCheck(new BlacklistChecker(builder.Service.Client));
|
||||
return builder;
|
||||
}
|
||||
public static CommandGroupBuilder UseGlobalBlacklist(this CommandGroupBuilder builder)
|
||||
{
|
||||
builder.AddCheck(new BlacklistChecker(builder.Service.Client));
|
||||
return builder;
|
||||
}
|
||||
public static CommandService UseGlobalBlacklist(this CommandService service)
|
||||
{
|
||||
service.Root.AddCheck(new BlacklistChecker(service.Client));
|
||||
return service;
|
||||
}
|
||||
|
||||
public static IEnumerable<ulong> GetBlacklistedUserIds(this DiscordClient client)
|
||||
=> client.GetService<BlacklistService>().UserIds;
|
||||
public static void BlacklistUser(this DiscordClient client, User user)
|
||||
{
|
||||
client.GetService<BlacklistService>().Add(user.Id);
|
||||
}
|
||||
public static void BlacklistUser(this DiscordClient client, ulong userId)
|
||||
{
|
||||
client.GetService<BlacklistService>().Add(userId);
|
||||
}
|
||||
public static void UnBlacklistUser(this DiscordClient client, User user)
|
||||
{
|
||||
client.GetService<BlacklistService>().Remove(user.Id);
|
||||
}
|
||||
public static void UnBlacklistUser(this DiscordClient client, ulong userId)
|
||||
{
|
||||
client.GetService<BlacklistService>().Remove(userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
namespace Discord.Commands.Permissions.Userlist
|
||||
{
|
||||
public class BlacklistService : UserlistService
|
||||
{
|
||||
public BlacklistService(params ulong[] initialList)
|
||||
: base(initialList)
|
||||
{
|
||||
}
|
||||
|
||||
public bool CanRun(User user)
|
||||
=> !_userList.ContainsKey(user.Id);
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Discord.Commands.Permissions.Userlist
|
||||
{
|
||||
public class UserlistService : IService
|
||||
{
|
||||
protected readonly ConcurrentDictionary<ulong, bool> _userList;
|
||||
private DiscordClient _client;
|
||||
|
||||
public DiscordClient Client => _client;
|
||||
public IEnumerable<ulong> UserIds => _userList.Select(x => x.Key);
|
||||
|
||||
public UserlistService(params ulong[] initialUserIds)
|
||||
{
|
||||
_userList = new ConcurrentDictionary<ulong, bool>();
|
||||
for (int i = 0; i < initialUserIds.Length; i++)
|
||||
_userList.TryAdd(initialUserIds[i], true);
|
||||
}
|
||||
|
||||
public void Add(User user)
|
||||
{
|
||||
if (user == null) throw new ArgumentNullException(nameof(user));
|
||||
|
||||
_userList[user.Id] = true;
|
||||
}
|
||||
public void Add(ulong userId)
|
||||
{
|
||||
_userList[userId] = true;
|
||||
}
|
||||
|
||||
public bool Remove(User user)
|
||||
{
|
||||
if (user == null) throw new ArgumentNullException(nameof(user));
|
||||
|
||||
bool ignored;
|
||||
return _userList.TryRemove(user.Id, out ignored);
|
||||
}
|
||||
public bool Remove(ulong userId)
|
||||
{
|
||||
bool ignored;
|
||||
return _userList.TryRemove(userId, out ignored);
|
||||
}
|
||||
|
||||
void IService.Install(DiscordClient client)
|
||||
{
|
||||
_client = client;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
namespace Discord.Commands.Permissions.Userlist
|
||||
{
|
||||
public class WhitelistChecker : IPermissionChecker
|
||||
{
|
||||
private readonly WhitelistService _service;
|
||||
|
||||
internal WhitelistChecker(DiscordClient client)
|
||||
{
|
||||
_service = client.GetService<WhitelistService>(true);
|
||||
}
|
||||
|
||||
public bool CanRun(Command command, User user, Channel channel, out string error)
|
||||
{
|
||||
error = null; //Use default error text.
|
||||
return _service.CanRun(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Discord.Commands.Permissions.Userlist
|
||||
{
|
||||
public static class WhitelistExtensions
|
||||
{
|
||||
public static DiscordClient UsingGlobalWhitelist(this DiscordClient client, params ulong[] initialUserIds)
|
||||
{
|
||||
client.AddService(new WhitelistService(initialUserIds));
|
||||
return client;
|
||||
}
|
||||
|
||||
public static CommandBuilder UseGlobalWhitelist(this CommandBuilder builder)
|
||||
{
|
||||
builder.AddCheck(new WhitelistChecker(builder.Service.Client));
|
||||
return builder;
|
||||
}
|
||||
public static CommandGroupBuilder UseGlobalWhitelist(this CommandGroupBuilder builder)
|
||||
{
|
||||
builder.AddCheck(new WhitelistChecker(builder.Service.Client));
|
||||
return builder;
|
||||
}
|
||||
public static CommandService UseGlobalWhitelist(this CommandService service)
|
||||
{
|
||||
service.Root.AddCheck(new BlacklistChecker(service.Client));
|
||||
return service;
|
||||
}
|
||||
|
||||
public static IEnumerable<ulong> GetWhitelistedUserIds(this DiscordClient client)
|
||||
=> client.GetService<WhitelistService>().UserIds;
|
||||
public static void WhitelistUser(this DiscordClient client, User user)
|
||||
{
|
||||
client.GetService<WhitelistService>().Add(user.Id);
|
||||
}
|
||||
public static void WhitelistUser(this DiscordClient client, ulong userId)
|
||||
{
|
||||
client.GetService<WhitelistService>().Add(userId);
|
||||
}
|
||||
public static void UnWhitelistUser(this DiscordClient client, User user)
|
||||
{
|
||||
client.GetService<WhitelistService>().Remove(user.Id);
|
||||
}
|
||||
public static void RemoveFromWhitelist(this DiscordClient client, ulong userId)
|
||||
{
|
||||
client.GetService<WhitelistService>().Remove(userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
namespace Discord.Commands.Permissions.Userlist
|
||||
{
|
||||
public class WhitelistService : UserlistService
|
||||
{
|
||||
public WhitelistService(params ulong[] initialList)
|
||||
: base(initialList)
|
||||
{
|
||||
}
|
||||
|
||||
public bool CanRun(User user)
|
||||
=> _userList.ContainsKey(user.Id);
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
namespace Discord.Commands.Permissions.Visibility
|
||||
{
|
||||
public class PrivateChecker : IPermissionChecker
|
||||
{
|
||||
internal PrivateChecker() { }
|
||||
|
||||
public bool CanRun(Command command, User user, Channel channel, out string error)
|
||||
{
|
||||
if (user.Server != null)
|
||||
{
|
||||
error = "This command may only be run in a private chat.";
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
error = null;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
namespace Discord.Commands.Permissions.Visibility
|
||||
{
|
||||
public static class PrivateExtensions
|
||||
{
|
||||
public static CommandBuilder PrivateOnly(this CommandBuilder builder)
|
||||
{
|
||||
builder.AddCheck(new PrivateChecker());
|
||||
return builder;
|
||||
}
|
||||
public static CommandGroupBuilder PrivateOnly(this CommandGroupBuilder builder)
|
||||
{
|
||||
builder.AddCheck(new PrivateChecker());
|
||||
return builder;
|
||||
}
|
||||
public static CommandService PrivateOnly(this CommandService service)
|
||||
{
|
||||
service.Root.AddCheck(new PrivateChecker());
|
||||
return service;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
namespace Discord.Commands.Permissions.Visibility
|
||||
{
|
||||
public class PublicChecker : IPermissionChecker
|
||||
{
|
||||
internal PublicChecker() { }
|
||||
|
||||
public bool CanRun(Command command, User user, Channel channel, out string error)
|
||||
{
|
||||
if (user.Server == null)
|
||||
{
|
||||
error = "This command can't be run in a private chat.";
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
error = null;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
namespace Discord.Commands.Permissions.Visibility
|
||||
{
|
||||
public static class PublicExtensions
|
||||
{
|
||||
public static CommandBuilder PublicOnly(this CommandBuilder builder)
|
||||
{
|
||||
builder.AddCheck(new PublicChecker());
|
||||
return builder;
|
||||
}
|
||||
public static CommandGroupBuilder PublicOnly(this CommandGroupBuilder builder)
|
||||
{
|
||||
builder.AddCheck(new PublicChecker());
|
||||
return builder;
|
||||
}
|
||||
public static CommandService PublicOnly(this CommandService service)
|
||||
{
|
||||
service.Root.AddCheck(new PublicChecker());
|
||||
return service;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "0.9.0-rc3-1",
|
||||
"version": "1.0.0-alpha1",
|
||||
"description": "A Discord.Net extension adding basic command support.",
|
||||
"authors": [ "RogueException" ],
|
||||
"tags": [ "discord", "discordapp" ],
|
||||
@@ -16,7 +16,7 @@
|
||||
},
|
||||
|
||||
"dependencies": {
|
||||
"Discord.Net": "0.9.0-rc3-3"
|
||||
"Discord.Net": "1.0.0-alpha1"
|
||||
},
|
||||
"frameworks": {
|
||||
"net45": { },
|
||||
|
||||
Reference in New Issue
Block a user