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

@@ -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);
}