Added pagination to say list

This commit is contained in:
Toastie (DCS Team) 2024-10-29 23:17:44 +13:00
parent 8af3e0a2f4
commit 3296fa98d1
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
2 changed files with 64 additions and 43 deletions

View file

@ -16,7 +16,7 @@ public class CloseCommand : ApplicationCommandModule
[SlashRequireGuild] [SlashRequireGuild]
[SlashCommand("close", "Closes a ticket.")] [SlashCommand("close", "Closes a ticket.")]
public async Task OnExecute(InteractionContext command, [Option("Reason", "Reason for closing the ticket.")] string reason = "") public async Task OnExecute(InteractionContext command, [Option("Reason", "(Optional) Reason for closing the ticket.")] string reason = "")
{ {
// Check if ticket exists in the database // Check if ticket exists in the database
if (!Database.TryGetOpenTicket(command.Channel.Id, out Database.Ticket _)) if (!Database.TryGetOpenTicket(command.Channel.Id, out Database.Ticket _))

View file

@ -1,7 +1,8 @@
using DSharpPlus.Entities; using DSharpPlus.Entities;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Extensions;
using DSharpPlus.SlashCommands; using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes; using DSharpPlus.SlashCommands.Attributes;
@ -16,52 +17,72 @@ public class SayCommand : ApplicationCommandModule
// Print list of all messages if no identifier is provided // Print list of all messages if no identifier is provided
if (identifier == null) if (identifier == null)
{ {
List<Database.Message> messages = Database.GetAllMessages(); SendMessageList(command);
if (!messages.Any()) return;
{
await command.CreateResponseAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "There are no messages registered."
}, true);
return;
}
List<string> listItems = new List<string>();
foreach (Database.Message message in messages)
{
listItems.Add("**" + message.identifier + "** Added by <@" + message.userID + ">\n");
}
LinkedList<string> listMessages = Utilities.ParseListIntoMessages(listItems);
foreach (string listMessage in listMessages)
{
await command.CreateResponseAsync(new DiscordEmbedBuilder
{
Title = "Available messages: ",
Color = DiscordColor.Green,
Description = listMessage
}, true);
}
} }
// Print specific message
else
{
if (!Database.TryGetMessage(identifier.ToLower(), out Database.Message message))
{
await command.CreateResponseAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "There is no message with that identifier."
}, true);
return;
}
if (!Database.TryGetMessage(identifier.ToLower(), out Database.Message message))
{
await command.CreateResponseAsync(new DiscordEmbedBuilder await command.CreateResponseAsync(new DiscordEmbedBuilder
{ {
Color = DiscordColor.Cyan, Color = DiscordColor.Red,
Description = message.message.Replace("\\n", "\n") Description = "There is no message with that identifier."
}, true);
return;
}
await command.CreateResponseAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Cyan,
Description = message.message.Replace("\\n", "\n")
});
}
private static async void SendMessageList(InteractionContext command)
{
List<Database.Message> messages = Database.GetAllMessages();
if (messages.Count == 0)
{
await command.CreateResponseAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "There are no messages registered."
}, true);
return;
}
List<string> listItems = [];
foreach (Database.Message message in messages)
{
listItems.Add("**" + message.identifier + "** Added by <@" + message.userID + ">\n");
}
List<DiscordEmbedBuilder> embeds = [];
foreach (string message in Utilities.ParseListIntoMessages(listItems))
{
embeds.Add(new DiscordEmbedBuilder
{
Title = "Available messages:",
Color = DiscordColor.Green,
Description = message
}); });
} }
// Add the footers
for (int i = 0; i < embeds.Count; i++)
{
embeds[i].Footer = new DiscordEmbedBuilder.EmbedFooter
{
Text = $"Page {i + 1} / {embeds.Count}"
};
}
List<Page> listPages = [];
foreach (DiscordEmbedBuilder embed in embeds)
{
listPages.Add(new Page("", embed));
}
await command.Interaction.SendPaginatedResponseAsync(true, command.User, listPages);
} }
} }