sharp-chat/SharpChat/Commands/WhoCommand.cs

41 lines
1.6 KiB
C#
Raw Normal View History

using SharpChat.PacketsS2C;
using System.Linq;
namespace SharpChat.Commands {
public class WhoCommand : ISockChatClientCommand {
public bool IsMatch(SockChatClientCommandContext ctx) {
return ctx.NameEquals("who");
}
public void Dispatch(SockChatClientCommandContext ctx) {
2024-05-14 22:17:25 +00:00
string? channelName = ctx.Args.FirstOrDefault();
if(string.IsNullOrEmpty(channelName)) {
ctx.Chat.SendTo(ctx.User, new WhoServerResponseS2CPacket(
2024-05-19 21:02:17 +00:00
ctx.Chat.Users.All.Select(u => SockChatUtility.GetUserNameWithStatus(u)).ToArray(),
SockChatUtility.GetUserName(ctx.User)
2024-05-14 22:17:25 +00:00
));
return;
}
2024-05-19 21:02:17 +00:00
ChannelInfo? channel = ctx.Chat.Channels.Get(channelName, SockChatUtility.SanitiseChannelName);
2024-05-14 22:17:25 +00:00
if(channel == null) {
ctx.Chat.SendTo(ctx.User, new ChannelNotFoundErrorS2CPacket(channelName));
2024-05-14 22:17:25 +00:00
return;
}
if(channel.Rank > ctx.User.Rank || (channel.HasPassword && !ctx.User.Permissions.HasFlag(UserPermissions.JoinAnyChannel))) {
ctx.Chat.SendTo(ctx.User, new WhoChannelNotFoundErrorS2CPacket(channelName));
2024-05-14 22:17:25 +00:00
return;
}
2024-05-14 22:17:25 +00:00
ctx.Chat.SendTo(ctx.User, new WhoChannelResponseS2CPacket(
2024-05-14 22:17:25 +00:00
channel.Name,
2024-05-19 21:02:17 +00:00
ctx.Chat.GetChannelUsers(channel).Select(user => SockChatUtility.GetUserNameWithStatus(user)).ToArray(),
SockChatUtility.GetUserNameWithStatus(ctx.User)
2024-05-14 22:17:25 +00:00
));
}
}
}