@@ -6,19 +6,23 @@ using System.Threading.Tasks; | |||||
namespace Discord.Commands | namespace Discord.Commands | ||||
{ | { | ||||
/// <summary> | |||||
/// A <see cref="TypeReader"/> for parsing objects implementing <see cref="IChannel"/>. | |||||
/// </summary> | |||||
/// <typeparam name="T">The type to be checked; must implement <see cref="IChannel"/>.</typeparam> | |||||
public class ChannelTypeReader<T> : TypeReader | public class ChannelTypeReader<T> : TypeReader | ||||
where T : class, IChannel | where T : class, IChannel | ||||
{ | { | ||||
/// <inheritdoc /> | |||||
public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) | public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) | ||||
{ | { | ||||
if (context.Guild != null) | if (context.Guild != null) | ||||
{ | { | ||||
var results = new Dictionary<ulong, TypeReaderValue>(); | var results = new Dictionary<ulong, TypeReaderValue>(); | ||||
var channels = await context.Guild.GetChannelsAsync(CacheMode.CacheOnly).ConfigureAwait(false); | var channels = await context.Guild.GetChannelsAsync(CacheMode.CacheOnly).ConfigureAwait(false); | ||||
ulong id; | |||||
//By Mention (1.0) | //By Mention (1.0) | ||||
if (MentionUtils.TryParseChannel(input, out id)) | |||||
if (MentionUtils.TryParseChannel(input, out ulong id)) | |||||
AddResult(results, await context.Guild.GetChannelAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) as T, 1.00f); | AddResult(results, await context.Guild.GetChannelAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) as T, 1.00f); | ||||
//By Id (0.9) | //By Id (0.9) | ||||
@@ -4,15 +4,18 @@ using System.Threading.Tasks; | |||||
namespace Discord.Commands | namespace Discord.Commands | ||||
{ | { | ||||
/// <summary> | |||||
/// A <see cref="TypeReader"/> for parsing objects implementing <see cref="IMessage"/>. | |||||
/// </summary> | |||||
/// <typeparam name="T">The type to be checked; must implement <see cref="IMessage"/>.</typeparam> | |||||
public class MessageTypeReader<T> : TypeReader | public class MessageTypeReader<T> : TypeReader | ||||
where T : class, IMessage | where T : class, IMessage | ||||
{ | { | ||||
/// <inheritdoc /> | |||||
public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) | public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) | ||||
{ | { | ||||
ulong id; | |||||
//By Id (1.0) | //By Id (1.0) | ||||
if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id)) | |||||
if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out ulong id)) | |||||
{ | { | ||||
if (await context.Channel.GetMessageAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) is T msg) | if (await context.Channel.GetMessageAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) is T msg) | ||||
return TypeReaderResult.FromSuccess(msg); | return TypeReaderResult.FromSuccess(msg); | ||||
@@ -6,9 +6,14 @@ using System.Threading.Tasks; | |||||
namespace Discord.Commands | namespace Discord.Commands | ||||
{ | { | ||||
/// <summary> | |||||
/// A <see cref="TypeReader"/> for parsing objects implementing <see cref="IRole"/>. | |||||
/// </summary> | |||||
/// <typeparam name="T">The type to be checked; must implement <see cref="IRole"/>.</typeparam> | |||||
public class RoleTypeReader<T> : TypeReader | public class RoleTypeReader<T> : TypeReader | ||||
where T : class, IRole | where T : class, IRole | ||||
{ | { | ||||
/// <inheritdoc /> | |||||
public override Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) | public override Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) | ||||
{ | { | ||||
if (context.Guild != null) | if (context.Guild != null) | ||||
@@ -1,10 +1,22 @@ | |||||
using System; | |||||
using System; | |||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
namespace Discord.Commands | namespace Discord.Commands | ||||
{ | { | ||||
/// <summary> | |||||
/// Defines a reader class that parses user input into a specified type. | |||||
/// </summary> | |||||
public abstract class TypeReader | public abstract class TypeReader | ||||
{ | { | ||||
/// <summary> | |||||
/// Attempts to parse the <paramref name="input"/> into the desired type. | |||||
/// </summary> | |||||
/// <param name="context">The context of the command.</param> | |||||
/// <param name="input">The raw input of the command.</param> | |||||
/// <param name="services">The service collection used for dependency injection.</param> | |||||
/// <returns> | |||||
/// An awaitable Task containing the result of the type reading process. | |||||
/// </returns> | |||||
public abstract Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services); | public abstract Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services); | ||||
} | } | ||||
} | } |
@@ -7,9 +7,14 @@ using System.Threading.Tasks; | |||||
namespace Discord.Commands | namespace Discord.Commands | ||||
{ | { | ||||
/// <summary> | |||||
/// A <see cref="TypeReader"/> for parsing objects implementing <see cref="IUser"/>. | |||||
/// </summary> | |||||
/// <typeparam name="T">The type to be checked; must implement <see cref="IUser"/>.</typeparam> | |||||
public class UserTypeReader<T> : TypeReader | public class UserTypeReader<T> : TypeReader | ||||
where T : class, IUser | where T : class, IUser | ||||
{ | { | ||||
/// <inheritdoc /> | |||||
public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) | public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) | ||||
{ | { | ||||
var results = new Dictionary<ulong, TypeReaderValue>(); | var results = new Dictionary<ulong, TypeReaderValue>(); | ||||
@@ -72,8 +77,8 @@ namespace Discord.Commands | |||||
.ForEachAsync(channelUser => AddResult(results, channelUser as T, (channelUser as IGuildUser).Nickname == input ? 0.65f : 0.55f)) | .ForEachAsync(channelUser => AddResult(results, channelUser as T, (channelUser as IGuildUser).Nickname == input ? 0.65f : 0.55f)) | ||||
.ConfigureAwait(false); | .ConfigureAwait(false); | ||||
foreach (var guildUser in guildUsers.Where(x => string.Equals(input, (x as IGuildUser).Nickname, StringComparison.OrdinalIgnoreCase))) | |||||
AddResult(results, guildUser as T, (guildUser as IGuildUser).Nickname == input ? 0.60f : 0.50f); | |||||
foreach (var guildUser in guildUsers.Where(x => string.Equals(input, x.Nickname, StringComparison.OrdinalIgnoreCase))) | |||||
AddResult(results, guildUser as T, guildUser.Nickname == input ? 0.60f : 0.50f); | |||||
} | } | ||||
if (results.Count > 0) | if (results.Count > 0) | ||||