From 25c55df3b3c79339df54178e940f488f86be0e18 Mon Sep 17 00:00:00 2001 From: Iris System Date: Sat, 31 Dec 2022 19:24:19 +1300 Subject: [PATCH] feat(bot): allow clearing embeds from proxied messages --- .../Types/Requests/WebhookMessageEditRequest.cs | 4 ++++ PluralKit.Bot/Commands/Message.cs | 15 ++++++++++----- PluralKit.Bot/Services/WebhookExecutorService.cs | 13 +++++++++---- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Myriad/Rest/Types/Requests/WebhookMessageEditRequest.cs b/Myriad/Rest/Types/Requests/WebhookMessageEditRequest.cs index f9dc68c2..e1765a6f 100644 --- a/Myriad/Rest/Types/Requests/WebhookMessageEditRequest.cs +++ b/Myriad/Rest/Types/Requests/WebhookMessageEditRequest.cs @@ -1,6 +1,7 @@ using System.Text.Json.Serialization; using Myriad.Utils; +using Myriad.Types; namespace Myriad.Rest.Types.Requests; @@ -11,4 +12,7 @@ public record WebhookMessageEditRequest [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public Optional AllowedMentions { get; init; } + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] + public Optional Embeds { get; init; } } \ No newline at end of file diff --git a/PluralKit.Bot/Commands/Message.cs b/PluralKit.Bot/Commands/Message.cs index 6cb01e84..63c4876a 100644 --- a/PluralKit.Bot/Commands/Message.cs +++ b/PluralKit.Bot/Commands/Message.cs @@ -95,9 +95,6 @@ public class ProxiedMessage if (ctx.System.Id != systemId) throw new PKError("Can't edit a message sent by a different system."); - if (!ctx.HasNext()) - throw new PKSyntaxError("You need to include the message to edit in."); - var originalMsg = await _rest.GetMessageOrNull(msg.Channel, msg.Mid); if (originalMsg == null) throw new PKError("Could not edit message."); @@ -109,7 +106,15 @@ public class ProxiedMessage // Grab the original message content and new message content var originalContent = originalMsg.Content; - var newContent = ctx.RemainderOrNull().NormalizeLineEndSpacing(); + var newContent = ctx.RemainderOrNull()?.NormalizeLineEndSpacing(); + + // Should we clear embeds? + var clearEmbeds = ctx.MatchFlag("clear-embed", "ce"); + if (clearEmbeds && newContent == null) + newContent = originalMsg.Content!; + + if (newContent == null) + throw new PKSyntaxError("You need to include the message to edit in."); // Append or prepend the new content to the original message content if needed. // If no flag is supplied, the new contents will completly overwrite the old contents @@ -124,7 +129,7 @@ public class ProxiedMessage try { var editedMsg = - await _webhookExecutor.EditWebhookMessage(msg.Channel, msg.Mid, newContent); + await _webhookExecutor.EditWebhookMessage(msg.Channel, msg.Mid, newContent, clearEmbeds); if (ctx.Guild == null) await _rest.CreateReaction(ctx.Channel.Id, ctx.Message.Id, new Emoji { Name = Emojis.Success }); diff --git a/PluralKit.Bot/Services/WebhookExecutorService.cs b/PluralKit.Bot/Services/WebhookExecutorService.cs index b98348ce..505a1f73 100644 --- a/PluralKit.Bot/Services/WebhookExecutorService.cs +++ b/PluralKit.Bot/Services/WebhookExecutorService.cs @@ -18,6 +18,7 @@ using Newtonsoft.Json.Linq; using Serilog; using PluralKit.Core; +using Myriad.Utils; namespace PluralKit.Bot; @@ -80,7 +81,7 @@ public class WebhookExecutorService return webhookMessage; } - public async Task EditWebhookMessage(ulong channelId, ulong messageId, string newContent) + public async Task EditWebhookMessage(ulong channelId, ulong messageId, string newContent, bool clearEmbeds = false) { var allowedMentions = newContent.ParseMentions() with { @@ -97,10 +98,14 @@ public class WebhookExecutorService } var webhook = await _webhookCache.GetWebhook(channelId); + var editReq = new WebhookMessageEditRequest + { + Content = newContent, + AllowedMentions = allowedMentions, + Embeds = (clearEmbeds == true ? Optional.Some(new Embed[] { }) : Optional.None()), + }; - return await _rest.EditWebhookMessage(webhook.Id, webhook.Token, messageId, - new WebhookMessageEditRequest { Content = newContent, AllowedMentions = allowedMentions }, - threadId); + return await _rest.EditWebhookMessage(webhook.Id, webhook.Token, messageId, editReq, threadId); } private async Task ExecuteWebhookInner(Webhook webhook, ProxyRequest req, bool hasRetried = false)