/
ExtensionMethods.cs
207 lines (189 loc) · 8.91 KB
/
ExtensionMethods.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using DSharpPlus.CommandsNext.Builders;
using DSharpPlus.CommandsNext.Converters;
using Microsoft.Extensions.Logging;
namespace DSharpPlus.CommandsNext;
/// <summary>
/// Defines various extensions specific to CommandsNext.
/// </summary>
public static class ExtensionMethods
{
/// <summary>
/// Enables CommandsNext module on this <see cref="DiscordClient"/>.
/// </summary>
/// <param name="client">Client to enable CommandsNext for.</param>
/// <param name="cfg">CommandsNext configuration to use.</param>
/// <returns>Created <see cref="CommandsNextExtension"/>.</returns>
public static CommandsNextExtension UseCommandsNext(this DiscordClient client, CommandsNextConfiguration cfg)
{
if (client.GetExtension<CommandsNextExtension>() != null)
{
throw new InvalidOperationException("CommandsNext is already enabled for that client.");
}
if (!Utilities.HasMessageIntents(client.Configuration.Intents))
{
client.Logger.LogCritical(CommandsNextEvents.Intents, "The CommandsNext extension is registered but there are no message intents enabled. It is highly recommended to enable them.");
}
if (!client.Configuration.Intents.HasIntent(DiscordIntents.Guilds))
{
client.Logger.LogCritical(CommandsNextEvents.Intents, "The CommandsNext extension is registered but the guilds intent is not enabled. It is highly recommended to enable it.");
}
CommandsNextExtension cnext = new(cfg);
client.AddExtension(cnext);
return cnext;
}
/// <summary>
/// Enables CommandsNext module on all shards in this <see cref="DiscordShardedClient"/>.
/// </summary>
/// <param name="client">Client to enable CommandsNext for.</param>
/// <param name="cfg">CommandsNext configuration to use.</param>
/// <returns>A dictionary of created <see cref="CommandsNextExtension"/>, indexed by shard id.</returns>
public static async Task<IReadOnlyDictionary<int, CommandsNextExtension>> UseCommandsNextAsync(this DiscordShardedClient client, CommandsNextConfiguration cfg)
{
Dictionary<int, CommandsNextExtension> modules = [];
await client.InitializeShardsAsync();
foreach (DiscordClient? shard in client.ShardClients.Select(xkvp => xkvp.Value))
{
CommandsNextExtension? cnext = shard.GetExtension<CommandsNextExtension>() ?? shard.UseCommandsNext(cfg);
modules[shard.ShardId] = cnext;
}
return new ReadOnlyDictionary<int, CommandsNextExtension>(modules);
}
/// <summary>
/// Gets the active CommandsNext module for this client.
/// </summary>
/// <param name="client">Client to get CommandsNext module from.</param>
/// <returns>The module, or null if not activated.</returns>
public static CommandsNextExtension GetCommandsNext(this DiscordClient client)
=> client.GetExtension<CommandsNextExtension>();
/// <summary>
/// Gets the active CommandsNext modules for all shards in this client.
/// </summary>
/// <param name="client">Client to get CommandsNext instances from.</param>
/// <returns>A dictionary of the modules, indexed by shard id.</returns>
public static async Task<IReadOnlyDictionary<int, CommandsNextExtension>> GetCommandsNextAsync(this DiscordShardedClient client)
{
await client.InitializeShardsAsync();
Dictionary<int, CommandsNextExtension> extensions = [];
foreach (DiscordClient? shard in client.ShardClients.Select(xkvp => xkvp.Value))
{
extensions.Add(shard.ShardId, shard.GetExtension<CommandsNextExtension>());
}
return new ReadOnlyDictionary<int, CommandsNextExtension>(extensions);
}
/// <summary>
/// Registers all commands from a given assembly. The command classes need to be public to be considered for registration.
/// </summary>
/// <param name="extensions">Extensions to register commands on.</param>
/// <param name="assembly">Assembly to register commands from.</param>
public static void RegisterCommands(this IReadOnlyDictionary<int, CommandsNextExtension> extensions, Assembly assembly)
{
foreach (CommandsNextExtension extension in extensions.Values)
{
extension.RegisterCommands(assembly);
}
}
/// <summary>
/// Registers all commands from a given command class.
/// </summary>
/// <typeparam name="T">Class which holds commands to register.</typeparam>
/// <param name="extensions">Extensions to register commands on.</param>
public static void RegisterCommands<T>(this IReadOnlyDictionary<int, CommandsNextExtension> extensions) where T : BaseCommandModule
{
foreach (CommandsNextExtension extension in extensions.Values)
{
extension.RegisterCommands<T>();
}
}
/// <summary>
/// Registers all commands from a given command class.
/// </summary>
/// <param name="extensions">Extensions to register commands on.</param>
/// <param name="t">Type of the class which holds commands to register.</param>
public static void RegisterCommands(this IReadOnlyDictionary<int, CommandsNextExtension> extensions, Type t)
{
foreach (CommandsNextExtension extension in extensions.Values)
{
extension.RegisterCommands(t);
}
}
/// <summary>
/// Builds and registers all supplied commands.
/// </summary>
/// <param name="extensions">Extensions to register commands on.</param>
/// <param name="cmds">Commands to build and register.</param>
public static void RegisterCommands(this IReadOnlyDictionary<int, CommandsNextExtension> extensions, params CommandBuilder[] cmds)
{
foreach (CommandsNextExtension extension in extensions.Values)
{
extension.RegisterCommands(cmds);
}
}
/// <summary>
/// Unregisters specified commands from CommandsNext.
/// </summary>
/// <param name="extensions">Extensions to unregister commands on.</param>
/// <param name="cmds">Commands to unregister.</param>
public static void UnregisterCommands(this IReadOnlyDictionary<int, CommandsNextExtension> extensions, params Command[] cmds)
{
foreach (CommandsNextExtension extension in extensions.Values)
{
extension.UnregisterCommands(cmds);
}
}
/// <summary>
/// Registers an argument converter for specified type.
/// </summary>
/// <typeparam name="T">Type for which to register the converter.</typeparam>
/// <param name="extensions">Extensions to register the converter on.</param>
/// <param name="converter">Converter to register.</param>
public static void RegisterConverter<T>(this IReadOnlyDictionary<int, CommandsNextExtension> extensions, IArgumentConverter<T> converter)
{
foreach (CommandsNextExtension extension in extensions.Values)
{
extension.RegisterConverter(converter);
}
}
/// <summary>
/// Unregisters an argument converter for specified type.
/// </summary>
/// <typeparam name="T">Type for which to unregister the converter.</typeparam>
/// <param name="extensions">Extensions to unregister the converter on.</param>
public static void UnregisterConverter<T>(this IReadOnlyDictionary<int, CommandsNextExtension> extensions)
{
foreach (CommandsNextExtension extension in extensions.Values)
{
extension.UnregisterConverter<T>();
}
}
/// <summary>
/// Registers a user-friendly type name.
/// </summary>
/// <typeparam name="T">Type to register the name for.</typeparam>
/// <param name="extensions">Extensions to register the name on.</param>
/// <param name="value">Name to register.</param>
public static void RegisterUserFriendlyTypeName<T>(this IReadOnlyDictionary<int, CommandsNextExtension> extensions, string value)
{
foreach (CommandsNextExtension extension in extensions.Values)
{
extension.RegisterUserFriendlyTypeName<T>(value);
}
}
/// <summary>
/// Sets the help formatter to use with the default help command.
/// </summary>
/// <typeparam name="T">Type of the formatter to use.</typeparam>
/// <param name="extensions">Extensions to set the help formatter on.</param>
public static void SetHelpFormatter<T>(this IReadOnlyDictionary<int, CommandsNextExtension> extensions) where T : BaseHelpFormatter
{
foreach (CommandsNextExtension extension in extensions.Values)
{
extension.SetHelpFormatter<T>();
}
}
}