Allow any linked account to edit/delete messages sent by the same system

Closes #375.
This commit is contained in:
spiral 2021-07-27 11:39:37 -04:00
parent 52a7a015cd
commit a8727f474b
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
3 changed files with 29 additions and 22 deletions

View File

@ -38,16 +38,16 @@ namespace PluralKit.Bot
if (!ctx.HasNext()) if (!ctx.HasNext())
throw new PKSyntaxError("You need to include the message to edit in."); 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."); throw new PKError("Can't edit a message sent from a different account.");
var newContent = ctx.RemainderOrNull(); 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 try
{ {
await _webhookExecutor.EditWebhookMessage(msg.Channel, msg.Mid, newContent); await _webhookExecutor.EditWebhookMessage(msg.Message.Channel, msg.Message.Mid, newContent);
if (ctx.Guild == null) if (ctx.Guild == null)
await _rest.CreateReaction(ctx.Channel.Id, ctx.Message.Id, new() { Name = Emojis.Success }); await _rest.CreateReaction(ctx.Channel.Id, ctx.Message.Id, new() { Name = Emojis.Success });
@ -63,27 +63,32 @@ namespace PluralKit.Bot
} }
} }
private async Task<PKMessage> GetMessageToEdit(Context ctx) private async Task<FullMessage> GetMessageToEdit(Context ctx)
{ {
await using var conn = await _db.Obtain();
FullMessage? msg = null;
var referencedMessage = ctx.MatchMessage(false); var referencedMessage = ctx.MatchMessage(false);
if (referencedMessage != null) if (referencedMessage != null)
{ {
await using var conn = await _db.Obtain(); msg = await _repo.GetMessage(conn, referencedMessage.Value);
var msg = await _repo.GetMessage(conn, referencedMessage.Value);
if (msg == null) if (msg == null)
throw new PKError("This is not a message proxied by PluralKit."); throw new PKError("This is not a message proxied by PluralKit.");
return msg.Message;
} }
if (ctx.Guild == null) if (msg == null)
throw new PKError("You must use a message link to edit messages in DMs."); {
if (ctx.Guild == null)
throw new PKError("You must use a message link to edit messages in DMs.");
var recent = await FindRecentMessage(ctx); var recent = await FindRecentMessage(ctx);
if (recent == null) if (recent == null)
throw new PKError("Could not find a recent message to edit."); throw new PKError("Could not find a recent message to edit.");
return recent; msg = await _repo.GetMessage(conn, recent.Mid);
}
return msg;
} }
private async Task<PKMessage?> FindRecentMessage(Context ctx) private async Task<PKMessage?> FindRecentMessage(Context ctx)

View File

@ -115,8 +115,11 @@ namespace PluralKit.Bot
if (!_bot.PermissionsIn(evt.ChannelId).HasFlag(PermissionSet.ManageMessages)) if (!_bot.PermissionsIn(evt.ChannelId).HasFlag(PermissionSet.ManageMessages))
return; return;
using var conn = await _db.Obtain();
var system = await _repo.GetSystemByAccount(conn, evt.UserId);
// Can only delete your own message // Can only delete your own message
if (msg.Message.Sender != evt.UserId) return; if (msg.System.Id != system.Id) return;
try try
{ {

View File

@ -49,19 +49,18 @@ namespace PluralKit.Bot {
await _rest.CreateMessage(logChannel.Id, new() {Content = url, Embed = embed}); 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; if (logChannel == null) return;
var triggerChannel = _cache.GetChannel(proxy.Channel); var triggerChannel = _cache.GetChannel(proxy.Message.Channel);
// Send embed! // Send embed!
await using var conn = await _db.Obtain(); await using var conn = await _db.Obtain();
var embed = _embed.CreateEditedMessageEmbed(await _repo.GetSystem(conn, ctx.SystemId.Value), var embed = _embed.CreateEditedMessageEmbed(proxy.System, proxy.Member, originalMessage.Id, trigger.Id, trigger.Author, newContent, originalMessage.Content,
await _repo.GetMember(conn, proxy.Member), originalMessage.Id, trigger.Id, trigger.Author, newContent, originalMessage.Content,
triggerChannel); 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}); await _rest.CreateMessage(logChannel.Id, new() {Content = url, Embed = embed});
} }