sharp-chat/SharpChat/Commands/MessageDeleteCommand.cs

42 lines
1.5 KiB
C#
Raw Normal View History

using SharpChat.EventStorage;
using SharpChat.Packet;
using System.Linq;
namespace SharpChat.Commands
{
2024-05-14 22:17:25 +00:00
public class MessageDeleteCommand : 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)) {
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}
2024-05-10 19:18:55 +00:00
string? firstArg = ctx.Args.FirstOrDefault();
if(string.IsNullOrWhiteSpace(firstArg) || !firstArg.All(char.IsDigit) || !long.TryParse(firstArg, out long delSeqId)) {
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
return;
}
2024-05-10 19:18:55 +00:00
StoredEventInfo? delMsg = ctx.Chat.Events.GetEvent(delSeqId);
2024-05-10 19:18:55 +00:00
if(delMsg == null || delMsg.Sender?.Rank > ctx.User.Rank || (!deleteAnyMessage && delMsg.Sender?.UserId != ctx.User.UserId)) {
2024-05-14 22:17:25 +00:00
ctx.Chat.SendTo(ctx.User, new MessageDeleteNotAllowedErrorPacket());
2023-02-19 22:27:08 +00:00
return;
}
2023-02-19 22:27:08 +00:00
ctx.Chat.Events.RemoveEvent(delMsg);
2024-05-14 22:17:25 +00:00
ctx.Chat.Send(new MessageDeletePacket(delMsg.Id));
}
}
}