-
-
Notifications
You must be signed in to change notification settings - Fork 739
/
Copy pathCommandService.Examples.cs
55 lines (47 loc) · 2.02 KB
/
CommandService.Examples.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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 AddCustomUserEntityReader(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
#region AddEntityTypeReader2
public void AddCustomChannelEntityReader(CommandService commandService)
{
commandService.AddEntityTypeReader<IUser>(typeof(MyUserTypeReader<>));
}
public class MyChannelTypeReader<T> : TypeReader
where T : class, IChannel
{
public override async Task<TypeReaderResult> 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
}
}