sharp-chat/SharpChat/Commands/WhisperCommand.cs

51 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.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
return;
}
string whisperUserStr = ctx.Args.FirstOrDefault();
2023-02-19 22:27:08 +00:00
ChatUser whisperUser = ctx.Chat.Users.FirstOrDefault(u => u.NameEquals(whisperUserStr));
if(whisperUser == null) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.USER_NOT_FOUND, true, whisperUserStr));
return;
}
2023-02-19 22:27:08 +00:00
if(whisperUser == ctx.User)
return;
string whisperStr = string.Join(' ', ctx.Args.Skip(1));
2023-02-16 22:56:50 +00:00
string whisperChan = ChatUser.GetDMChannelName(ctx.User, whisperUser);
DateTimeOffset dateTime = DateTimeOffset.Now;
ctx.Chat.SendTo(whisperUser, new ChatMessageAddPacket(new ChatMessage {
2023-02-16 22:56:50 +00:00
DateTime = dateTime,
ChannelName = whisperChan,
Sender = ctx.User,
Text = whisperStr,
Flags = ChatMessageFlags.Private,
}));
ctx.Chat.SendTo(ctx.User, new ChatMessageAddPacket(new ChatMessage {
2023-02-16 22:56:50 +00:00
DateTime = dateTime,
ChannelName = whisperChan,
Sender = ctx.User,
Text = $"{whisperUser.DisplayName} {whisperStr}",
Flags = ChatMessageFlags.Private,
}));
}
}
}