From a8727f474b3ce3c863991a8d8488dba8d50d2b00 Mon Sep 17 00:00:00 2001 From: spiral Date: Tue, 27 Jul 2021 11:39:37 -0400 Subject: [PATCH] Allow any linked account to edit/delete messages sent by the same system Closes #375. --- PluralKit.Bot/Commands/MessageEdit.cs | 35 ++++++++++++--------- PluralKit.Bot/Handlers/ReactionAdded.cs | 5 ++- PluralKit.Bot/Services/LogChannelService.cs | 11 +++---- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/PluralKit.Bot/Commands/MessageEdit.cs b/PluralKit.Bot/Commands/MessageEdit.cs index 85987e97..0a08ee58 100644 --- a/PluralKit.Bot/Commands/MessageEdit.cs +++ b/PluralKit.Bot/Commands/MessageEdit.cs @@ -38,16 +38,16 @@ namespace PluralKit.Bot if (!ctx.HasNext()) throw new PKSyntaxError("You need to include the message to edit in."); - if (ctx.Author.Id != msg.Sender) + if (ctx.System.Id != msg.System.Id) throw new PKError("Can't edit a message sent from a different account."); var newContent = ctx.RemainderOrNull(); - var originalMsg = await _rest.GetMessage(msg.Channel, msg.Mid); + var originalMsg = await _rest.GetMessage(msg.Message.Channel, msg.Message.Mid); try { - await _webhookExecutor.EditWebhookMessage(msg.Channel, msg.Mid, newContent); + await _webhookExecutor.EditWebhookMessage(msg.Message.Channel, msg.Message.Mid, newContent); if (ctx.Guild == null) await _rest.CreateReaction(ctx.Channel.Id, ctx.Message.Id, new() { Name = Emojis.Success }); @@ -63,27 +63,32 @@ namespace PluralKit.Bot } } - private async Task GetMessageToEdit(Context ctx) + private async Task GetMessageToEdit(Context ctx) { + await using var conn = await _db.Obtain(); + FullMessage? msg = null; + var referencedMessage = ctx.MatchMessage(false); if (referencedMessage != null) { - await using var conn = await _db.Obtain(); - var msg = await _repo.GetMessage(conn, referencedMessage.Value); + msg = await _repo.GetMessage(conn, referencedMessage.Value); if (msg == null) throw new PKError("This is not a message proxied by PluralKit."); - - return msg.Message; } - if (ctx.Guild == null) - throw new PKError("You must use a message link to edit messages in DMs."); + if (msg == null) + { + if (ctx.Guild == null) + throw new PKError("You must use a message link to edit messages in DMs."); - var recent = await FindRecentMessage(ctx); - if (recent == null) - throw new PKError("Could not find a recent message to edit."); - - return recent; + var recent = await FindRecentMessage(ctx); + if (recent == null) + throw new PKError("Could not find a recent message to edit."); + + msg = await _repo.GetMessage(conn, recent.Mid); + } + + return msg; } private async Task FindRecentMessage(Context ctx) diff --git a/PluralKit.Bot/Handlers/ReactionAdded.cs b/PluralKit.Bot/Handlers/ReactionAdded.cs index e395577c..7c1b4bae 100644 --- a/PluralKit.Bot/Handlers/ReactionAdded.cs +++ b/PluralKit.Bot/Handlers/ReactionAdded.cs @@ -115,8 +115,11 @@ namespace PluralKit.Bot if (!_bot.PermissionsIn(evt.ChannelId).HasFlag(PermissionSet.ManageMessages)) return; + using var conn = await _db.Obtain(); + var system = await _repo.GetSystemByAccount(conn, evt.UserId); + // Can only delete your own message - if (msg.Message.Sender != evt.UserId) return; + if (msg.System.Id != system.Id) return; try { diff --git a/PluralKit.Bot/Services/LogChannelService.cs b/PluralKit.Bot/Services/LogChannelService.cs index 308d452f..b2880024 100644 --- a/PluralKit.Bot/Services/LogChannelService.cs +++ b/PluralKit.Bot/Services/LogChannelService.cs @@ -49,19 +49,18 @@ namespace PluralKit.Bot { await _rest.CreateMessage(logChannel.Id, new() {Content = url, Embed = embed}); } - public async ValueTask LogEditedMessage(MessageContext ctx, PKMessage proxy, Message trigger, Message originalMessage, string newContent) + public async ValueTask LogEditedMessage(MessageContext ctx, FullMessage proxy, Message trigger, Message originalMessage, string newContent) { - var logChannel = await GetAndCheckLogChannel(ctx, trigger, proxy); + var logChannel = await GetAndCheckLogChannel(ctx, trigger, proxy.Message); if (logChannel == null) return; - var triggerChannel = _cache.GetChannel(proxy.Channel); + var triggerChannel = _cache.GetChannel(proxy.Message.Channel); // Send embed! await using var conn = await _db.Obtain(); - var embed = _embed.CreateEditedMessageEmbed(await _repo.GetSystem(conn, ctx.SystemId.Value), - await _repo.GetMember(conn, proxy.Member), originalMessage.Id, trigger.Id, trigger.Author, newContent, originalMessage.Content, + var embed = _embed.CreateEditedMessageEmbed(proxy.System, proxy.Member, originalMessage.Id, trigger.Id, trigger.Author, newContent, originalMessage.Content, triggerChannel); - var url = $"https://discord.com/channels/{proxy.Guild.Value}/{proxy.Channel}/{proxy.Mid}"; + var url = $"https://discord.com/channels/{proxy.Message.Guild.Value}/{proxy.Message.Channel}/{proxy.Message.Mid}"; await _rest.CreateMessage(logChannel.Id, new() {Content = url, Embed = embed}); }