Browse Source

Add example and fix param reference

pull/1487/head
SubZero0 5 years ago
parent
commit
b5425e9660
3 changed files with 40 additions and 2 deletions
  1. +7
    -2
      src/Discord.Net.Commands/CommandService.cs
  2. +32
    -0
      src/Discord.Net.Examples/Commands/CommandService.Examples.cs
  3. +1
    -0
      src/Discord.Net.Examples/Discord.Net.Examples.csproj

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

@@ -367,8 +367,13 @@ namespace Discord.Commands
/// Adds a custom entity <see cref="TypeReader" /> to this <see cref="CommandService" /> for the supplied
/// object type.
/// </summary>
/// <example>
/// <para>The following example adds a custom entity reader to this <see cref="CommandService"/>.</para>
/// <code language="cs" region="AddEntityTypeReader"
/// source="..\..\..\Discord.Net.Examples\Commands\CommandService.Examples.cs" />
/// </example>
/// <typeparam name="T">The object type to be read by the <see cref="TypeReader"/>.</typeparam>
/// <param name="typeReaderType">
/// <param name="typeReaderGenericType">
/// A <see cref="Type" /> that is a generic type definition with a single open argument
/// of the <see cref="TypeReader" /> to be added.
/// </param>
@@ -379,7 +384,7 @@ namespace Discord.Commands
/// object type.
/// </summary>
/// <param name="type">A <see cref="Type" /> instance for the type to be read.</param>
/// <param name="typeReaderType">
/// <param name="typeReaderGenericType">
/// A <see cref="Type" /> that is a generic type definition with a single open argument
/// of the <see cref="TypeReader" /> to be added.
/// </param>


+ 32
- 0
src/Discord.Net.Examples/Commands/CommandService.Examples.cs View File

@@ -0,0 +1,32 @@
using Discord.Commands;
using JetBrains.Annotations;
using System;
using System.Threading.Tasks;

namespace Discord.Net.Examples.Commands
{
[PublicAPI]
internal class CommandServiceExamples
{
#region AddEntityTypeReader

public void AddCustomEntityReader(CommandService commandService)
{
commandService.AddEntityTypeReader<IUser>(typeof(MyUserTypeReader<>));
}

public class MyUserTypeReader<T> : TypeReader
where T : class, IUser
{
public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
{
if (ulong.TryParse(input, out var id))
return ((await context.Client.GetUserAsync(id)) is T user)
? TypeReaderResult.FromSuccess(user)
: TypeReaderResult.FromError(CommandError.ObjectNotFound, "User not found.");
return TypeReaderResult.FromError(CommandError.ParseFailed, "Couldn't parse input to ulong.");
}

#endregion
}
}

+ 1
- 0
src/Discord.Net.Examples/Discord.Net.Examples.csproj View File

@@ -13,6 +13,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Discord.Net.Commands\Discord.Net.Commands.csproj" />
<ProjectReference Include="..\Discord.Net.Core\Discord.Net.Core.csproj" />
<ProjectReference Include="..\Discord.Net.WebSocket\Discord.Net.WebSocket.csproj" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />


Loading…
Cancel
Save