Make sure that interaction commands without changes are not being patched unnecessarily

This commit is contained in:
Daan Boerlage 2021-11-14 01:16:50 +01:00
parent 0f7f936492
commit 1b396a529c
Signed by: daan
GPG key ID: FCE070E1E4956606
2 changed files with 23 additions and 41 deletions

View file

@ -1,4 +1,5 @@
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text.Json;
using Geekbot.Core; using Geekbot.Core;
using Geekbot.Core.GlobalSettings; using Geekbot.Core.GlobalSettings;
using Geekbot.Interactions; using Geekbot.Interactions;
@ -45,23 +46,21 @@ public class InteractionRegistrarController : ControllerBase
{ {
operations.Create.Add(command); operations.Create.Add(command);
} }
else else if (!RemoteIsEqualToSource(command, existing))
{ {
operations.Update.Add(existing.Id, command); 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); operations.Remove.Add(registeredInteraction.Id);
} }
await Task.WhenAll(new[] await Remove(operations.Remove);
{ await Update(operations.Update);
Create(operations.Create), await Create(operations.Create);
Update(operations.Update),
Remove(operations.Remove)
});
return Ok(operations); return Ok(operations);
} }
@ -123,11 +122,25 @@ public class InteractionRegistrarController : ControllerBase
} }
} }
private async Task<List<RegisteredInteraction>> GetRegisteredInteractions() private async Task<List<Command>> GetRegisteredInteractions()
{ {
var httpClient = HttpAbstractions.CreateDefaultClient(); var httpClient = HttpAbstractions.CreateDefaultClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bot", _discordToken); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bot", _discordToken);
return await HttpAbstractions.Get<List<RegisteredInteraction>>(_guildCommandUri, httpClient); return await HttpAbstractions.Get<List<Command>>(_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);
} }
} }

View file

@ -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; }
}