Added CommandContext, fixed commands compile errors

This commit is contained in:
RogueException
2016-10-04 09:54:44 -03:00
parent 8aa1050b93
commit 708f9fe514
32 changed files with 187 additions and 148 deletions

View File

@@ -0,0 +1,8 @@
using System;
namespace Discord.Commands
{
public class InAttribute : Attribute
{
}
}

View File

@@ -6,6 +6,6 @@ namespace Discord.Commands
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public abstract class PreconditionAttribute : Attribute
{
public abstract Task<PreconditionResult> CheckPermissions(IUserMessage context, Command executingCommand, object moduleInstance);
public abstract Task<PreconditionResult> CheckPermissions(CommandContext context, Command executingCommand, object moduleInstance);
}
}

View File

@@ -21,7 +21,7 @@ namespace Discord.Commands
Contexts = contexts;
}
public override Task<PreconditionResult> CheckPermissions(IUserMessage context, Command executingCommand, object moduleInstance)
public override Task<PreconditionResult> CheckPermissions(CommandContext context, Command executingCommand, object moduleInstance)
{
bool isValid = false;

View File

@@ -20,9 +20,9 @@ namespace Discord.Commands
GuildPermission = null;
}
public override Task<PreconditionResult> CheckPermissions(IUserMessage context, Command executingCommand, object moduleInstance)
public override Task<PreconditionResult> CheckPermissions(CommandContext context, Command executingCommand, object moduleInstance)
{
var guildUser = context.Author as IGuildUser;
var guildUser = context.User as IGuildUser;
if (GuildPermission.HasValue)
{

View File

@@ -16,7 +16,7 @@ namespace Discord.Commands
private static readonly ConcurrentDictionary<Type, Func<IEnumerable<object>, object>> _arrayConverters = new ConcurrentDictionary<Type, Func<IEnumerable<object>, object>>();
private readonly object _instance;
private readonly Func<IUserMessage, IReadOnlyList<object>, Task> _action;
private readonly Func<CommandContext, IReadOnlyList<object>, Task> _action;
public MethodInfo Source { get; }
public Module Module { get; }
@@ -85,7 +85,7 @@ namespace Discord.Commands
}
}
public async Task<PreconditionResult> CheckPreconditions(IUserMessage context)
public async Task<PreconditionResult> CheckPreconditions(CommandContext context)
{
foreach (PreconditionAttribute precondition in Module.Preconditions)
{
@@ -104,7 +104,7 @@ namespace Discord.Commands
return PreconditionResult.FromSuccess();
}
public async Task<ParseResult> Parse(IUserMessage context, SearchResult searchResult, PreconditionResult? preconditionResult = null)
public async Task<ParseResult> Parse(CommandContext context, SearchResult searchResult, PreconditionResult? preconditionResult = null)
{
if (!searchResult.IsSuccess)
return ParseResult.FromError(searchResult);
@@ -125,7 +125,7 @@ namespace Discord.Commands
return await CommandParser.ParseArgs(this, context, input, 0).ConfigureAwait(false);
}
public Task<ExecuteResult> Execute(IUserMessage context, ParseResult parseResult)
public Task<ExecuteResult> Execute(CommandContext context, ParseResult parseResult)
{
if (!parseResult.IsSuccess)
return Task.FromResult(ExecuteResult.FromError(parseResult));
@@ -148,7 +148,7 @@ namespace Discord.Commands
return Execute(context, argList, paramList);
}
public async Task<ExecuteResult> Execute(IUserMessage context, IEnumerable<object> argList, IEnumerable<object> paramList)
public async Task<ExecuteResult> Execute(CommandContext context, IEnumerable<object> argList, IEnumerable<object> paramList)
{
try
{
@@ -209,7 +209,7 @@ namespace Discord.Commands
}
return paramBuilder.ToImmutable();
}
private Func<IUserMessage, IReadOnlyList<object>, Task> BuildAction(MethodInfo methodInfo)
private Func<CommandContext, IReadOnlyList<object>, Task> BuildAction(MethodInfo methodInfo)
{
if (methodInfo.ReturnType != typeof(Task))
throw new InvalidOperationException("Commands must return a non-generic Task.");

View File

@@ -0,0 +1,18 @@
namespace Discord.Commands
{
public struct CommandContext
{
public IGuild Guild { get; }
public IMessageChannel Channel { get; }
public IUser User { get; }
public IUserMessage Message { get; }
internal CommandContext(IGuild guild, IMessageChannel channel, IUser user, IUserMessage msg)
{
Guild = guild;
Channel = channel;
User = user;
Message = msg;
}
}
}

View File

@@ -32,7 +32,7 @@ namespace Discord.Commands
DefaultValue = defaultValue;
}
public async Task<TypeReaderResult> Parse(IUserMessage context, string input)
public async Task<TypeReaderResult> Parse(CommandContext context, string input)
{
return await _reader.Read(context, input).ConfigureAwait(false);
}

View File

@@ -13,7 +13,7 @@ namespace Discord.Commands
QuotedParameter
}
public static async Task<ParseResult> ParseArgs(Command command, IUserMessage context, string input, int startPos)
public static async Task<ParseResult> ParseArgs(Command command, CommandContext context, string input, int startPos)
{
CommandParameter curParam = null;
StringBuilder argBuilder = new StringBuilder(input.Length);

View File

@@ -176,8 +176,8 @@ namespace Discord.Commands
return false;
}
public SearchResult Search(IUserMessage message, int argPos) => Search(message, message.Content.Substring(argPos));
public SearchResult Search(IUserMessage message, string input)
public SearchResult Search(CommandContext context, int argPos) => Search(context, context.Message.Content.Substring(argPos));
public SearchResult Search(CommandContext context, string input)
{
string lowerInput = input.ToLowerInvariant();
var matches = _map.GetCommands(input).OrderByDescending(x => x.Priority).ToImmutableArray();
@@ -188,18 +188,18 @@ namespace Discord.Commands
return SearchResult.FromError(CommandError.UnknownCommand, "Unknown command.");
}
public Task<IResult> Execute(IUserMessage message, int argPos, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
=> Execute(message, message.Content.Substring(argPos), multiMatchHandling);
public async Task<IResult> Execute(IUserMessage message, string input, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
public Task<IResult> Execute(CommandContext context, int argPos, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
=> Execute(context, context.Message.Content.Substring(argPos), multiMatchHandling);
public async Task<IResult> Execute(CommandContext context, string input, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
{
var searchResult = Search(message, input);
var searchResult = Search(context, input);
if (!searchResult.IsSuccess)
return searchResult;
var commands = searchResult.Commands;
for (int i = commands.Count - 1; i >= 0; i--)
{
var preconditionResult = await commands[i].CheckPreconditions(message);
var preconditionResult = await commands[i].CheckPreconditions(context);
if (!preconditionResult.IsSuccess)
{
if (commands.Count == 1)
@@ -208,7 +208,7 @@ namespace Discord.Commands
continue;
}
var parseResult = await commands[i].Parse(message, searchResult, preconditionResult);
var parseResult = await commands[i].Parse(context, searchResult, preconditionResult);
if (!parseResult.IsSuccess)
{
if (parseResult.Error == CommandError.MultipleMatches)
@@ -233,7 +233,7 @@ namespace Discord.Commands
}
}
return await commands[i].Execute(message, parseResult);
return await commands[i].Execute(context, parseResult);
}
return SearchResult.FromError(CommandError.UnknownCommand, "This input does not match any overload.");

View File

@@ -9,23 +9,21 @@ namespace Discord.Commands
internal class ChannelTypeReader<T> : TypeReader
where T : class, IChannel
{
public override async Task<TypeReaderResult> Read(IUserMessage context, string input)
public override async Task<TypeReaderResult> Read(CommandContext context, string input)
{
var guild = (context.Channel as IGuildChannel)?.Guild;
if (guild != null)
if (context.Guild != null)
{
var results = new Dictionary<ulong, TypeReaderValue>();
var channels = await guild.GetChannelsAsync().ConfigureAwait(false);
var channels = await context.Guild.GetChannelsAsync().ConfigureAwait(false);
ulong id;
//By Mention (1.0)
if (MentionUtils.TryParseChannel(input, out id))
AddResult(results, await guild.GetChannelAsync(id).ConfigureAwait(false) as T, 1.00f);
AddResult(results, await context.Guild.GetChannelAsync(id).ConfigureAwait(false) as T, 1.00f);
//By Id (0.9)
if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id))
AddResult(results, await guild.GetChannelAsync(id).ConfigureAwait(false) as T, 0.90f);
AddResult(results, await context.Guild.GetChannelAsync(id).ConfigureAwait(false) as T, 0.90f);
//By Name (0.7-0.8)
foreach (var channel in channels.Where(x => string.Equals(input, x.Name, StringComparison.OrdinalIgnoreCase)))

View File

@@ -42,7 +42,7 @@ namespace Discord.Commands
_enumsByValue = byValueBuilder.ToImmutable();
}
public override Task<TypeReaderResult> Read(IUserMessage context, string input)
public override Task<TypeReaderResult> Read(CommandContext context, string input)
{
T baseValue;
object enumValue;

View File

@@ -6,19 +6,19 @@ namespace Discord.Commands
internal class MessageTypeReader<T> : TypeReader
where T : class, IMessage
{
public override Task<TypeReaderResult> Read(IUserMessage context, string input)
public override async Task<TypeReaderResult> Read(CommandContext context, string input)
{
ulong id;
//By Id (1.0)
if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id))
{
var msg = context.Channel.GetCachedMessage(id) as T;
var msg = await context.Channel.GetMessageAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) as T;
if (msg != null)
return Task.FromResult(TypeReaderResult.FromSuccess(msg));
return TypeReaderResult.FromSuccess(msg);
}
return Task.FromResult(TypeReaderResult.FromError(CommandError.ObjectNotFound, "Message not found."));
return TypeReaderResult.FromError(CommandError.ObjectNotFound, "Message not found.");
}
}
}

View File

@@ -9,23 +9,22 @@ namespace Discord.Commands
internal class RoleTypeReader<T> : TypeReader
where T : class, IRole
{
public override Task<TypeReaderResult> Read(IUserMessage context, string input)
public override Task<TypeReaderResult> Read(CommandContext context, string input)
{
var guild = (context.Channel as IGuildChannel)?.Guild;
ulong id;
if (guild != null)
if (context.Guild != null)
{
var results = new Dictionary<ulong, TypeReaderValue>();
var roles = guild.Roles;
var roles = context.Guild.Roles;
//By Mention (1.0)
if (MentionUtils.TryParseRole(input, out id))
AddResult(results, guild.GetRole(id) as T, 1.00f);
AddResult(results, context.Guild.GetRole(id) as T, 1.00f);
//By Id (0.9)
if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id))
AddResult(results, guild.GetRole(id) as T, 0.90f);
AddResult(results, context.Guild.GetRole(id) as T, 0.90f);
//By Name (0.7-0.8)
foreach (var role in roles.Where(x => string.Equals(input, x.Name, StringComparison.OrdinalIgnoreCase)))

View File

@@ -11,7 +11,7 @@ namespace Discord.Commands
_tryParse = PrimitiveParsers.Get<T>();
}
public override Task<TypeReaderResult> Read(IUserMessage context, string input)
public override Task<TypeReaderResult> Read(CommandContext context, string input)
{
T value;
if (_tryParse(input, out value))

View File

@@ -4,6 +4,6 @@ namespace Discord.Commands
{
public abstract class TypeReader
{
public abstract Task<TypeReaderResult> Read(IUserMessage context, string input);
public abstract Task<TypeReaderResult> Read(CommandContext context, string input);
}
}

View File

@@ -9,22 +9,21 @@ namespace Discord.Commands
internal class UserTypeReader<T> : TypeReader
where T : class, IUser
{
public override async Task<TypeReaderResult> Read(IUserMessage context, string input)
public override async Task<TypeReaderResult> Read(CommandContext context, string input)
{
var results = new Dictionary<ulong, TypeReaderValue>();
var guild = (context.Channel as IGuildChannel)?.Guild;
IReadOnlyCollection<IUser> channelUsers = await context.Channel.GetUsersAsync().ConfigureAwait(false);
IReadOnlyCollection<IUser> channelUsers = (await context.Channel.GetUsersAsync().Flatten().ConfigureAwait(false)).ToArray(); //TODO: must be a better way?
IReadOnlyCollection<IGuildUser> guildUsers = null;
ulong id;
if (guild != null)
guildUsers = await guild.GetUsersAsync().ConfigureAwait(false);
if (context.Guild != null)
guildUsers = await context.Guild.GetUsersAsync().ConfigureAwait(false);
//By Mention (1.0)
if (MentionUtils.TryParseUser(input, out id))
{
if (guild != null)
AddResult(results, await guild.GetUserAsync(id).ConfigureAwait(false) as T, 1.00f);
if (context.Guild != null)
AddResult(results, await context.Guild.GetUserAsync(id).ConfigureAwait(false) as T, 1.00f);
else
AddResult(results, await context.Channel.GetUserAsync(id).ConfigureAwait(false) as T, 1.00f);
}
@@ -32,8 +31,8 @@ namespace Discord.Commands
//By Id (0.9)
if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id))
{
if (guild != null)
AddResult(results, await guild.GetUserAsync(id).ConfigureAwait(false) as T, 0.90f);
if (context.Guild != null)
AddResult(results, await context.Guild.GetUserAsync(id).ConfigureAwait(false) as T, 0.90f);
else
AddResult(results, await context.Channel.GetUserAsync(id).ConfigureAwait(false) as T, 0.90f);
}

View File

@@ -13,18 +13,14 @@
}
},
"buildOptions": {
"allowUnsafe": true,
"warningsAsErrors": false,
"xmlDoc": true
},
"configurations": {
"Release": {
"buildOptions": {
"define": [ "RELEASE" ],
"nowarn": [ "CS1573", "CS1591" ],
"optimize": true
"optimize": true,
"warningsAsErrors": true,
"xmlDoc": true
}
}
},