sharp-chat/SharpChat/Commands/ChannelDeleteCommand.cs

37 lines
1.3 KiB
C#
Raw Normal View History

using SharpChat.Packet;
using System.Linq;
namespace SharpChat.Commands {
2024-05-14 22:17:25 +00:00
public class ChannelDeleteCommand : IChatCommand {
public bool IsMatch(ChatCommandContext ctx) {
return ctx.NameEquals("delchan") || (
ctx.NameEquals("delete")
&& ctx.Args.FirstOrDefault()?.All(char.IsDigit) == false
);
}
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.Args.Any() || string.IsNullOrWhiteSpace(ctx.Args.FirstOrDefault())) {
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
return;
}
string delChanName = string.Join('_', ctx.Args);
2024-05-10 19:18:55 +00:00
ChatChannel? delChan = ctx.Chat.Channels.FirstOrDefault(c => c.NameEquals(delChanName));
if(delChan == null) {
2024-05-14 22:17:25 +00:00
ctx.Chat.SendTo(ctx.User, new ChannelNotFoundErrorPacket(delChanName));
return;
}
if(!ctx.User.Can(ChatUserPermissions.DeleteChannel) && delChan.IsOwner(ctx.User)) {
2024-05-14 22:17:25 +00:00
ctx.Chat.SendTo(ctx.User, new ChannelDeleteNotAllowedErrorPacket(delChan.Name));
return;
}
2023-02-19 22:27:08 +00:00
ctx.Chat.RemoveChannel(delChan);
2024-05-14 22:17:25 +00:00
ctx.Chat.SendTo(ctx.User, new ChannelDeleteResponsePacket(delChan.Name));
}
}
}