diff --git a/DisCatSharp.ApplicationCommands/ExtensionMethods.cs b/DisCatSharp.ApplicationCommands/ExtensionMethods.cs deleted file mode 100644 index 6c3164b74..000000000 --- a/DisCatSharp.ApplicationCommands/ExtensionMethods.cs +++ /dev/null @@ -1,134 +0,0 @@ -// This file is part of the DisCatSharp project. -// -// Copyright (c) 2021 AITSYS -// -// 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.Globalization; -using System.Linq; -using System.Reflection; -using System.Threading.Tasks; - -namespace DisCatSharp.ApplicationCommands -{ - /// - /// Defines various extension methods for application commands. - /// - public static class ExtensionMethods - { - /// - /// Enables application commands on this . - /// - /// Client to enable application commands for. - /// Configuration to use. - /// Created . - public static ApplicationCommandsExtension UseApplicationCommands(this DiscordClient client, - ApplicationCommandsConfiguration config = null) - { - if (client.GetExtension() != null) - throw new InvalidOperationException("Application commands are already enabled for that client."); - - var scomm = new ApplicationCommandsExtension(config); - client.AddExtension(scomm); - return scomm; - } - - /// - /// Gets the application commands module for this client. - /// - /// Client to get application commands for. - /// The module, or null if not activated. - public static ApplicationCommandsExtension GetApplicationCommands(this DiscordClient client) - => client.GetExtension(); - - /// - /// Enables application commands on this . - /// - /// Client to enable application commands on. - /// Configuration to use. - /// A dictionary of created with the key being the shard id. - public static async Task> UseApplicationCommandsAsync(this DiscordShardedClient client, ApplicationCommandsConfiguration config = null) - { - var modules = new Dictionary(); - await (Task)client.GetType().GetMethod("InitializeShardsAsync", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(client, null); - foreach (var shard in client.ShardClients.Values) - { - var scomm = shard.GetApplicationCommands(); - if (scomm == null) - scomm = shard.UseApplicationCommands(config); - - modules[shard.ShardId] = scomm; - } - - return modules; - } - - /// - /// Registers a commands class. - /// - /// The command class to register. - /// The modules to register it on. - /// The guild id to register it on. If you want global commands, leave it null. - public static void RegisterCommands(this IReadOnlyDictionary modules, ulong? guildId = null) where T : ApplicationCommandsModule - { - foreach (var module in modules.Values) - module.RegisterCommands(guildId); - } - - /// - /// Registers a command class. - /// - /// The modules to register it on. - /// The of the command class to register. - /// The guild id to register it on. If you want global commands, leave it null. - public static void RegisterCommands(this IReadOnlyDictionary modules, Type type, ulong? guildId = null) - { - foreach (var module in modules.Values) - module.RegisterCommands(type, guildId); - } - - /// - /// Gets the name from the for this enum value. - /// - /// The name. - public static string GetName(this T e) where T : IConvertible - { - if (e is Enum) - { - var type = e.GetType(); - var values = Enum.GetValues(type); - - foreach (int val in values) - { - if (val == e.ToInt32(CultureInfo.InvariantCulture)) - { - var memInfo = type.GetMember(type.GetEnumName(val)); - - return memInfo[0] - .GetCustomAttributes(typeof(ChoiceNameAttribute), false) - .FirstOrDefault() is ChoiceNameAttribute nameAttribute ? nameAttribute.Name : type.GetEnumName(val); - } - } - } - return null; - } - } -}