40 lines
1.5 KiB
C#
40 lines
1.5 KiB
C#
|
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())) {
|
|||
|
ctx.User.Send(new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
|
|||
|
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) {
|
|||
|
ctx.User.Send(new LegacyCommandResponse(LCR.CHANNEL_NOT_FOUND, true, delChanName));
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if(!ctx.User.Can(ChatUserPermissions.DeleteChannel) && delChan.Owner != ctx.User) {
|
|||
|
ctx.User.Send(new LegacyCommandResponse(LCR.CHANNEL_DELETE_FAILED, true, delChan.Name));
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
lock(ctx.Chat.ChannelsAccess)
|
|||
|
ctx.Chat.RemoveChannel(delChan);
|
|||
|
ctx.User.Send(new LegacyCommandResponse(LCR.CHANNEL_DELETED, false, delChan.Name));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|