Browse Source

Document exposed TypeReaders

pull/1161/head
Still Hsu 7 years ago
parent
commit
f27d659ebe
No known key found for this signature in database GPG Key ID: 8601A145FDA95209
5 changed files with 37 additions and 8 deletions
  1. +6
    -2
      src/Discord.Net.Commands/Readers/ChannelTypeReader.cs
  2. +6
    -3
      src/Discord.Net.Commands/Readers/MessageTypeReader.cs
  3. +5
    -0
      src/Discord.Net.Commands/Readers/RoleTypeReader.cs
  4. +13
    -1
      src/Discord.Net.Commands/Readers/TypeReader.cs
  5. +7
    -2
      src/Discord.Net.Commands/Readers/UserTypeReader.cs

+ 6
- 2
src/Discord.Net.Commands/Readers/ChannelTypeReader.cs View File

@@ -6,19 +6,23 @@ using System.Threading.Tasks;

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
where T : class, IChannel
{
/// <inheritdoc />
public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
{
if (context.Guild != null)
{
var results = new Dictionary<ulong, TypeReaderValue>();
var channels = await context.Guild.GetChannelsAsync(CacheMode.CacheOnly).ConfigureAwait(false);
ulong id;

//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);

//By Id (0.9)


+ 6
- 3
src/Discord.Net.Commands/Readers/MessageTypeReader.cs View File

@@ -4,15 +4,18 @@ using System.Threading.Tasks;

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
where T : class, IMessage
{
/// <inheritdoc />
public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
{
ulong id;

//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)
return TypeReaderResult.FromSuccess(msg);


+ 5
- 0
src/Discord.Net.Commands/Readers/RoleTypeReader.cs View File

@@ -6,9 +6,14 @@ using System.Threading.Tasks;

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
where T : class, IRole
{
/// <inheritdoc />
public override Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
{
if (context.Guild != null)


+ 13
- 1
src/Discord.Net.Commands/Readers/TypeReader.cs View File

@@ -1,10 +1,22 @@
using System;
using System;
using System.Threading.Tasks;

namespace Discord.Commands
{
/// <summary>
/// Defines a reader class that parses user input into a specified type.
/// </summary>
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);
}
}

+ 7
- 2
src/Discord.Net.Commands/Readers/UserTypeReader.cs View File

@@ -7,9 +7,14 @@ using System.Threading.Tasks;

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
where T : class, IUser
{
/// <inheritdoc />
public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
{
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))
.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)


Loading…
Cancel
Save