2023-02-23 21:46:49 +00:00
|
|
|
|
using SharpChat.EventStorage;
|
2023-02-16 20:34:59 +00:00
|
|
|
|
using SharpChat.Packet;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2023-02-23 21:46:49 +00:00
|
|
|
|
namespace SharpChat.Commands
|
|
|
|
|
{
|
2023-02-16 20:34:59 +00:00
|
|
|
|
public class DeleteMessageCommand : IChatCommand {
|
|
|
|
|
public bool IsMatch(ChatCommandContext ctx) {
|
|
|
|
|
return ctx.NameEquals("delmsg") || (
|
|
|
|
|
ctx.NameEquals("delete")
|
|
|
|
|
&& ctx.Args.FirstOrDefault()?.All(char.IsDigit) == true
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispatch(ChatCommandContext ctx) {
|
|
|
|
|
bool deleteAnyMessage = ctx.User.Can(ChatUserPermissions.DeleteAnyMessage);
|
|
|
|
|
|
|
|
|
|
if(!deleteAnyMessage && !ctx.User.Can(ChatUserPermissions.DeleteOwnMessage)) {
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
string? firstArg = ctx.Args.FirstOrDefault();
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(firstArg) || !firstArg.All(char.IsDigit) || !long.TryParse(firstArg, out long delSeqId)) {
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
StoredEventInfo? delMsg = ctx.Chat.Events.GetEvent(delSeqId);
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
if(delMsg == null || delMsg.Sender?.Rank > ctx.User.Rank || (!deleteAnyMessage && delMsg.Sender?.UserId != ctx.User.UserId)) {
|
2023-02-19 22:27:08 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.MESSAGE_DELETE_ERROR));
|
|
|
|
|
return;
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}
|
2023-02-19 22:27:08 +00:00
|
|
|
|
|
|
|
|
|
ctx.Chat.Events.RemoveEvent(delMsg);
|
2023-02-23 21:46:49 +00:00
|
|
|
|
ctx.Chat.Send(new ChatMessageDeletePacket(delMsg.Id));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|