feat(bot): allow clearing embeds from proxied messages

This commit is contained in:
Iris System 2022-12-31 19:24:19 +13:00
parent 31ff2b7c87
commit 25c55df3b3
3 changed files with 23 additions and 9 deletions

View File

@ -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> AllowedMentions { get; init; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public Optional<Embed[]?> Embeds { get; init; }
}

View File

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

View File

@ -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<Message> EditWebhookMessage(ulong channelId, ulong messageId, string newContent)
public async Task<Message> 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<Embed[]>.Some(new Embed[] { }) : Optional<Embed[]>.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<Message> ExecuteWebhookInner(Webhook webhook, ProxyRequest req, bool hasRetried = false)