forked from DSharpPlus/DSharpPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensionMethods.cs
211 lines (191 loc) · 10.2 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
208
209
210
211
// This file is part of the DSharpPlus project.
//
// Copyright (c) 2015 Mike Santiago
// Copyright (c) 2016-2022 DSharpPlus Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
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.");
var cnext = new CommandsNextExtension(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)
{
var modules = new Dictionary<int, CommandsNextExtension>();
await client.InitializeShardsAsync().ConfigureAwait(false);
foreach (var shard in client.ShardClients.Select(xkvp => xkvp.Value))
{
var cnext = shard.GetExtension<CommandsNextExtension>();
if (cnext == null)
cnext = 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().ConfigureAwait(false);
var extensions = new Dictionary<int, CommandsNextExtension>();
foreach (var 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 (var 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 (var 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 (var 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 (var 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 (var 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 (var 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 (var 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 (var 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 (var extension in extensions.Values)
extension.SetHelpFormatter<T>();
}
}
}