2023-02-16 20:34:59 +00:00
|
|
|
|
using SharpChat.Packet;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace SharpChat.Commands {
|
|
|
|
|
public class WhoCommand : IChatCommand {
|
|
|
|
|
public bool IsMatch(ChatCommandContext ctx) {
|
|
|
|
|
return ctx.NameEquals("who");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispatch(ChatCommandContext ctx) {
|
2024-05-14 22:17:25 +00:00
|
|
|
|
string? channelName = ctx.Args.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrEmpty(channelName)) {
|
|
|
|
|
ctx.Chat.SendTo(ctx.User, new WhoServerResponsePacket(
|
|
|
|
|
ctx.Chat.Users.Select(u => u.LegacyName).ToArray(),
|
|
|
|
|
ctx.User.LegacyName
|
|
|
|
|
));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
2024-05-14 22:17:25 +00:00
|
|
|
|
ChatChannel? channel = ctx.Chat.Channels.FirstOrDefault(c => c.NameEquals(channelName));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
2024-05-14 22:17:25 +00:00
|
|
|
|
if(channel == null) {
|
|
|
|
|
ctx.Chat.SendTo(ctx.User, new ChannelNotFoundErrorPacket(channelName));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
2024-05-14 22:17:25 +00:00
|
|
|
|
if(channel.Rank > ctx.User.Rank || (channel.HasPassword && !ctx.User.Can(ChatUserPermissions.JoinAnyChannel))) {
|
|
|
|
|
ctx.Chat.SendTo(ctx.User, new WhoChannelNotFoundErrorPacket(channelName));
|
|
|
|
|
return;
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}
|
2024-05-14 22:17:25 +00:00
|
|
|
|
|
|
|
|
|
ctx.Chat.SendTo(ctx.User, new WhoChannelResponsePacket(
|
|
|
|
|
channel.Name,
|
|
|
|
|
ctx.Chat.GetChannelUsers(channel).Select(user => user.LegacyName).ToArray(),
|
|
|
|
|
ctx.User.LegacyName
|
|
|
|
|
));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|