2025-04-26 22:47:57 +00:00
|
|
|
using SharpChat.SockChat.S2CPackets;
|
2023-02-16 21:34:59 +01:00
|
|
|
|
2025-04-26 23:15:54 +00:00
|
|
|
namespace SharpChat.ClientCommands;
|
2023-02-16 21:34:59 +01:00
|
|
|
|
2025-04-26 23:15:54 +00:00
|
|
|
public class DeleteChannelClientCommand : ClientCommand {
|
|
|
|
public bool IsMatch(ClientCommandContext ctx) {
|
|
|
|
return ctx.NameEquals("delchan") || (
|
|
|
|
ctx.NameEquals("delete")
|
|
|
|
&& ctx.Args.FirstOrDefault()?.All(char.IsDigit) == false
|
|
|
|
);
|
|
|
|
}
|
2025-04-25 20:05:55 +00:00
|
|
|
|
2025-04-26 23:15:54 +00:00
|
|
|
public async Task Dispatch(ClientCommandContext ctx) {
|
|
|
|
long msgId = ctx.Chat.RandomSnowflake.Next();
|
2023-02-16 21:34:59 +01:00
|
|
|
|
2025-04-26 23:15:54 +00:00
|
|
|
if(ctx.Args.Length < 1 || string.IsNullOrWhiteSpace(ctx.Args.FirstOrDefault())) {
|
|
|
|
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_FORMAT_ERROR));
|
|
|
|
return;
|
|
|
|
}
|
2023-02-16 21:34:59 +01:00
|
|
|
|
2025-04-26 23:15:54 +00:00
|
|
|
string delChanName = string.Join('_', ctx.Args);
|
|
|
|
Channel? delChan = ctx.Chat.Channels.FirstOrDefault(c => c.NameEquals(delChanName));
|
2023-02-16 21:34:59 +01:00
|
|
|
|
2025-04-26 23:15:54 +00:00
|
|
|
if(delChan == null) {
|
|
|
|
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.CHANNEL_NOT_FOUND, true, delChanName));
|
|
|
|
return;
|
|
|
|
}
|
2023-02-16 21:34:59 +01:00
|
|
|
|
2025-04-26 23:15:54 +00:00
|
|
|
if(!ctx.User.Can(UserPermissions.DeleteChannel) && delChan.IsOwner(ctx.User)) {
|
|
|
|
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.CHANNEL_DELETE_FAILED, true, delChan.Name));
|
|
|
|
return;
|
2023-02-16 21:34:59 +01:00
|
|
|
}
|
2025-04-26 23:15:54 +00:00
|
|
|
|
|
|
|
await ctx.Chat.RemoveChannel(delChan);
|
|
|
|
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.CHANNEL_DELETED, false, delChan.Name));
|
2023-02-16 21:34:59 +01:00
|
|
|
}
|
|
|
|
}
|