SupportChild/Commands/CreateButtonPanelCommand.cs

81 lines
3.2 KiB
C#
Raw Permalink 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 CreateButtonPanelCommand : ApplicationCommandModule
{
[SlashRequireGuild]
[SlashCommand("createbuttonpanel", "Creates a series of buttons which users can use to open new tickets in specific categories.")]
public async Task OnExecute(InteractionContext command)
{
DiscordMessageBuilder builder = new DiscordMessageBuilder().WithContent(" ");
List<Database.Category> verifiedCategories = await Utilities.GetVerifiedChannels();
2022-08-21 07:34:11 +00:00
if (verifiedCategories.Count == 0)
{
await command.CreateResponseAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Error: No registered categories found."
}, true);
return;
}
2022-08-21 07:34:11 +00:00
verifiedCategories = verifiedCategories.OrderBy(x => x.name).ToList();
2022-08-21 07:34:11 +00:00
int nrOfButtons = 0;
for (int nrOfButtonRows = 0; nrOfButtonRows < 5 && nrOfButtons < verifiedCategories.Count; nrOfButtonRows++)
{
List<DiscordButtonComponent> buttonRow = new List<DiscordButtonComponent>();
2022-08-21 07:34:11 +00:00
for (; nrOfButtons < 5 * (nrOfButtonRows + 1) && nrOfButtons < verifiedCategories.Count; nrOfButtons++)
{
buttonRow.Add(new DiscordButtonComponent(ButtonStyle.Primary, "supportchild_newticketbutton " + verifiedCategories[nrOfButtons].id, verifiedCategories[nrOfButtons].name));
}
builder.AddComponents(buttonRow);
}
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 OnButtonUsed(DiscordInteraction interaction)
{
await interaction.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral());
2022-08-21 07:34:11 +00:00
if (!ulong.TryParse(interaction.Data.CustomId.Replace("supportchild_newticketbutton ", ""), out ulong categoryID) || categoryID == 0)
{
Logger.Warn("Invalid ID: " + interaction.Data.CustomId.Replace("supportchild_newticketbutton ", ""));
return;
}
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
}));
}
else
{
await interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().AddEmbed(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = message
}));
}
}
2022-08-21 07:34:11 +00:00
}