From 3296fa98d122036f12f901155e19ab7715534a7a Mon Sep 17 00:00:00 2001 From: Toastie Date: Tue, 29 Oct 2024 23:17:44 +1300 Subject: [PATCH] Added pagination to say list --- Commands/CloseCommand.cs | 2 +- Commands/SayCommand.cs | 105 +++++++++++++++++++++++---------------- 2 files changed, 64 insertions(+), 43 deletions(-) diff --git a/Commands/CloseCommand.cs b/Commands/CloseCommand.cs index 78e7661..e020554 100644 --- a/Commands/CloseCommand.cs +++ b/Commands/CloseCommand.cs @@ -16,7 +16,7 @@ public class CloseCommand : ApplicationCommandModule [SlashRequireGuild] [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 if (!Database.TryGetOpenTicket(command.Channel.Id, out Database.Ticket _)) diff --git a/Commands/SayCommand.cs b/Commands/SayCommand.cs index 3699fcc..b60db54 100644 --- a/Commands/SayCommand.cs +++ b/Commands/SayCommand.cs @@ -1,7 +1,8 @@ using DSharpPlus.Entities; using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; +using DSharpPlus.Interactivity; +using DSharpPlus.Interactivity.Extensions; using DSharpPlus.SlashCommands; using DSharpPlus.SlashCommands.Attributes; @@ -16,52 +17,72 @@ public class SayCommand : ApplicationCommandModule // Print list of all messages if no identifier is provided if (identifier == null) { - List messages = Database.GetAllMessages(); - if (!messages.Any()) - { - await command.CreateResponseAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Red, - Description = "There are no messages registered." - }, true); - return; - } - - List listItems = new List(); - foreach (Database.Message message in messages) - { - listItems.Add("**" + message.identifier + "** Added by <@" + message.userID + ">\n"); - } - - LinkedList listMessages = Utilities.ParseListIntoMessages(listItems); - foreach (string listMessage in listMessages) - { - await command.CreateResponseAsync(new DiscordEmbedBuilder - { - Title = "Available messages: ", - Color = DiscordColor.Green, - Description = listMessage - }, true); - } + SendMessageList(command); + return; } - // 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 { - Color = DiscordColor.Cyan, - Description = message.message.Replace("\\n", "\n") + Color = DiscordColor.Red, + 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 messages = Database.GetAllMessages(); + if (messages.Count == 0) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "There are no messages registered." + }, true); + return; + } + + List listItems = []; + foreach (Database.Message message in messages) + { + listItems.Add("**" + message.identifier + "** Added by <@" + message.userID + ">\n"); + } + + List 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 listPages = []; + foreach (DiscordEmbedBuilder embed in embeds) + { + listPages.Add(new Page("", embed)); + } + + await command.Interaction.SendPaginatedResponseAsync(true, command.User, listPages); } } \ No newline at end of file