sharp-chat/SharpChat/Commands/SilenceApplyCommand.cs

58 lines
2 KiB
C#
Raw Normal View History

using SharpChat.Packet;
using System;
using System.Linq;
namespace SharpChat.Commands {
public class SilenceApplyCommand : IChatCommand {
public bool IsMatch(ChatCommandContext ctx) {
return ctx.NameEquals("silence");
}
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.SilenceUser)) {
ctx.User.Send(new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
return;
}
string silUserStr = ctx.Args.FirstOrDefault();
ChatUser silUser;
lock(ctx.Chat.UsersAccess)
if(string.IsNullOrWhiteSpace(silUserStr) || (silUser = ctx.Chat.Users.FirstOrDefault(u => u.NameEquals(silUserStr))) == null) {
ctx.User.Send(new LegacyCommandResponse(LCR.USER_NOT_FOUND, true, silUserStr ?? "User"));
return;
}
if(silUser == ctx.User) {
ctx.User.Send(new LegacyCommandResponse(LCR.SILENCE_SELF));
return;
}
if(silUser.Rank >= ctx.User.Rank) {
ctx.User.Send(new LegacyCommandResponse(LCR.SILENCE_HIERARCHY));
return;
}
if(silUser.IsSilenced) {
ctx.User.Send(new LegacyCommandResponse(LCR.SILENCE_ALREADY));
return;
}
DateTimeOffset silenceUntil = DateTimeOffset.MaxValue;
if(ctx.Args.Length > 1) {
if(!double.TryParse(ctx.Args.ElementAt(1), out double silenceSeconds)) {
ctx.User.Send(new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
return;
}
silenceUntil = DateTimeOffset.UtcNow.AddSeconds(silenceSeconds);
}
silUser.SilencedUntil = silenceUntil;
silUser.Send(new LegacyCommandResponse(LCR.SILENCED, false));
ctx.User.Send(new LegacyCommandResponse(LCR.TARGET_SILENCED, false, silUser.DisplayName));
}
}
}