SupportChild/Commands/MoveCommand.cs

110 lines
4 KiB
C#
Raw Permalink Normal View History

2022-04-18 10:52:03 +00:00
using System;
2022-02-21 08:40:09 +00:00
using System.Collections.Generic;
using System.ComponentModel;
2022-02-21 08:40:09 +00:00
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus.Commands;
using DSharpPlus.Commands.ContextChecks;
using DSharpPlus.Commands.Processors.SlashCommands;
2022-02-21 08:40:09 +00:00
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
2022-08-21 07:34:11 +00:00
namespace SupportChild.Commands;
public class MoveCommand
2022-02-21 08:40:09 +00:00
{
[RequireGuild]
[Command("move")]
[Description("Moves a ticket to another category.")]
public async Task OnExecute(SlashCommandContext command,
[Parameter("category")][Description("The category to move the ticket to. Only has to be the beginning of the name.")] string category)
{
// Check if ticket exists in the database
if (!Database.TryGetOpenTicket(command.Channel.Id, out Database.Ticket ticket))
{
await command.RespondAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "This channel is not a ticket."
}, true);
return;
}
2022-02-21 08:40:09 +00:00
if (string.IsNullOrEmpty(category))
{
await command.RespondAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Error: No category provided."
}, true);
return;
}
2022-02-21 08:40:09 +00:00
IReadOnlyList<DiscordChannel> channels = await command.Guild.GetChannelsAsync();
IEnumerable<DiscordChannel> categories = channels.Where(x => x.IsCategory);
DiscordChannel categoryChannel = categories.FirstOrDefault(x => x.Name.StartsWith(category.Trim(), StringComparison.OrdinalIgnoreCase));
2022-02-21 08:40:09 +00:00
if (categoryChannel == null)
{
await command.RespondAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Error: Could not find a category by that name."
}, true);
return;
}
2022-02-21 08:40:09 +00:00
if (command.Channel.Parent.Id == categoryChannel.Id)
{
await command.RespondAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Error: The ticket is already in that category."
}, true);
return;
}
2022-02-21 08:40:09 +00:00
if (categoryChannel.Children.Count == 50)
{
Logger.Warn("Could not move ticket " + ticket.id.ToString("00000") + " to the category '" + categoryChannel.Name + "', likely because it is full.");
await command.RespondAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Error: Unable to move ticket to that category, is it full?"
}, true);
return;
}
try
{
await command.Channel.ModifyAsync(modifiedAttributes => modifiedAttributes.Parent = categoryChannel);
}
catch (UnauthorizedException)
{
await command.RespondAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Error: Not authorized to move this ticket to that category."
}, true);
return;
}
catch (BadRequestException)
{
Logger.Warn("Could not move ticket " + ticket.id.ToString("00000") + " to the category '" + categoryChannel.Name + "', likely because it is full.");
await command.RespondAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Error: Unable to move ticket to that category, is it full?"
}, true);
return;
}
2022-08-21 07:34:11 +00:00
await command.RespondAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Green,
Description = "Ticket was moved to `" + categoryChannel.Name + "`."
});
2024-12-27 07:17:14 +00:00
await LogChannel.Success(command.User.Mention + " moved " + command.Channel.Mention + " to `" + categoryChannel.Name + "`.", ticket.id);
}
2022-05-19 11:38:59 +00:00
}