diff --git a/CustomModules/ButtonHandlers.js b/CustomModules/ButtonHandlers.js index a9cfa6c..4e9011f 100644 --- a/CustomModules/ButtonHandlers.js +++ b/CustomModules/ButtonHandlers.js @@ -1,22 +1,55 @@ const customEmbeds = require('../CustomModules/Embeds.js'); +const InteractionStorage = require('../CustomModules/InteractionStorage.js'); +const indexer = require('../CustomModules/Indexer.js'); +const fn = require('../functions.js'); module.exports = { baseEvent(interaction) { - console.log(interaction.component.customId); switch (interaction.component.customId) { // Any of the gifsPage Buttons - case 'prevGifsPage' || 'nextGifsPage' : + case 'prevGifsPage': + module.exports.gifsPage(interaction); + break; + case 'nextGifsPage': + module.exports.gifsPage(interaction); break; default: return; } }, - gifsPage(interaction, gifs, page) { - switch (buttonId) { - case 'prevGifsPage': - return 'previous'; - case 'nextGifsPage': - return 'next'; + 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); + + switch (interaction.component.customId) { + case 'prevGifsPage': + if (iStorage.page > 0) { + iStorage.page = iStorage.page - 1; + } + break; + case 'nextGifsPage': + if (iStorage.page < interaction.client.gifs.size / 10) { + iStorage.page = iStorage.page + 1; + } + break; + default: + break; + } + const indexedGifs = indexer(interaction.client.gifs, iStorage.page); + indexedGifs.gifsString = new String(); + + for (const gif of indexedGifs.thisPage) { + 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)); } } \ No newline at end of file diff --git a/CustomModules/Embeds.js b/CustomModules/Embeds.js index cc057da..bbb9bb3 100644 --- a/CustomModules/Embeds.js +++ b/CustomModules/Embeds.js @@ -36,7 +36,7 @@ module.exports = { return new MessageActionRow() .addComponents(previousButton, confirmButton, nextButton, cancelButton); }, - gifsPageAR() { + gifsPageAR(state) { // Setup the buttons const previousButton = new MessageButton() .setCustomId('prevGifsPage') @@ -48,6 +48,15 @@ module.exports = { .setLabel('➡️') .setStyle('SECONDARY'); + 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, nextButton); diff --git a/CustomModules/Indexer.js b/CustomModules/Indexer.js index 8a3960f..dacb3bb 100644 --- a/CustomModules/Indexer.js +++ b/CustomModules/Indexer.js @@ -26,6 +26,7 @@ module.exports = (collection, page) => { return { state: state, thisPage: thisPage, - pages: `Page ${page + 1}/${totalPages}` + totalPages: totalPages, + pagesString: `${page + 1}/${totalPages}` }; } \ No newline at end of file diff --git a/CustomModules/InteractionStorage.js b/CustomModules/InteractionStorage.js index d5cf06a..e1ace19 100644 --- a/CustomModules/InteractionStorage.js +++ b/CustomModules/InteractionStorage.js @@ -7,6 +7,7 @@ module.exports = class InteractionStorage { // Delete this from the interactionStorage after 5 minutes setTimeout(() => { + console.log(`Deleting interactionStorage with id: ${idString}`); interaction.client.iStorage.delete(idString); }, 300000); diff --git a/functions.js b/functions.js index 15a5e5a..f2e2507 100644 --- a/functions.js +++ b/functions.js @@ -49,7 +49,7 @@ const functions = { if (!client.iStorage) client.iStorage = new Discord.Collection(); client.iStorage.clear(); if (isDev) console.log('Interaction Storage Collection Built'); - } + }, // Create the collection of slash commands slashCommands(client) { if (!client.slashCommands) client.slashCommands = new Discord.Collection(); @@ -267,14 +267,14 @@ const functions = { return { embeds: [pastasEmbed], ephemeral: true }; }, - gifs(commandData, gifsString, state) { + gifs(commandData, indexedGifs) { const gifsEmbed = new Discord.MessageEmbed() .setAuthor({name: commandData.command}) .setTimestamp() - .setFooter({text: commandData.author}) - .setDescription(gifsString); + .setFooter({text: `Page: ${indexedGifs.pagesString}`}) + .setDescription(indexedGifs.gifsString); - const gifsPageAR = customEmbeds.gifsPageAR(state); + const gifsPageAR = customEmbeds.gifsPageAR(indexedGifs.state); return { embeds: [gifsEmbed], components: [gifsPageAR], ephemeral: true }; }, text(commandData) { @@ -599,38 +599,6 @@ const functions = { } } }, - openAI: { - chatPrompt(userPrompt) { - return new Promise(async (resolve, reject) => { - const response = await openai.chat.completions.create({ - messages: [{ - role: 'user', - content: userPrompt - }], - model: strings.ai.chatModel - }).catch(e => { - reject(e); - return null; - }); - resolve(response); - }); - }, - imagePrompt(userPrompt, size, userId) { - return new Promise(async (resolve, reject) => { - try { - const response = await openai.createImage({ - prompt: userPrompt, - size: size, - user: userId - }); - resolve(response.data.data[0].url); - } catch (e) { - reject(e); - return; - } - }); - } - }, search: { gifs(query, client) { const gifSearcher = new FuzzySearch(client.gifs.map(element => element.name)); diff --git a/main.js b/main.js index 304c7be..147f500 100644 --- a/main.js +++ b/main.js @@ -57,14 +57,12 @@ client.once('ready', async () => { client.on('interactionCreate', async interaction => { if (interaction.isCommand()) { if (isDev) { - console.log(interaction); + console.log('Interaction ID: ' + interaction.id); } const { commandName } = interaction; - const idString = `${interaction.channelId}${interaction.member.id}`; - - if (!client.interactionStorage.has(idString)) { - new InteractionStorage(idString, interaction); + if (!client.iStorage.has(interaction.id)) { + new InteractionStorage(interaction.id, interaction); } if (client.slashCommands.has(commandName)) { @@ -76,7 +74,8 @@ client.on('interactionCreate', async interaction => { } if (interaction.isButton()) { - if (isDev) console.log(interaction.id); + if (isDev) console.log('Origin Interaction ID: ' + interaction.message.interaction.id); + if (isDev) console.log('Button ID: ' + interaction.component.customId); // Get some meta info from strings const index = strings.temp.gifIndex; const limit = strings.temp.gifLimit; diff --git a/slash-commands/gifs.js b/slash-commands/gifs.js index 00a30f0..51d1d9d 100644 --- a/slash-commands/gifs.js +++ b/slash-commands/gifs.js @@ -13,17 +13,20 @@ module.exports = { interaction.reply('For some reason I don\'t have access to the collection of gifs. Sorry about that!'); resolve(); } - let gifList = indexer(interaction.client.gifs, 0); - let gifsString = new String(); + let iStorage = interaction.client.iStorage.get(interaction.id); + let indexedGifs = indexer(interaction.client.gifs, 0); + indexedGifs.gifsString = new String(); - for (const gif of gifList.thisPage) { - gifsString += `[${gif.name}.gif](${gif.url})\n`; + 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, gifsString, gifList.state)); + interaction.reply(fn.embeds.gifs(commandData, indexedGifs)); }); }, }; \ No newline at end of file