42 lines
1.6 KiB
C#
42 lines
1.6 KiB
C#
using SharpChat.Events;
|
|
using SharpChat.Packet;
|
|
using System.Linq;
|
|
|
|
namespace SharpChat.Commands {
|
|
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)) {
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
|
|
return;
|
|
}
|
|
|
|
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 LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
|
|
return;
|
|
}
|
|
|
|
lock(ctx.Chat.EventsAccess) {
|
|
IChatEvent delMsg = ctx.Chat.Events.GetEvent(delSeqId);
|
|
|
|
if(delMsg == null || delMsg.Sender.Rank > ctx.User.Rank || (!deleteAnyMessage && delMsg.Sender.UserId != ctx.User.UserId)) {
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.MESSAGE_DELETE_ERROR));
|
|
return;
|
|
}
|
|
|
|
ctx.Chat.Events.RemoveEvent(delMsg);
|
|
ctx.Chat.Send(new ChatMessageDeletePacket(delMsg.SequenceId));
|
|
}
|
|
}
|
|
}
|
|
}
|