From c3fa30ea649440b20585ea74cb7e2614d0c1bf13 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Thu, 26 Sep 2024 08:37:16 -0400 Subject: [PATCH] MVP for paged /requests --- CustomModules/ButtonHandlers.js | 33 +++++++++++++++++++++++ CustomModules/Embeds.js | 21 +++++++-------- functions.js | 19 ++++--------- slash-commands/requests.js | 48 ++++++++++++++------------------- 4 files changed, 68 insertions(+), 53 deletions(-) diff --git a/CustomModules/ButtonHandlers.js b/CustomModules/ButtonHandlers.js index d0682b8..f28b878 100644 --- a/CustomModules/ButtonHandlers.js +++ b/CustomModules/ButtonHandlers.js @@ -2,6 +2,7 @@ const customEmbeds = require('../CustomModules/Embeds.js'); const InteractionStorage = require('../CustomModules/InteractionStorage.js'); const indexer = require('../CustomModules/Indexer.js'); const fn = require('../functions.js'); +const requests = require('../slash-commands/requests.js'); module.exports = { baseEvent(interaction) { @@ -27,6 +28,12 @@ module.exports = { case 'nextPastasPage': module.exports.pastasPage(interaction); break; + case 'prevRequestsPage': + module.exports.requestsPage(interaction); + break; + case 'nextRequestsPage': + module.exports.requestsPage(interaction); + break; default: return; } @@ -82,5 +89,31 @@ module.exports = { } interaction.update(fn.embeds.pastas({command: "/pastas", author: interaction.member.displayName}, indexedPastas)); + }, + requestsPage(interaction) { + const iStorage = interaction.client.iStorage.get(interaction.message.interaction.id); + + switch (interaction.component.customId) { + case 'prevRequestsPage': + if (iStorage.page > 0) { + iStorage.page = iStorage.page - 1; + } + break; + case 'nextRequestsPage': + if (iStorage.page < interaction.client.requests.size / 10) { + iStorage.page = iStorage.page + 1; + } + break; + default: + break; + } + const indexedRequests = indexer(interaction.client.requests, iStorage.page); + indexedRequests.requestsString = new String(); + + for (const request of indexedRequests.thisPage) { + indexedRequests.requestsString += `[${request.id}]: ${request.request} (submitted by ${request.author})\n`; + } + + interaction.update(fn.embeds.requests({command: "/requests", author: interaction.member.displayName}, indexedRequests)); } } \ No newline at end of file diff --git a/CustomModules/Embeds.js b/CustomModules/Embeds.js index 036eb9b..3264174 100644 --- a/CustomModules/Embeds.js +++ b/CustomModules/Embeds.js @@ -61,31 +61,30 @@ module.exports = { return new MessageActionRow() .addComponents(previousButton, nextButton); }, - requestsPageAR() { + requestsPageAR(state) { // Setup the buttons const previousButton = new MessageButton() .setCustomId('prevRequestsPage') .setLabel('⬅️') .setStyle('SECONDARY'); - const confirmButton = new MessageButton() - .setCustomId('confirmRequestsPage') - .setLabel('✅') - .setStyle('PRIMARY'); - const nextButton = new MessageButton() .setCustomId('nextRequestsPage') .setLabel('➡️') .setStyle('SECONDARY'); - const cancelButton = new MessageButton() - .setCustomId('cancelRequestsPage') - .setLabel('❌') - .setStyle('DANGER'); + switch (state) { + case 'first': + previousButton.setDisabled(true); + break; + case 'last': + nextButton.setDisabled(true); + break; + } // Put the buttons into an ActionRow return new MessageActionRow() - .addComponents(previousButton, confirmButton, nextButton, cancelButton); + .addComponents(previousButton, nextButton); }, pastasPageAR(state) { // Setup the buttons diff --git a/functions.js b/functions.js index 7099c30..15b8caa 100644 --- a/functions.js +++ b/functions.js @@ -278,24 +278,15 @@ const functions = { .setTimestamp() .setFooter({text: commandData.author})]}; }, - requests(commandData) { + requests(commandData, indexedRequests) { const requestsEmbed = new Discord.MessageEmbed() .setAuthor({name: commandData.command}) .setTimestamp() - .setFooter({text: commandData.author}); + .setFooter({text: `Page: ${indexedRequests.pagesString}`}) + .setDescription(indexedRequests.requestsString); - const requestsArray = []; - - for (const row of commandData.requests) { - requestsArray.push( - `**#${row.id} - ${row.author}**`, - `Request: ${row.request}` - ); - } - - requestsEmbed.setDescription(requestsArray.join('\n')); - - return { embeds: [requestsEmbed], ephemeral: true }; + const requestsPageAR = customEmbeds.requestsPageAR(indexedRequests.state); + return { embeds: [requestsEmbed], components: [requestsPageAR], ephemeral: true }; }, strain(strainInfo, interaction) { const strainEmbed = new Discord.MessageEmbed() diff --git a/slash-commands/requests.js b/slash-commands/requests.js index 039504e..fbc8b3b 100644 --- a/slash-commands/requests.js +++ b/slash-commands/requests.js @@ -1,39 +1,31 @@ const { SlashCommandBuilder } = require('@discordjs/builders'); const { config } = require('dotenv'); const fn = require('../functions.js'); +const indexer = require('../CustomModules/Indexer.js'); module.exports = { data: new SlashCommandBuilder() .setName('requests') - .setDescription('Get a list of Active requests from the database') - .addStringOption(option => - option - .setName('page') - .setDescription('Page Number') - .setRequired(true)), + .setDescription('Get a list of Active requests from the database'), async execute(interaction) { - const pageNum = interaction.options.getString('page'); - const commandData = { - author: interaction.user.tag, - command: interaction.commandName, - requests: [], - }; - const requestsMap = interaction.client.requests.map(e => { - return { - id: e.id, - author: e.author, - request: e.request, - }; - }); - for (let i = ( 10 * ( pageNum - 1 ) ); i < ( 10 * pageNum ); i++) { - if (requestsMap[i] != undefined) { - commandData.requests.push({ - id: requestsMap[i].id, - author: requestsMap[i].author, - request: requestsMap[i].request, - }); - } + if (!interaction.client.requests) { + interaction.reply('For some reason I don\'t have access to the collection of requests. Sorry about that!'); + return; } - interaction.reply(fn.embeds.requests(commandData)); + let iStorage = interaction.client.iStorage.get(interaction.id); + let indexedRequests = indexer(interaction.client.requests, 0); + indexedRequests.requestsString = new String(); + + iStorage.page = 0; + + for (const request of indexedRequests.thisPage) { + indexedRequests.requestsString += `[${request.id}]: ${request.request} (submitted by ${request.author})\n`; + } + + const commandData = { + command: "/requests", + author: interaction.member.displayName + }; + interaction.reply(fn.embeds.requests(commandData, indexedRequests)); }, }; \ No newline at end of file