diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index e61f14a5b..4ca26c82f 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -82,8 +82,8 @@ namespace Discord.Commands /// Represents all entity type reader s loaded within . /// /// - /// A that the key is the object type to be read by the - /// and the element is the type of the generic definition. + /// A ; the key is the object type to be read by the , + /// while the element is the type of the generic definition. /// public ILookup EntityTypeReaders => _userEntityTypeReaders.SelectMany(x => x.Value.Select(y => new { x.Key, TypeReaderType = y })).ToLookup(x => x.Key, y => y.TypeReaderType); @@ -368,13 +368,10 @@ namespace Discord.Commands /// /// The following example adds a custom entity reader to this . /// + /// source="..\Discord.Net.Examples\Commands\CommandService.Examples.cs" /> /// /// 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. - /// + /// A generic type definition (with one open argument) of the . public void AddEntityTypeReader(Type typeReaderGenericType) => AddEntityTypeReader(typeof(T), typeReaderGenericType); /// @@ -382,10 +379,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. - /// + /// A generic type definition (with one open argument) of the . public void AddEntityTypeReader(Type type, Type typeReaderGenericType) { if (!typeReaderGenericType.IsGenericTypeDefinition) @@ -406,6 +400,11 @@ namespace Discord.Commands /// If is a , a nullable will /// also be added. /// + /// + /// The following example adds a custom entity reader to this . + /// + /// /// The object type to be read by the . /// An instance 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 index a094ebb3d..ca656aaf9 100644 --- a/src/Discord.Net.Examples/Commands/CommandService.Examples.cs +++ b/src/Discord.Net.Examples/Commands/CommandService.Examples.cs @@ -10,7 +10,7 @@ namespace Discord.Net.Examples.Commands { #region AddEntityTypeReader - public void AddCustomEntityReader(CommandService commandService) + public void AddCustomUserEntityReader(CommandService commandService) { commandService.AddEntityTypeReader(typeof(MyUserTypeReader<>)); } @@ -29,5 +29,27 @@ namespace Discord.Net.Examples.Commands } #endregion + + #region AddEntityTypeReader2 + + public void AddCustomChannelEntityReader(CommandService commandService) + { + commandService.AddEntityTypeReader(typeof(MyUserTypeReader<>)); + } + + public class MyChannelTypeReader : TypeReader + where T : class, IChannel + { + public override async Task ReadAsync(ICommandContext context, string input, IServiceProvider services) + { + if (ulong.TryParse(input, out var id)) + return ((await context.Client.GetChannelAsync(id)) is T channel) + ? TypeReaderResult.FromSuccess(channel) + : TypeReaderResult.FromError(CommandError.ObjectNotFound, "Channel not found."); + return TypeReaderResult.FromError(CommandError.ParseFailed, "Couldn't parse input to ulong."); + } + } + + #endregion } }