sharp-chat/SharpChat/Commands/WhisperCommand.cs

52 lines
1.8 KiB
C#
Raw Normal View History

using SharpChat.Events;
using SharpChat.Packet;
using System;
using System.Linq;
namespace SharpChat.Commands {
public class WhisperCommand : IChatCommand {
public bool IsMatch(ChatCommandContext ctx) {
return ctx.NameEquals("whisper")
|| ctx.NameEquals("msg");
}
public void Dispatch(ChatCommandContext ctx) {
if(ctx.Args.Length < 2) {
ctx.User.Send(new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
return;
}
ChatUser whisperUser;
string whisperUserStr = ctx.Args.FirstOrDefault();
lock(ctx.Chat.UsersAccess)
whisperUser = ctx.Chat.Users.FirstOrDefault(u => u.NameEquals(whisperUserStr));
if(whisperUser == null) {
ctx.User.Send(new LegacyCommandResponse(LCR.USER_NOT_FOUND, true, whisperUserStr));
return;
}
if(whisperUser == ctx.User)
return;
string whisperStr = string.Join(' ', ctx.Args.Skip(1));
whisperUser.Send(new ChatMessageAddPacket(new ChatMessage {
DateTime = DateTimeOffset.Now,
Target = whisperUser,
TargetName = whisperUser.TargetName,
Sender = ctx.User,
Text = whisperStr,
Flags = ChatMessageFlags.Private,
}));
ctx.User.Send(new ChatMessageAddPacket(new ChatMessage {
DateTime = DateTimeOffset.Now,
Target = whisperUser,
TargetName = whisperUser.TargetName,
Sender = ctx.User,
Text = $"{whisperUser.DisplayName} {whisperStr}",
Flags = ChatMessageFlags.Private,
}));
}
}
}