2023-02-16 20:34:59 +00:00
|
|
|
|
using SharpChat.Packet;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace SharpChat.Commands {
|
|
|
|
|
public class DeleteChannelCommand : 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())) {
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string delChanName = string.Join('_', ctx.Args);
|
|
|
|
|
ChatChannel delChan;
|
|
|
|
|
lock(ctx.Chat.ChannelsAccess)
|
|
|
|
|
delChan = ctx.Chat.Channels.FirstOrDefault(c => c.NameEquals(delChanName));
|
|
|
|
|
|
|
|
|
|
if(delChan == null) {
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.CHANNEL_NOT_FOUND, true, delChanName));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!ctx.User.Can(ChatUserPermissions.DeleteChannel) && delChan.Owner != ctx.User) {
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.CHANNEL_DELETE_FAILED, true, delChan.Name));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lock(ctx.Chat.ChannelsAccess)
|
|
|
|
|
ctx.Chat.RemoveChannel(delChan);
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.CHANNEL_DELETED, false, delChan.Name));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|