From 7acbcfdfa5c0b6caef09a99e0acb5e751ff408ae Mon Sep 17 00:00:00 2001 From: Gradyn Wursten Date: Sat, 21 Jan 2023 03:52:37 -0700 Subject: [PATCH] #2536 Add RemoveModulesFromGuildAsync (#2537) * Add RemoveModulesFromGuildAsync * fix RemoveModulesFromGuildAsync * add docs * fix parameter name --- .../InteractionService.cs | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/Discord.Net.Interactions/InteractionService.cs b/src/Discord.Net.Interactions/InteractionService.cs index 035c6bed1..502860cd7 100644 --- a/src/Discord.Net.Interactions/InteractionService.cs +++ b/src/Discord.Net.Interactions/InteractionService.cs @@ -684,6 +684,42 @@ namespace Discord.Interactions } } + /// + /// Unregister Application Commands from modules provided in from a guild. + /// + /// The target guild. + /// Modules to be deregistered from Discord. + /// + /// A task representing the command de-registration process. The task result contains the active application commands of the target guild. + /// + public async Task> RemoveModulesFromGuildAsync(IGuild guild, params ModuleInfo[] modules) + { + if (guild is null) + throw new ArgumentNullException(nameof(guild)); + + return await RemoveModulesFromGuildAsync(guild.Id, modules).ConfigureAwait(false); + } + + /// + /// Unregister Application Commands from modules provided in from a guild. + /// + /// The target guild ID. + /// Modules to be deregistered from Discord. + /// + /// A task representing the command de-registration process. The task result contains the active application commands of the target guild. + /// + public async Task> RemoveModulesFromGuildAsync(ulong guildId, params ModuleInfo[] modules) + { + EnsureClientReady(); + + var exclude = modules.SelectMany(x => x.ToApplicationCommandProps(true)).ToList(); + var existing = await RestClient.GetGuildApplicationCommands(guildId).ConfigureAwait(false); + + var props = existing.Where(x => !exclude.Any(y => y.Name.IsSpecified && x.Name == y.Name.Value)).Select(x => x.ToApplicationCommandProps()); + + return await RestClient.BulkOverwriteGuildCommands(props.ToArray(), guildId).ConfigureAwait(false); + } + private bool RemoveModuleInternal (ModuleInfo moduleInfo) { if (!_moduleDefs.Remove(moduleInfo))