chore: split db file into multiple files and centralized pagination

This commit is contained in:
Ahmad 2025-04-16 17:57:17 -04:00
parent 2f5c3499e7
commit be8df5f6a2
No known key found for this signature in database
GPG key ID: 8FD8A93530D182BF
11 changed files with 1612 additions and 1401 deletions

View file

@ -17,6 +17,7 @@ import {
import { postFactOfTheDay } from '@/util/factManager.js';
import { loadConfig } from '@/util/configLoader.js';
import { SubcommandCommand } from '@/types/CommandTypes.js';
import { createPaginationButtons } from '@/util/helpers.js';
const command: SubcommandCommand = {
data: new SlashCommandBuilder()
@ -197,6 +198,7 @@ const command: SubcommandCommand = {
return;
}
const FACTS_PER_PAGE = 5;
const pendingFacts = await getPendingFacts();
if (pendingFacts.length === 0) {
@ -206,20 +208,85 @@ const command: SubcommandCommand = {
return;
}
const embed = new EmbedBuilder()
.setTitle('Pending Facts')
.setColor(0x0099ff)
.setDescription(
pendingFacts
.map((fact) => {
return `**ID #${fact.id}**\n${fact.content}\nSubmitted by: <@${fact.addedBy}>\nSource: ${fact.source || 'Not provided'}`;
})
.join('\n\n'),
)
.setTimestamp();
const pages: EmbedBuilder[] = [];
for (let i = 0; i < pendingFacts.length; i += FACTS_PER_PAGE) {
const pageFacts = pendingFacts.slice(i, i + FACTS_PER_PAGE);
await interaction.editReply({
embeds: [embed],
const embed = new EmbedBuilder()
.setTitle('Pending Facts')
.setColor(0x0099ff)
.setDescription(
pageFacts
.map((fact) => {
return `**ID #${fact.id}**\n${fact.content}\nSubmitted by: <@${fact.addedBy}>\nSource: ${fact.source || 'Not provided'}`;
})
.join('\n\n'),
)
.setTimestamp();
pages.push(embed);
}
let currentPage = 0;
const message = await interaction.editReply({
embeds: [pages[currentPage]],
components: [createPaginationButtons(pages.length, currentPage)],
});
if (pages.length <= 1) return;
const collector = message.createMessageComponentCollector({
time: 300000,
});
collector.on('collect', async (i) => {
if (i.user.id !== interaction.user.id) {
await i.reply({
content: 'These controls are not for you!',
flags: ['Ephemeral'],
});
return;
}
if (i.isButton()) {
switch (i.customId) {
case 'first':
currentPage = 0;
break;
case 'prev':
if (currentPage > 0) currentPage--;
break;
case 'next':
if (currentPage < pages.length - 1) currentPage++;
break;
case 'last':
currentPage = pages.length - 1;
break;
}
}
if (i.isStringSelectMenu()) {
const selected = parseInt(i.values[0]);
if (!isNaN(selected) && selected >= 0 && selected < pages.length) {
currentPage = selected;
}
}
await i.update({
embeds: [pages[currentPage]],
components: [createPaginationButtons(pages.length, currentPage)],
});
});
collector.on('end', async () => {
if (message) {
try {
await interaction.editReply({ components: [] });
} catch (error) {
console.error('Error removing components:', error);
}
}
});
} else if (subcommand === 'post') {
if (

View file

@ -17,6 +17,7 @@ import {
formatWinnerMentions,
builder,
} from '@/util/giveaways/giveawayManager.js';
import { createPaginationButtons } from '@/util/helpers.js';
const command: SubcommandCommand = {
data: new SlashCommandBuilder()
@ -97,37 +98,103 @@ async function handleCreateGiveaway(interaction: ChatInputCommandInteraction) {
*/
async function handleListGiveaways(interaction: ChatInputCommandInteraction) {
await interaction.deferReply();
const GIVEAWAYS_PER_PAGE = 5;
const activeGiveaways = await getActiveGiveaways();
try {
const activeGiveaways = await getActiveGiveaways();
if (activeGiveaways.length === 0) {
await interaction.editReply('There are no active giveaways at the moment.');
return;
if (activeGiveaways.length === 0) {
await interaction.editReply({
content: 'There are no active giveaways at the moment.',
});
return;
}
const pages: EmbedBuilder[] = [];
for (let i = 0; i < activeGiveaways.length; i += GIVEAWAYS_PER_PAGE) {
const pageGiveaways = activeGiveaways.slice(i, i + GIVEAWAYS_PER_PAGE);
const embed = new EmbedBuilder()
.setTitle('🎉 Active Giveaways')
.setColor(0x00ff00)
.setDescription('Here are the currently active giveaways:')
.setTimestamp();
pageGiveaways.forEach((giveaway) => {
embed.addFields({
name: `${giveaway.prize} (ID: ${giveaway.id})`,
value: [
`**Hosted by:** <@${giveaway.hostId}>`,
`**Winners:** ${giveaway.winnerCount}`,
`**Ends:** <t:${Math.floor(giveaway.endAt.getTime() / 1000)}:R>`,
`**Entries:** ${giveaway.participants?.length || 0}`,
`[Jump to Giveaway](https://discord.com/channels/${interaction.guildId}/${giveaway.channelId}/${giveaway.messageId})`,
].join('\n'),
inline: false,
});
});
pages.push(embed);
}
let currentPage = 0;
const message = await interaction.editReply({
embeds: [pages[currentPage]],
components: [createPaginationButtons(pages.length, currentPage)],
});
const collector = message.createMessageComponentCollector({
time: 300000,
});
collector.on('collect', async (i) => {
if (i.user.id !== interaction.user.id) {
await i.reply({
content: 'You cannot use these buttons.',
ephemeral: true,
});
return;
}
if (i.isButton()) {
switch (i.customId) {
case 'first':
currentPage = 0;
break;
case 'prev':
if (currentPage > 0) currentPage--;
break;
case 'next':
if (currentPage < pages.length - 1) currentPage++;
break;
case 'last':
currentPage = pages.length - 1;
break;
}
await i.update({
embeds: [pages[currentPage]],
components: [createPaginationButtons(pages.length, currentPage)],
});
}
});
collector.on('end', async () => {
try {
await interaction.editReply({
components: [],
});
} catch (error) {
console.error('Error removing components:', error);
}
});
} catch (error) {
console.error('Error fetching giveaways:', error);
await interaction.editReply({
content: 'There was an error fetching the giveaways.',
});
}
const embed = new EmbedBuilder()
.setTitle('🎉 Active Giveaways')
.setColor(0x00ff00)
.setTimestamp();
const giveawayDetails = activeGiveaways.map((g) => {
const channel = interaction.guild?.channels.cache.get(g.channelId);
const channelMention = channel ? `<#${channel.id}>` : 'Unknown channel';
return [
`**Prize**: ${g.prize}`,
`**ID**: ${g.id}`,
`**Winners**: ${g.winnerCount}`,
`**Ends**: <t:${Math.floor(g.endAt.getTime() / 1000)}:R>`,
`**Channel**: ${channelMention}`,
`**Entries**: ${g.participants?.length || 0}`,
'───────────────────',
].join('\n');
});
embed.setDescription(giveawayDetails.join('\n'));
await interaction.editReply({ embeds: [embed] });
}
/**

View file

@ -1,9 +1,7 @@
import {
SlashCommandBuilder,
EmbedBuilder,
ButtonBuilder,
ActionRowBuilder,
ButtonStyle,
StringSelectMenuBuilder,
APIEmbed,
JSONEncodable,
@ -11,6 +9,7 @@ import {
import { OptionsCommand } from '@/types/CommandTypes.js';
import { getLevelLeaderboard } from '@/db/db.js';
import { createPaginationButtons } from '@/util/helpers.js';
const command: OptionsCommand = {
data: new SlashCommandBuilder()
@ -78,18 +77,7 @@ const command: OptionsCommand = {
let currentPage = 0;
const getButtonActionRow = () =>
new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setCustomId('previous')
.setLabel('Previous')
.setStyle(ButtonStyle.Primary)
.setDisabled(currentPage === 0),
new ButtonBuilder()
.setCustomId('next')
.setLabel('Next')
.setStyle(ButtonStyle.Primary)
.setDisabled(currentPage === pages.length - 1),
);
createPaginationButtons(pages.length, currentPage);
const getSelectMenuRow = () => {
const options = pages.map((_, index) => ({
@ -119,7 +107,7 @@ const command: OptionsCommand = {
if (pages.length <= 1) return;
const collector = message.createMessageComponentCollector({
time: 60000,
time: 300000,
});
collector.on('collect', async (i) => {
@ -132,10 +120,19 @@ const command: OptionsCommand = {
}
if (i.isButton()) {
if (i.customId === 'previous' && currentPage > 0) {
currentPage--;
} else if (i.customId === 'next' && currentPage < pages.length - 1) {
currentPage++;
switch (i.customId) {
case 'first':
currentPage = 0;
break;
case 'prev':
if (currentPage > 0) currentPage--;
break;
case 'next':
if (currentPage < pages.length - 1) currentPage++;
break;
case 'last':
currentPage = pages.length - 1;
break;
}
}

View file

@ -1,9 +1,7 @@
import {
SlashCommandBuilder,
EmbedBuilder,
ButtonBuilder,
ActionRowBuilder,
ButtonStyle,
StringSelectMenuBuilder,
APIEmbed,
JSONEncodable,
@ -11,6 +9,7 @@ import {
import { getAllMembers } from '@/db/db.js';
import { Command } from '@/types/CommandTypes.js';
import { createPaginationButtons } from '@/util/helpers.js';
const command: Command = {
data: new SlashCommandBuilder()
@ -42,18 +41,7 @@ const command: Command = {
let currentPage = 0;
const getButtonActionRow = () =>
new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setCustomId('previous')
.setLabel('Previous')
.setStyle(ButtonStyle.Primary)
.setDisabled(currentPage === 0),
new ButtonBuilder()
.setCustomId('next')
.setLabel('Next')
.setStyle(ButtonStyle.Primary)
.setDisabled(currentPage === pages.length - 1),
);
createPaginationButtons(pages.length, currentPage);
const getSelectMenuRow = () => {
const options = pages.map((_, index) => ({
@ -85,7 +73,7 @@ const command: Command = {
if (pages.length <= 1) return;
const collector = message.createMessageComponentCollector({
time: 60000,
time: 300000,
});
collector.on('collect', async (i) => {
@ -98,10 +86,19 @@ const command: Command = {
}
if (i.isButton()) {
if (i.customId === 'previous' && currentPage > 0) {
currentPage--;
} else if (i.customId === 'next' && currentPage < pages.length - 1) {
currentPage++;
switch (i.customId) {
case 'first':
currentPage = 0;
break;
case 'prev':
if (currentPage > 0) currentPage--;
break;
case 'next':
if (currentPage < pages.length - 1) currentPage++;
break;
case 'last':
currentPage = pages.length - 1;
break;
}
}