From 1b396a529cb4d15d93273c798cafc281183d2d84 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Sun, 14 Nov 2021 01:16:50 +0100 Subject: [PATCH] Make sure that interaction commands without changes are not being patched unnecessarily --- .../InteractionRegistrarController.cs | 33 +++++++++++++------ .../Model/RegisteredInteraction.cs | 31 ----------------- 2 files changed, 23 insertions(+), 41 deletions(-) delete mode 100644 src/Web/Controllers/Interactions/Model/RegisteredInteraction.cs diff --git a/src/Web/Controllers/Interactions/InteractionRegistrarController.cs b/src/Web/Controllers/Interactions/InteractionRegistrarController.cs index fc29e14..9098f2b 100644 --- a/src/Web/Controllers/Interactions/InteractionRegistrarController.cs +++ b/src/Web/Controllers/Interactions/InteractionRegistrarController.cs @@ -1,4 +1,5 @@ using System.Net.Http.Headers; +using System.Text.Json; using Geekbot.Core; using Geekbot.Core.GlobalSettings; using Geekbot.Interactions; @@ -45,23 +46,21 @@ public class InteractionRegistrarController : ControllerBase { operations.Create.Add(command); } - else + else if (!RemoteIsEqualToSource(command, existing)) { operations.Update.Add(existing.Id, command); } } - foreach (var registeredInteraction in registeredInteractions.Where(registeredInteraction => !_interactionCommandManager.CommandsInfo.Values.Any(c => c.Name == registeredInteraction.Name))) + foreach (var registeredInteraction in registeredInteractions.Where(registeredInteraction => _interactionCommandManager.CommandsInfo.Values.All(c => c.Name != registeredInteraction.Name))) { operations.Remove.Add(registeredInteraction.Id); } - await Task.WhenAll(new[] - { - Create(operations.Create), - Update(operations.Update), - Remove(operations.Remove) - }); + await Remove(operations.Remove); + await Update(operations.Update); + await Create(operations.Create); + return Ok(operations); } @@ -123,11 +122,25 @@ public class InteractionRegistrarController : ControllerBase } } - private async Task> GetRegisteredInteractions() + private async Task> GetRegisteredInteractions() { var httpClient = HttpAbstractions.CreateDefaultClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bot", _discordToken); - return await HttpAbstractions.Get>(_guildCommandUri, httpClient); + return await HttpAbstractions.Get>(_guildCommandUri, httpClient); + } + + private bool RemoteIsEqualToSource(Command source, Command remote) + { + var enrichedSource = source with + { + Id = remote.Id, + Version = remote.Version, + ApplicationId = remote.ApplicationId, + GuildId = remote.GuildId, + Description = source.Description ?? string.Empty, + }; + + return JsonSerializer.Serialize(enrichedSource) == JsonSerializer.Serialize(remote); } } \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/RegisteredInteraction.cs b/src/Web/Controllers/Interactions/Model/RegisteredInteraction.cs deleted file mode 100644 index 579ee98..0000000 --- a/src/Web/Controllers/Interactions/Model/RegisteredInteraction.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System.Text.Json.Serialization; -using Geekbot.Interactions.Request; - -namespace Geekbot.Web.Controllers.Interactions.Model; - -public record RegisteredInteraction -{ - [JsonPropertyName("id")] - public string Id { get; set; } - - [JsonPropertyName("application_id")] - public string ApplicationId { get; set; } - - [JsonPropertyName("name")] - public string Name { get; set; } - - [JsonPropertyName("description")] - public string Description { get; set; } - - [JsonPropertyName("version")] - public string Version { get; set; } - - [JsonPropertyName("default_permission")] - public bool DefaultPermission { get; set; } - - [JsonPropertyName("type")] - public InteractionType Type { get; set; } - - [JsonPropertyName("guild_id")] - public string GuildId { get; set; } -} \ No newline at end of file