Update commands with C#7 features (#689)
* C#7 features in commands, CommandInfo in ModuleBase * Update TypeReaders with C#7 features and IServiceProvider * Add best-choice command selection to CommandService * Normalize type reader scores correctly * Fix logic error and rebase onto dev * Change GetMethod for SetMethod in ReflectionUtils Should be checking against setters, not getters * Ensure args/params scores do not overwhelm Priority * Remove possibility of NaNs
This commit is contained in:
committed by
RogueException
parent
41222eafeb
commit
032aba9129
@@ -9,7 +9,7 @@ namespace Discord.Commands
|
||||
internal class ChannelTypeReader<T> : TypeReader
|
||||
where T : class, IChannel
|
||||
{
|
||||
public override async Task<TypeReaderResult> Read(ICommandContext context, string input)
|
||||
public override async Task<TypeReaderResult> Read(ICommandContext context, string input, IServiceProvider services)
|
||||
{
|
||||
if (context.Guild != null)
|
||||
{
|
||||
|
||||
@@ -44,12 +44,11 @@ namespace Discord.Commands
|
||||
_enumsByValue = byValueBuilder.ToImmutable();
|
||||
}
|
||||
|
||||
public override Task<TypeReaderResult> Read(ICommandContext context, string input)
|
||||
public override Task<TypeReaderResult> Read(ICommandContext context, string input, IServiceProvider services)
|
||||
{
|
||||
T baseValue;
|
||||
object enumValue;
|
||||
|
||||
if (_tryParse(input, out baseValue))
|
||||
if (_tryParse(input, out T baseValue))
|
||||
{
|
||||
if (_enumsByValue.TryGetValue(baseValue, out enumValue))
|
||||
return Task.FromResult(TypeReaderResult.FromSuccess(enumValue));
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Globalization;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Commands
|
||||
@@ -6,15 +7,14 @@ namespace Discord.Commands
|
||||
internal class MessageTypeReader<T> : TypeReader
|
||||
where T : class, IMessage
|
||||
{
|
||||
public override async Task<TypeReaderResult> Read(ICommandContext context, string input)
|
||||
public override async Task<TypeReaderResult> Read(ICommandContext context, string input, IServiceProvider services)
|
||||
{
|
||||
ulong id;
|
||||
|
||||
//By Id (1.0)
|
||||
if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id))
|
||||
{
|
||||
var msg = await context.Channel.GetMessageAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) as T;
|
||||
if (msg != null)
|
||||
if (await context.Channel.GetMessageAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) is T msg)
|
||||
return TypeReaderResult.FromSuccess(msg);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,17 +15,25 @@ namespace Discord.Commands
|
||||
internal class PrimitiveTypeReader<T> : TypeReader
|
||||
{
|
||||
private readonly TryParseDelegate<T> _tryParse;
|
||||
private readonly float _score;
|
||||
|
||||
public PrimitiveTypeReader()
|
||||
: this(PrimitiveParsers.Get<T>(), 1)
|
||||
{ }
|
||||
|
||||
public PrimitiveTypeReader(TryParseDelegate<T> tryParse, float score)
|
||||
{
|
||||
_tryParse = PrimitiveParsers.Get<T>();
|
||||
if (score < 0 || score > 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(score), score, "Scores must be within the range [0, 1]");
|
||||
|
||||
_tryParse = tryParse;
|
||||
_score = score;
|
||||
}
|
||||
|
||||
public override Task<TypeReaderResult> Read(ICommandContext context, string input)
|
||||
public override Task<TypeReaderResult> Read(ICommandContext context, string input, IServiceProvider services)
|
||||
{
|
||||
T value;
|
||||
if (_tryParse(input, out value))
|
||||
return Task.FromResult(TypeReaderResult.FromSuccess(value));
|
||||
if (_tryParse(input, out T value))
|
||||
return Task.FromResult(TypeReaderResult.FromSuccess(new TypeReaderValue(value, _score)));
|
||||
return Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, $"Failed to parse {typeof(T).Name}"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Discord.Commands
|
||||
internal class RoleTypeReader<T> : TypeReader
|
||||
where T : class, IRole
|
||||
{
|
||||
public override Task<TypeReaderResult> Read(ICommandContext context, string input)
|
||||
public override Task<TypeReaderResult> Read(ICommandContext context, string input, IServiceProvider services)
|
||||
{
|
||||
ulong id;
|
||||
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
public abstract class TypeReader
|
||||
{
|
||||
public abstract Task<TypeReaderResult> Read(ICommandContext context, string input);
|
||||
public abstract Task<TypeReaderResult> Read(ICommandContext context, string input, IServiceProvider services);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace Discord.Commands
|
||||
internal class UserTypeReader<T> : TypeReader
|
||||
where T : class, IUser
|
||||
{
|
||||
public override async Task<TypeReaderResult> Read(ICommandContext context, string input)
|
||||
public override async Task<TypeReaderResult> Read(ICommandContext context, string input, IServiceProvider services)
|
||||
{
|
||||
var results = new Dictionary<ulong, TypeReaderValue>();
|
||||
IReadOnlyCollection<IUser> channelUsers = (await context.Channel.GetUsersAsync(CacheMode.CacheOnly).Flatten().ConfigureAwait(false)).ToArray(); //TODO: must be a better way?
|
||||
@@ -43,8 +43,7 @@ namespace Discord.Commands
|
||||
if (index >= 0)
|
||||
{
|
||||
string username = input.Substring(0, index);
|
||||
ushort discriminator;
|
||||
if (ushort.TryParse(input.Substring(index + 1), out discriminator))
|
||||
if (ushort.TryParse(input.Substring(index + 1), out ushort discriminator))
|
||||
{
|
||||
var channelUser = channelUsers.FirstOrDefault(x => x.DiscriminatorValue == discriminator &&
|
||||
string.Equals(username, x.Username, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
Reference in New Issue
Block a user