From b5425e96607da142cb46dcd4640d0e05daacf048 Mon Sep 17 00:00:00 2001 From: SubZero0 Date: Fri, 26 Jun 2020 17:00:46 -0300 Subject: [PATCH] Add example and fix param reference --- src/Discord.Net.Commands/CommandService.cs | 9 ++++-- .../Commands/CommandService.Examples.cs | 32 +++++++++++++++++++ .../Discord.Net.Examples.csproj | 1 + 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/Discord.Net.Examples/Commands/CommandService.Examples.cs diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index ff392179e..80f9e5ce3 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -367,8 +367,13 @@ namespace Discord.Commands /// Adds a custom entity to this for the supplied /// object type. /// + /// + /// The following example adds a custom entity reader to this . + /// + /// /// The object type to be read by the . - /// + /// /// A that is a generic type definition with a single open argument /// of the to be added. /// @@ -379,7 +384,7 @@ namespace Discord.Commands /// object type. /// /// A instance for the type to be read. - /// + /// /// A that is a generic type definition with a single open argument /// of the to be added. /// diff --git a/src/Discord.Net.Examples/Commands/CommandService.Examples.cs b/src/Discord.Net.Examples/Commands/CommandService.Examples.cs new file mode 100644 index 000000000..add383fc2 --- /dev/null +++ b/src/Discord.Net.Examples/Commands/CommandService.Examples.cs @@ -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(typeof(MyUserTypeReader<>)); + } + + public class MyUserTypeReader : TypeReader + where T : class, IUser + { + public override async Task 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 + } +} diff --git a/src/Discord.Net.Examples/Discord.Net.Examples.csproj b/src/Discord.Net.Examples/Discord.Net.Examples.csproj index ec0253428..1f459f2e6 100644 --- a/src/Discord.Net.Examples/Discord.Net.Examples.csproj +++ b/src/Discord.Net.Examples/Discord.Net.Examples.csproj @@ -13,6 +13,7 @@ +