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())
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<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);
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<PKMessage?> FindRecentMessage(Context ctx)

View File

@ -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
{

View File

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