SupportChild/Commands/ListCommand.cs

99 lines
2.8 KiB
C#
Raw Normal View History

2022-04-18 10:52:03 +00:00
using System.Collections.Generic;
2022-02-21 08:40:09 +00:00
using System.Threading.Tasks;
using DSharpPlus.Entities;
2022-08-21 07:34:11 +00:00
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Extensions;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
2022-02-21 08:40:09 +00:00
2022-08-21 07:34:11 +00:00
namespace SupportChild.Commands;
public class ListCommand : ApplicationCommandModule
2022-02-21 08:40:09 +00:00
{
2022-08-21 07:34:11 +00:00
[SlashRequireGuild]
[SlashCommand("list", "Lists tickets opened by a user.")]
public async Task OnExecute(InteractionContext command, [Option("User", "(Optional) The user to get tickets by.")] DiscordUser user = null)
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
DiscordUser listUser = user == null ? command.User : user;
List<DiscordEmbedBuilder> openEmbeds = new List<DiscordEmbedBuilder>();
if (Database.TryGetOpenTickets(listUser.Id, out List<Database.Ticket> openTickets))
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
List<string> listItems = new List<string>();
foreach (Database.Ticket ticket in openTickets)
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
listItems.Add("**" + ticket.DiscordRelativeTime() + ":** <#" + ticket.channelID + ">\n");
2022-05-19 11:38:59 +00:00
}
2022-02-21 08:40:09 +00:00
2022-08-21 07:34:11 +00:00
foreach (string message in Utilities.ParseListIntoMessages(listItems))
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
openEmbeds.Add(new DiscordEmbedBuilder
{
Color = DiscordColor.Green,
Description = message
});
2022-05-19 11:38:59 +00:00
}
2022-08-21 07:34:11 +00:00
// Add the titles
for (int i = 0; i < openEmbeds.Count; i++)
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
openEmbeds[i].Title = $"Open tickets ({i + 1}/{openEmbeds.Count})";
2022-05-19 11:38:59 +00:00
}
2022-08-21 07:34:11 +00:00
}
2022-02-21 08:40:09 +00:00
2022-08-21 07:34:11 +00:00
List<DiscordEmbedBuilder> closedEmbeds = new List<DiscordEmbedBuilder>();
if (Database.TryGetClosedTickets(listUser.Id, out List<Database.Ticket> closedTickets))
{
List<string> listItems = new List<string>();
foreach (Database.Ticket ticket in closedTickets)
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
listItems.Add("**" + ticket.DiscordRelativeTime() + ":** Ticket " + ticket.id.ToString("00000") + "\n");
}
2022-02-21 08:40:09 +00:00
2022-08-21 07:34:11 +00:00
foreach (string message in Utilities.ParseListIntoMessages(listItems))
{
closedEmbeds.Add(new DiscordEmbedBuilder
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
Color = DiscordColor.Red,
Description = message
});
2022-05-19 11:38:59 +00:00
}
2022-08-21 07:34:11 +00:00
// Add the titles
for (int i = 0; i < closedEmbeds.Count; i++)
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
closedEmbeds[i].Title = $"Closed tickets ({i + 1}/{closedEmbeds.Count})";
2022-05-19 11:38:59 +00:00
}
2022-08-21 07:34:11 +00:00
}
2022-02-21 08:40:09 +00:00
2022-08-21 07:34:11 +00:00
// Merge the embed lists and add the footers
List<DiscordEmbedBuilder> embeds = new List<DiscordEmbedBuilder>();
embeds.AddRange(openEmbeds);
embeds.AddRange(closedEmbeds);
for (int i = 0; i < embeds.Count; i++)
{
embeds[i].Footer = new DiscordEmbedBuilder.EmbedFooter
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
Text = $"Page {i + 1} / {embeds.Count}"
};
}
2022-02-21 08:40:09 +00:00
2022-08-21 07:34:11 +00:00
if (embeds.Count == 0)
{
await command.CreateResponseAsync(new DiscordEmbedBuilder
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
Color = DiscordColor.Cyan,
Description = "User does not have any open or closed tickets."
});
return;
2022-05-19 11:38:59 +00:00
}
2022-08-21 07:34:11 +00:00
List<Page> listPages = new List<Page>();
foreach (DiscordEmbedBuilder embed in embeds)
{
listPages.Add(new Page("", embed));
}
await command.Interaction.SendPaginatedResponseAsync(true, command.User, listPages);
2022-05-19 11:38:59 +00:00
}
}