SupportChild/SupportChild/Commands/MoveCommand.cs

104 lines
3.1 KiB
C#
Raw Normal View History

2022-02-21 08:40:09 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
using Microsoft.Extensions.Logging;
namespace SupportChild.Commands
{
public class MoveCommand : BaseCommandModule
{
[Command("move")]
[Description("Move a ticket to another category.")]
public async Task OnExecute(CommandContext command, [RemainingText] string commandArgs)
{
// Check if the user has permission to use this command.
if (!Config.HasPermission(command.Member, "move"))
{
DiscordEmbed error = new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "You do not have permission to use this command."
};
await command.RespondAsync(error);
command.Client.Logger.Log(LogLevel.Information, "User tried to use the move command but did not have permission.");
return;
}
// Check if ticket exists in the database
if (!Database.TryGetOpenTicket(command.Channel.Id, out Database.Ticket ticket))
{
DiscordEmbed error = new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "This channel is not a ticket."
};
await command.RespondAsync(error);
return;
}
if (string.IsNullOrEmpty(command.RawArgumentString))
{
DiscordEmbed error = new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Error: No category provided."
};
await command.RespondAsync(error);
return;
}
IReadOnlyList<DiscordChannel> channels = await command.Guild.GetChannelsAsync();
IEnumerable<DiscordChannel> categories = channels.Where(x => x.IsCategory);
DiscordChannel category = categories.FirstOrDefault(x => x.Name.StartsWith(command.RawArgumentString.Trim(), StringComparison.OrdinalIgnoreCase));
if (category == null)
{
DiscordEmbed error = new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Error: Could not find a category by that name."
};
await command.RespondAsync(error);
return;
}
if (command.Channel.Id == category.Id)
{
DiscordEmbed error = new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Error: The ticket is already in that category."
};
await command.RespondAsync(error);
return;
}
try
{
await command.Channel.ModifyAsync(modifiedAttributes => modifiedAttributes.Parent = category);
}
catch (UnauthorizedException)
{
DiscordEmbed error = new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Error: Not authorized to move this ticket to that category."
};
await command.RespondAsync(error);
return;
}
DiscordEmbed feedback = new DiscordEmbedBuilder
{
Color = DiscordColor.Green,
Description = "Ticket was moved to " + category.Mention
};
await command.RespondAsync(feedback);
}
}
}