-
-
Notifications
You must be signed in to change notification settings - Fork 319
avoid bulk overwriting global commands #2115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0b850e6
c2c8be4
f182880
5dfd9d1
15b7fc1
01f30c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| using System.Threading.Tasks; | ||
|
|
||
| using DSharpPlus.Entities; | ||
|
|
||
| namespace DSharpPlus.Commands.Processors.SlashCommands.RemoteRecordRetentionPolicies; | ||
|
|
||
| internal sealed class DefaultRemoteRecordRetentionPolicy : IRemoteRecordRetentionPolicy | ||
| { | ||
| public Task<bool> CheckDeletionStatusAsync(DiscordApplicationCommand command) | ||
| => Task.FromResult(command.Type != DiscordApplicationCommandType.ActivityEntryPoint); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using System.Threading.Tasks; | ||
|
|
||
| using DSharpPlus.Entities; | ||
|
|
||
| namespace DSharpPlus.Commands.Processors.SlashCommands.RemoteRecordRetentionPolicies; | ||
|
|
||
| /// <summary> | ||
| /// Provides a means to customize when and which application commands get deleted from your bot. | ||
| /// </summary> | ||
| public interface IRemoteRecordRetentionPolicy | ||
| { | ||
| /// <summary> | ||
| /// Returns a value indicating whether the application command should be deleted or not. | ||
| /// </summary> | ||
| public Task<bool> CheckDeletionStatusAsync(DiscordApplicationCommand command); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
|
|
||
| using DSharpPlus.Entities; | ||
| using DSharpPlus.Net.Models; | ||
|
|
||
| namespace DSharpPlus.Commands.Processors.SlashCommands; | ||
|
|
||
| public sealed partial class SlashCommandProcessor | ||
| { | ||
| private async Task<IReadOnlyList<DiscordApplicationCommand>> VerifyAndUpdateRemoteCommandsAsync | ||
| ( | ||
| IReadOnlyList<DiscordApplicationCommand> local, | ||
| IReadOnlyList<DiscordApplicationCommand> remoteCommands | ||
| ) | ||
| { | ||
| int added = 0, edited = 0, unchanged = 0, deleted = 0; | ||
| List<DiscordApplicationCommand> updated = []; | ||
| List<DiscordApplicationCommand> remoteTracking = new(remoteCommands); | ||
|
|
||
| foreach (DiscordApplicationCommand command in local) | ||
| { | ||
| DiscordApplicationCommand remote; | ||
|
|
||
| if ((remote = remoteTracking.SingleOrDefault(x => x.Name == command.Name)) is not null) | ||
akiraveliara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| if (command.WeakEquals(remote)) | ||
| { | ||
| unchanged++; | ||
| updated.Add(command); | ||
| remoteTracking.Remove(remote); | ||
| continue; | ||
| } | ||
| else | ||
| { | ||
| edited++; | ||
| updated.Add(await ModifyGlobalCommandAsync(remote.Id, command)); | ||
| remoteTracking.Remove(remote); | ||
| continue; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| added++; | ||
| updated.Add(await CreateGlobalCommandAsync(command)); | ||
| } | ||
| } | ||
|
|
||
| deleted = remoteTracking.Count; | ||
|
|
||
| foreach (DiscordApplicationCommand toDelete in remoteTracking) | ||
| { | ||
| if (await this.Configuration.RemoteRecordRetentionPolicy.CheckDeletionStatusAsync(toDelete)) | ||
| { | ||
| await DeleteGlobalCommandAsync(toDelete); | ||
| } | ||
| } | ||
|
|
||
| if (added != 0 || edited != 0 || deleted != 0) | ||
| { | ||
| SlashLogging.detectedCommandRecordChanges(this.logger, unchanged, added, edited, deleted, null); | ||
| } | ||
| else | ||
| { | ||
| SlashLogging.commandRecordsUnchanged(this.logger, null); | ||
| } | ||
|
|
||
| return updated; | ||
| } | ||
|
|
||
| private async ValueTask<DiscordApplicationCommand> CreateGlobalCommandAsync(DiscordApplicationCommand command) | ||
| { | ||
| return this.extension.DebugGuildId == 0 | ||
| ? await this.extension.Client.CreateGlobalApplicationCommandAsync(command) | ||
| : await this.extension.Client.CreateGuildApplicationCommandAsync(this.extension.DebugGuildId, command); | ||
| } | ||
|
|
||
| #pragma warning disable IDE0046 | ||
| private async ValueTask<DiscordApplicationCommand> ModifyGlobalCommandAsync(ulong id, DiscordApplicationCommand command) | ||
| { | ||
| if (this.extension.DebugGuildId == 0) | ||
| { | ||
| return await this.extension.Client.EditGlobalApplicationCommandAsync(id, x => CopyToEditModel(command, x)); | ||
| } | ||
| else | ||
| { | ||
| return await this.extension.Client.EditGuildApplicationCommandAsync | ||
| ( | ||
| this.extension.DebugGuildId, | ||
| id, | ||
| x => CopyToEditModel(command, x) | ||
| ); | ||
| } | ||
| } | ||
| #pragma warning restore IDE0046 | ||
|
|
||
| private async ValueTask DeleteGlobalCommandAsync(DiscordApplicationCommand command) | ||
| { | ||
| if (this.extension.DebugGuildId == 0) | ||
| { | ||
| await this.extension.Client.DeleteGlobalApplicationCommandAsync(command.Id); | ||
| } | ||
| else | ||
| { | ||
| await this.extension.Client.DeleteGuildApplicationCommandAsync(this.extension.DebugGuildId, command.Id); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code stylistically, should be the same as in
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't be, because this one doesn't return a value it can't be a ternary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my bad then, overlooked it sorry |
||
| } | ||
|
|
||
| private static void CopyToEditModel(DiscordApplicationCommand command, ApplicationCommandEditModel editModel) | ||
| { | ||
| editModel.AllowDMUsage = command.AllowDMUsage.HasValue | ||
| ? new(command.AllowDMUsage.Value) | ||
| : Optional.FromNoValue<bool>(); | ||
| editModel.DefaultMemberPermissions = command.DefaultMemberPermissions; | ||
| editModel.Description = command.Description; | ||
| editModel.NameLocalizations = command.NameLocalizations; | ||
| editModel.DescriptionLocalizations = command.DescriptionLocalizations; | ||
| editModel.IntegrationTypes = command.IntegrationTypes is not null | ||
| ? new(command.IntegrationTypes) | ||
| : Optional.FromNoValue<IEnumerable<DiscordApplicationIntegrationType>>(); | ||
| editModel.AllowedContexts = command.Contexts is not null | ||
| ? new(command.Contexts) | ||
| : Optional.FromNoValue<IEnumerable<DiscordInteractionContextType>>(); | ||
| editModel.NSFW = command.NSFW; | ||
| editModel.Options = command.Options is not null | ||
| ? new(command.Options) | ||
| : Optional.FromNoValue<IReadOnlyList<DiscordApplicationCommandOption>>(); | ||
| } | ||
akiraveliara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.