Breadcraft-Support/Breadcraft/Commands/CreateButtonPanelCommand.cs

81 lines
2.8 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2023-01-26 03:42:11 -08:00
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
namespace Breadcraft.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();
if (verifiedCategories.Count == 0)
{
await command.CreateResponseAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Error: No registered categories found."
}, true);
return;
}
2023-01-26 03:42:11 -08:00
verifiedCategories = verifiedCategories.OrderBy(x => x.name).ToList();
2023-01-26 03:42:11 -08:00
int nrOfButtons = 0;
for (int nrOfButtonRows = 0; nrOfButtonRows < 5 && nrOfButtons < verifiedCategories.Count; nrOfButtonRows++)
{
List<DiscordButtonComponent> buttonRow = new List<DiscordButtonComponent>();
2023-01-26 03:42:11 -08:00
for (; nrOfButtons < 5 * (nrOfButtonRows + 1) && nrOfButtons < verifiedCategories.Count; nrOfButtons++)
{
buttonRow.Add(new DiscordButtonComponent(ButtonStyle.Primary, "bcsupport_newticketbutton " + verifiedCategories[nrOfButtons].id, verifiedCategories[nrOfButtons].name));
2023-01-26 03:42:11 -08:00
}
builder.AddComponents(buttonRow);
}
2023-01-26 03:42:11 -08: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);
}
2023-01-26 03:42:11 -08:00
public static async Task OnButtonUsed(DiscordInteraction interaction)
{
await interaction.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral());
if (!ulong.TryParse(interaction.Data.CustomId.Replace("bcsupport_newticketbutton ", ""), out ulong categoryID) || categoryID == 0)
2023-01-26 03:42:11 -08:00
{
Logger.Warn("Invalid ID: " + interaction.Data.CustomId.Replace("bcsupport_newticketbutton ", ""));
2023-01-26 03:42:11 -08:00
return;
}
2023-01-26 03:42:11 -08:00
(bool success, string message) = await NewCommand.OpenNewTicket(interaction.User.Id, interaction.ChannelId, categoryID);
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
}));
}
}
}