sharp-chat/SharpChat/Commands/SilenceApplyCommand.cs

57 lines
2.1 KiB
C#

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.Chat.SendTo(ctx.User, 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.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.USER_NOT_FOUND, true, silUserStr ?? "User"));
return;
}
if(silUser == ctx.User) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.SILENCE_SELF));
return;
}
if(silUser.Rank >= ctx.User.Rank) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.SILENCE_HIERARCHY));
return;
}
if(silUser.IsSilenced) {
ctx.Chat.SendTo(ctx.User, 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.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
return;
}
silenceUntil = DateTimeOffset.UtcNow.AddSeconds(silenceSeconds);
}
silUser.SilencedUntil = silenceUntil;
ctx.Chat.SendTo(silUser, new LegacyCommandResponse(LCR.SILENCED, false));
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.TARGET_SILENCED, false, silUser.DisplayName));
}
}
}