2023-02-16 20:34:59 +00:00
|
|
|
|
using SharpChat.Packet;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace SharpChat.Commands {
|
2024-05-19 02:17:51 +00:00
|
|
|
|
public class ChannelDeleteCommand : IUserCommand {
|
|
|
|
|
public bool IsMatch(UserCommandContext ctx) {
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return ctx.NameEquals("delchan") || (
|
|
|
|
|
ctx.NameEquals("delete")
|
|
|
|
|
&& ctx.Args.FirstOrDefault()?.All(char.IsDigit) == false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-19 02:17:51 +00:00
|
|
|
|
public void Dispatch(UserCommandContext ctx) {
|
2023-02-16 20:34:59 +00:00
|
|
|
|
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-19 02:17:51 +00:00
|
|
|
|
ChannelInfo? delChan = ctx.Chat.Channels.Values.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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-19 02:17:51 +00:00
|
|
|
|
if(!ctx.User.Permissions.HasFlag(UserPermissions.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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|