feat: delete command messages with pk;msg -delete
This commit is contained in:
parent
fa66fbe247
commit
0517c76abf
PluralKit.Bot
PluralKit.Core/Database
@ -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)
|
// 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
|
// 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;
|
return msg;
|
||||||
|
@ -127,10 +127,21 @@ namespace PluralKit.Bot
|
|||||||
throw new PKSyntaxError($"Could not parse {ctx.PeekArgument().AsCode()} as a message ID or link.");
|
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));
|
var isDelete = ctx.Match("delete") || ctx.MatchFlag("delete");
|
||||||
if (message == null) throw Errors.MessageNotFound(messageId.Value);
|
|
||||||
|
|
||||||
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)
|
if (message.System.Id != ctx.System.Id)
|
||||||
throw new PKError("You can only delete your own messages.");
|
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));
|
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>();
|
_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);
|
_logger.Debug("Registering command response {MessageId} from author {AuthorId} in {ChannelId}", messageId, authorId, channelId);
|
||||||
await _db.Execute(conn => _repo.SaveCommandMessage(conn, messageId, authorId));
|
await _db.Execute(conn => _repo.SaveCommandMessage(conn, messageId, channelId, authorId));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<CommandMessage?> GetCommandMessage(IPKConnection conn, ulong messageId)
|
public async Task<CommandMessage?> GetCommandMessage(IPKConnection conn, ulong messageId)
|
||||||
|
8
PluralKit.Core/Database/Migrations/17.sql
Normal file
8
PluralKit.Core/Database/Migrations/17.sql
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
-- schema version 17: 2021-09-26 --
|
||||||
|
-- add channel_id to command message table
|
||||||
|
|
||||||
|
alter table command_messages add column channel_id bigint;
|
||||||
|
update command_messages set channel_id = 0;
|
||||||
|
alter table command_messages alter column channel_id set not null;
|
||||||
|
|
||||||
|
update info set schema_version = 17;
|
@ -6,9 +6,9 @@ namespace PluralKit.Core
|
|||||||
{
|
{
|
||||||
public partial class ModelRepository
|
public partial class ModelRepository
|
||||||
{
|
{
|
||||||
public Task SaveCommandMessage(IPKConnection conn, ulong messageId, ulong authorId) =>
|
public Task SaveCommandMessage(IPKConnection conn, ulong messageId, ulong channelId, ulong authorId) =>
|
||||||
conn.QueryAsync("insert into command_messages (message_id, author_id) values (@Message, @Author)",
|
conn.QueryAsync("insert into command_messages (message_id, channel_id, author_id) values (@Message, @Channel, @Author)",
|
||||||
new { Message = messageId, Author = authorId });
|
new { Message = messageId, Channel = channelId, Author = authorId });
|
||||||
|
|
||||||
public Task<CommandMessage?> GetCommandMessage(IPKConnection conn, ulong messageId) =>
|
public Task<CommandMessage?> GetCommandMessage(IPKConnection conn, ulong messageId) =>
|
||||||
conn.QuerySingleOrDefaultAsync<CommandMessage>("select * from command_messages where message_id = @Message",
|
conn.QuerySingleOrDefaultAsync<CommandMessage>("select * from command_messages where message_id = @Message",
|
||||||
@ -23,5 +23,6 @@ namespace PluralKit.Core
|
|||||||
{
|
{
|
||||||
public ulong AuthorId { get; set; }
|
public ulong AuthorId { get; set; }
|
||||||
public ulong MessageId { get; set; }
|
public ulong MessageId { get; set; }
|
||||||
|
public ulong ChannelId { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -12,7 +12,7 @@ namespace PluralKit.Core
|
|||||||
internal class DatabaseMigrator
|
internal class DatabaseMigrator
|
||||||
{
|
{
|
||||||
private const string RootPath = "PluralKit.Core.Database"; // "resource path" root for SQL files
|
private const string RootPath = "PluralKit.Core.Database"; // "resource path" root for SQL files
|
||||||
private const int TargetSchemaVersion = 16;
|
private const int TargetSchemaVersion = 17;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
public DatabaseMigrator(ILogger logger)
|
public DatabaseMigrator(ILogger logger)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user