SupportChild/Commands/CreateSelectionBoxPanelCommand.cs

79 lines
3.5 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2022-08-21 07:34:11 +00:00
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
namespace SupportChild.Commands;
public class CreateSelectionBoxPanelCommand : ApplicationCommandModule
{
[SlashRequireGuild]
[SlashCommand("createselectionboxpanel", "Creates a selection box which users can use to open new tickets in specific categories.")]
public async Task OnExecute(InteractionContext command, [Option("Message", "(Optional) The message to show in the selection box.")] string message = null)
{
DiscordMessageBuilder builder = new DiscordMessageBuilder()
.WithContent(" ")
.AddComponents(await GetSelectComponents(command, message ?? "Open new ticket..."));
2022-08-21 07:34:11 +00:00
await command.Channel.SendMessageAsync(builder);
await command.CreateResponseAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Green,
Description = "Successfully created message, make sure to run this command again if you add new categories to the bot."
}, true);
}
2022-08-21 07:34:11 +00:00
public static async Task<List<DiscordSelectComponent>> GetSelectComponents(InteractionContext command, string placeholder)
{
List<Database.Category> verifiedCategories = await Utilities.GetVerifiedChannels();
2022-08-21 07:34:11 +00:00
if (verifiedCategories.Count == 0) return new List<DiscordSelectComponent>();
2022-08-21 07:34:11 +00:00
verifiedCategories = verifiedCategories.OrderBy(x => x.name).ToList();
List<DiscordSelectComponent> selectionComponents = new List<DiscordSelectComponent>();
int selectionOptions = 0;
for (int selectionBoxes = 0; selectionBoxes < 5 && selectionOptions < verifiedCategories.Count; selectionBoxes++)
{
List<DiscordSelectComponentOption> categoryOptions = new List<DiscordSelectComponentOption>();
2022-08-21 07:34:11 +00:00
for (; selectionOptions < 25 * (selectionBoxes + 1) && selectionOptions < verifiedCategories.Count; selectionOptions++)
{
categoryOptions.Add(new DiscordSelectComponentOption(verifiedCategories[selectionOptions].name, verifiedCategories[selectionOptions].id.ToString()));
}
selectionComponents.Add(new DiscordSelectComponent("supportchild_newticketselector" + selectionBoxes, placeholder, categoryOptions, false, 0, 1));
}
2022-08-21 07:34:11 +00:00
return selectionComponents;
}
2022-08-21 07:34:11 +00:00
public static async Task OnSelectionMenuUsed(DiscordInteraction interaction)
{
if (interaction.Data.Values == null || interaction.Data.Values.Length <= 0) return;
2022-08-21 07:34:11 +00:00
if (!ulong.TryParse(interaction.Data.Values[0], out ulong categoryID) || categoryID == 0) return;
2022-08-21 07:34:11 +00:00
await interaction.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral());
2022-08-21 07:34:11 +00:00
(bool success, string message) = await NewCommand.OpenNewTicket(interaction.User.Id, interaction.ChannelId, categoryID);
2022-08-21 07:34:11 +00:00
if (success)
{
await interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().AddEmbed(new DiscordEmbedBuilder
{
Color = DiscordColor.Green,
Description = message
}).AsEphemeral());
}
else
{
await interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().AddEmbed(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = message
}).AsEphemeral());
}
}
2022-08-21 07:34:11 +00:00
}