From 646bb12a68fbccdfd85d7ba9c3e2eba70f12a880 Mon Sep 17 00:00:00 2001 From: Karl Essinger Date: Sun, 29 Sep 2024 18:17:33 +0200 Subject: [PATCH] Added pagination to say list --- Commands/CloseCommand.cs | 2 +- Commands/SayCommand.cs | 97 ++++++++++++++++++++++++---------------- 2 files changed, 60 insertions(+), 39 deletions(-) diff --git a/Commands/CloseCommand.cs b/Commands/CloseCommand.cs index 857ff81..5ef0701 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 112afdb..fa5e09b 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 async Task OnExecute(InteractionContext command, [Option("Identifier", "( // 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; - } + SendMessageList(command); + return; + } - List listItems = new List(); - foreach (Database.Message message in messages) + if (!Database.TryGetMessage(identifier.ToLower(), out Database.Message message)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder { - listItems.Add("**" + message.identifier + "** Added by <@" + message.userID + ">\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") + }); + } - LinkedList listMessages = Utilities.ParseListIntoMessages(listItems); - foreach (string listMessage in listMessages) + private static async void SendMessageList(InteractionContext command) + { + List messages = Database.GetAllMessages(); + if (messages.Count == 0) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder { - await command.CreateResponseAsync(new DiscordEmbedBuilder - { - Title = "Available messages: ", - Color = DiscordColor.Green, - Description = listMessage - }, true); - } + 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"); } - // Print specific message - else + + List embeds = []; + foreach (string message in Utilities.ParseListIntoMessages(listItems)) { - if (!Database.TryGetMessage(identifier.ToLower(), out Database.Message message)) + embeds.Add(new DiscordEmbedBuilder { - await command.CreateResponseAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Red, - Description = "There is no message with that identifier." - }, true); - return; - } + Title = "Available messages:", + Color = DiscordColor.Green, + Description = message + }); + } - await command.CreateResponseAsync(new DiscordEmbedBuilder + // Add the footers + for (int i = 0; i < embeds.Count; i++) + { + embeds[i].Footer = new DiscordEmbedBuilder.EmbedFooter { - Color = DiscordColor.Cyan, - Description = message.message.Replace("\\n", "\n") - }); + 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