feat: delete command messages with pk;msg -delete
This commit is contained in:
@@ -104,7 +104,7 @@ namespace PluralKit.Bot
|
||||
{
|
||||
// Sensitive information that might want to be deleted by :x: reaction is typically in an embed format (member cards, for example)
|
||||
// This may need to be changed at some point but works well enough for now
|
||||
await _commandMessageService.RegisterMessage(msg.Id, Author.Id);
|
||||
await _commandMessageService.RegisterMessage(msg.Id, msg.ChannelId, Author.Id);
|
||||
}
|
||||
|
||||
return msg;
|
||||
|
||||
@@ -127,10 +127,21 @@ namespace PluralKit.Bot
|
||||
throw new PKSyntaxError($"Could not parse {ctx.PeekArgument().AsCode()} as a message ID or link.");
|
||||
}
|
||||
|
||||
var message = await _db.Execute(c => _repo.GetMessage(c, messageId.Value));
|
||||
if (message == null) throw Errors.MessageNotFound(messageId.Value);
|
||||
var isDelete = ctx.Match("delete") || ctx.MatchFlag("delete");
|
||||
|
||||
if (ctx.Match("delete") || ctx.MatchFlag("delete"))
|
||||
var message = await _db.Execute(c => _repo.GetMessage(c, messageId.Value));
|
||||
if (message == null)
|
||||
{
|
||||
if (isDelete)
|
||||
{
|
||||
await DeleteCommandMessage(ctx, messageId.Value);
|
||||
return;
|
||||
}
|
||||
else
|
||||
throw Errors.MessageNotFound(messageId.Value);
|
||||
}
|
||||
|
||||
if (isDelete)
|
||||
{
|
||||
if (message.System.Id != ctx.System.Id)
|
||||
throw new PKError("You can only delete your own messages.");
|
||||
@@ -154,5 +165,22 @@ namespace PluralKit.Bot
|
||||
|
||||
await ctx.Reply(embed: await _embeds.CreateMessageInfoEmbed(message));
|
||||
}
|
||||
|
||||
private async Task DeleteCommandMessage(Context ctx, ulong messageId)
|
||||
{
|
||||
var message = await _db.Execute(conn => _repo.GetCommandMessage(conn, messageId));
|
||||
if (message == null)
|
||||
throw Errors.MessageNotFound(messageId);
|
||||
|
||||
if (message.AuthorId != ctx.Author.Id)
|
||||
throw new PKError("You can only delete command messages queried by this account.");
|
||||
|
||||
await ctx.Rest.DeleteMessage(message.ChannelId, message.MessageId);
|
||||
|
||||
if (ctx.Guild != null)
|
||||
await ctx.Rest.DeleteMessage(ctx.Message);
|
||||
else
|
||||
await ctx.Rest.CreateReaction(ctx.Message.ChannelId, ctx.Message.Id, new() { Name = Emojis.Success });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,10 +25,10 @@ namespace PluralKit.Bot
|
||||
_logger = logger.ForContext<CommandMessageService>();
|
||||
}
|
||||
|
||||
public async Task RegisterMessage(ulong messageId, ulong authorId)
|
||||
public async Task RegisterMessage(ulong messageId, ulong channelId, ulong authorId)
|
||||
{
|
||||
_logger.Debug("Registering command response {MessageId} from author {AuthorId}", messageId, authorId);
|
||||
await _db.Execute(conn => _repo.SaveCommandMessage(conn, messageId, authorId));
|
||||
_logger.Debug("Registering command response {MessageId} from author {AuthorId} in {ChannelId}", messageId, authorId, channelId);
|
||||
await _db.Execute(conn => _repo.SaveCommandMessage(conn, messageId, channelId, authorId));
|
||||
}
|
||||
|
||||
public async Task<CommandMessage?> GetCommandMessage(IPKConnection conn, ulong messageId)
|
||||
|
||||
Reference in New Issue
Block a user