diff --git a/CustomModules/ButtonHandlers.js b/CustomModules/ButtonHandlers.js index 4e9011f..d0682b8 100644 --- a/CustomModules/ButtonHandlers.js +++ b/CustomModules/ButtonHandlers.js @@ -5,6 +5,14 @@ const fn = require('../functions.js'); module.exports = { baseEvent(interaction) { + let iStorage; + if (interaction.client.iStorage.has(interaction.message.interaction.id)) { + iStorage = interaction.client.iStorage.get(interaction.message.interaction.id) + } else { + iStorage = new InteractionStorage(interaction.message.interaction.id, interaction); + iStorage.page = 0; + } + if (interaction.user.id !== iStorage.userId) return; switch (interaction.component.customId) { // Any of the gifsPage Buttons case 'prevGifsPage': @@ -13,19 +21,18 @@ module.exports = { case 'nextGifsPage': module.exports.gifsPage(interaction); break; + case 'prevPastasPage': + module.exports.pastasPage(interaction); + break; + case 'nextPastasPage': + module.exports.pastasPage(interaction); + break; default: return; } }, gifsPage(interaction) { - let iStorage; - if (interaction.client.iStorage.has(interaction.message.interaction.id)) { - iStorage = interaction.client.iStorage.get(interaction.message.interaction.id) - } else { - iStorage = new InteractionStorage(interaction.message.interaction.id, interaction); - iStorage.page = 0; - } - console.log('Beginning Page: ' + iStorage.page); + const iStorage = interaction.client.iStorage.get(interaction.message.interaction.id); switch (interaction.component.customId) { case 'prevGifsPage': @@ -48,8 +55,32 @@ module.exports = { indexedGifs.gifsString += `[${gif.name}.gif](${gif.url})\n`; } - console.log('Ending Page: ' + iStorage.page); - interaction.update(fn.embeds.gifs({command: "/gifs", author: interaction.member.displayName}, indexedGifs)); + }, + pastasPage(interaction) { + const iStorage = interaction.client.iStorage.get(interaction.message.interaction.id); + + switch (interaction.component.customId) { + case 'prevPastasPage': + if (iStorage.page > 0) { + iStorage.page = iStorage.page - 1; + } + break; + case 'nextPastasPage': + if (iStorage.page < interaction.client.pastas.size / 10) { + iStorage.page = iStorage.page + 1; + } + break; + default: + break; + } + const indexedPastas = indexer(interaction.client.pastas, iStorage.page); + indexedPastas.pastasString = new String(); + + for (const pasta of indexedPastas.thisPage) { + indexedPastas.pastasString += `${pasta.name}.pasta\n`; + } + + interaction.update(fn.embeds.pastas({command: "/pastas", author: interaction.member.displayName}, indexedPastas)); } } \ No newline at end of file diff --git a/CustomModules/Embeds.js b/CustomModules/Embeds.js index bbb9bb3..036eb9b 100644 --- a/CustomModules/Embeds.js +++ b/CustomModules/Embeds.js @@ -87,30 +87,29 @@ module.exports = { return new MessageActionRow() .addComponents(previousButton, confirmButton, nextButton, cancelButton); }, - pastasPageAR() { + pastasPageAR(state) { // Setup the buttons const previousButton = new MessageButton() .setCustomId('prevPastasPage') .setLabel('⬅️') .setStyle('SECONDARY'); - const confirmButton = new MessageButton() - .setCustomId('confirmPastasPage') - .setLabel('✅') - .setStyle('PRIMARY'); - const nextButton = new MessageButton() .setCustomId('nextPastasPage') .setLabel('➡️') .setStyle('SECONDARY'); - const cancelButton = new MessageButton() - .setCustomId('cancelPastasPage') - .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); } } \ No newline at end of file diff --git a/CustomModules/InteractionStorage.js b/CustomModules/InteractionStorage.js index e1ace19..6103ecd 100644 --- a/CustomModules/InteractionStorage.js +++ b/CustomModules/InteractionStorage.js @@ -1,6 +1,7 @@ module.exports = class InteractionStorage { constructor(idString, interaction) { this.idString = idString; + this.userId = interaction.user.id; // Store in the client interaction.client.iStorage.set(idString, this); diff --git a/functions.js b/functions.js index f2e2507..7099c30 100644 --- a/functions.js +++ b/functions.js @@ -251,21 +251,15 @@ const functions = { .setTimestamp() .setFooter({text: commandData.author})]}; }, - pastas(commandData) { - const pastasArray = []; + pastas(commandData, indexedPastas) { const pastasEmbed = new Discord.MessageEmbed() .setAuthor({name: commandData.command}) .setTimestamp() - .setFooter({text: commandData.author}); + .setFooter({text: `Page: ${indexedPastas.pagesString}`}) + .setDescription(indexedPastas.pastasString); - for (const row of commandData.pastas) { - pastasArray.push(`#${row.id} - ${row.name}.pasta`); - } - - const pastasString = pastasArray.join('\n'); - pastasEmbed.setDescription(pastasString); - - return { embeds: [pastasEmbed], ephemeral: true }; + const pastasPageAR = customEmbeds.pastasPageAR(indexedPastas.state); + return { embeds: [pastasEmbed], components: [pastasPageAR], ephemeral: true }; }, gifs(commandData, indexedGifs) { const gifsEmbed = new Discord.MessageEmbed() diff --git a/slash-commands/gifs.js b/slash-commands/gifs.js index 51d1d9d..c372cc0 100644 --- a/slash-commands/gifs.js +++ b/slash-commands/gifs.js @@ -8,25 +8,23 @@ module.exports = { .setName('gifs') .setDescription('Get a list of currently saved GIFs.'), execute(interaction) { - return new Promise((resolve, reject) => { - if (!interaction.client.gifs) { - interaction.reply('For some reason I don\'t have access to the collection of gifs. Sorry about that!'); - resolve(); - } - let iStorage = interaction.client.iStorage.get(interaction.id); - let indexedGifs = indexer(interaction.client.gifs, 0); - indexedGifs.gifsString = new String(); + if (!interaction.client.gifs) { + interaction.reply('For some reason I don\'t have access to the collection of gifs. Sorry about that!'); + return; + } + let iStorage = interaction.client.iStorage.get(interaction.id); + let indexedGifs = indexer(interaction.client.gifs, 0); + indexedGifs.gifsString = new String(); - iStorage.page = 0; + iStorage.page = 0; - for (const gif of indexedGifs.thisPage) { - indexedGifs.gifsString += `[${gif.name}.gif](${gif.url})\n`; - } - const commandData = { - command: "/gifs", - author: interaction.member.displayName - }; - interaction.reply(fn.embeds.gifs(commandData, indexedGifs)); - }); - }, + for (const gif of indexedGifs.thisPage) { + indexedGifs.gifsString += `[${gif.name}.gif](${gif.url})\n`; + } + const commandData = { + command: "/gifs", + author: interaction.member.displayName + }; + interaction.reply(fn.embeds.gifs(commandData, indexedGifs)); + } }; \ No newline at end of file diff --git a/slash-commands/pastas.js b/slash-commands/pastas.js index baeb52d..8d43578 100644 --- a/slash-commands/pastas.js +++ b/slash-commands/pastas.js @@ -1,6 +1,7 @@ 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() @@ -11,23 +12,19 @@ module.exports = { interaction.reply({ content: 'For some reason I don\'t have access to the collection of copypastas. Sorry about that!', ephemeral: true }); return; } - const commandData = { - author: interaction.user.tag, - command: interaction.commandName, - pastas: [], - }; - const pastasMap = interaction.client.pastas.map(e => { - return { - id: e.id, - name: e.name, - }; - }); - for (const row of pastasMap) { - commandData.pastas.push({ - id: row.id, - name: row.name, - }); + let iStorage = interaction.client.iStorage.get(interaction.id); + let indexedPastas = indexer(interaction.client.pastas, 0); + indexedPastas.pastasString = new String(); + + iStorage.page = 0; + + for (const pasta of indexedPastas.thisPage) { + indexedPastas.pastasString += `${pasta.name}.pasta\n`; } - interaction.reply(fn.embeds.pastas(commandData)); + const commandData = { + command: "/pastas", + author: interaction.member.displayName + }; + interaction.reply(fn.embeds.pastas(commandData, indexedPastas)); }, }; \ No newline at end of file