Skip to content

Commit

Permalink
Added pagination to say list
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlOfDuty committed Sep 29, 2024
1 parent f2a02bb commit 646bb12
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Commands/CloseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _))
Expand Down
97 changes: 59 additions & 38 deletions Commands/SayCommand.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<Database.Message> 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<string> listItems = new List<string>();
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<string> listMessages = Utilities.ParseListIntoMessages(listItems);
foreach (string listMessage in listMessages)
private static async void SendMessageList(InteractionContext command)
{
List<Database.Message> 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<string> listItems = [];
foreach (Database.Message message in messages)
{
listItems.Add("**" + message.identifier + "** Added by <@" + message.userID + ">\n");
}
// Print specific message
else

List<DiscordEmbedBuilder> 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<Page> listPages = [];
foreach (DiscordEmbedBuilder embed in embeds)
{
listPages.Add(new Page("", embed));
}

await command.Interaction.SendPaginatedResponseAsync(true, command.User, listPages);
}
}

0 comments on commit 646bb12

Please sign in to comment.