2023-02-16 20:34:59 +00:00
|
|
|
|
using SharpChat.Packet;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace SharpChat.Commands {
|
2024-05-14 22:17:25 +00:00
|
|
|
|
public class ChannelDeleteCommand : IChatCommand {
|
2023-02-16 20:34:59 +00:00
|
|
|
|
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())) {
|
2024-05-14 19:11:09 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
|
2023-02-16 20:34:59 +00:00
|
|
|
|
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));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
|
|
|
|
if(delChan == null) {
|
2024-05-14 22:17:25 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new ChannelNotFoundErrorPacket(delChanName));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 00:28:53 +00:00
|
|
|
|
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));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
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));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|