40 lines
1.4 KiB
C#
40 lines
1.4 KiB
C#
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) {
|
|
string? channelName = ctx.Args.FirstOrDefault();
|
|
|
|
if(string.IsNullOrEmpty(channelName)) {
|
|
ctx.Chat.SendTo(ctx.User, new WhoServerResponsePacket(
|
|
ctx.Chat.Users.Values.Select(u => u.LegacyName).ToArray(),
|
|
ctx.User.LegacyName
|
|
));
|
|
return;
|
|
}
|
|
|
|
ChatChannel? channel = ctx.Chat.Channels.Values.FirstOrDefault(c => c.NameEquals(channelName));
|
|
|
|
if(channel == null) {
|
|
ctx.Chat.SendTo(ctx.User, new ChannelNotFoundErrorPacket(channelName));
|
|
return;
|
|
}
|
|
|
|
if(channel.Rank > ctx.User.Rank || (channel.HasPassword && !ctx.User.Permissions.HasFlag(ChatUserPermissions.JoinAnyChannel))) {
|
|
ctx.Chat.SendTo(ctx.User, new WhoChannelNotFoundErrorPacket(channelName));
|
|
return;
|
|
}
|
|
|
|
ctx.Chat.SendTo(ctx.User, new WhoChannelResponsePacket(
|
|
channel.Name,
|
|
ctx.Chat.GetChannelUsers(channel).Select(user => user.LegacyName).ToArray(),
|
|
ctx.User.LegacyName
|
|
));
|
|
}
|
|
}
|
|
}
|