MVP for Paged /gifs Browser

This commit is contained in:
Skylar Grant 2024-09-26 08:09:09 -04:00
parent 8e5931e0d4
commit 7aa3d5d0a1
7 changed files with 72 additions and 58 deletions

View File

@ -1,22 +1,55 @@
const customEmbeds = require('../CustomModules/Embeds.js'); 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 = { module.exports = {
baseEvent(interaction) { baseEvent(interaction) {
console.log(interaction.component.customId);
switch (interaction.component.customId) { switch (interaction.component.customId) {
// Any of the gifsPage Buttons // Any of the gifsPage Buttons
case 'prevGifsPage' || 'nextGifsPage' : case 'prevGifsPage':
module.exports.gifsPage(interaction);
break;
case 'nextGifsPage':
module.exports.gifsPage(interaction);
break; break;
default: default:
return; return;
} }
}, },
gifsPage(interaction, gifs, page) { gifsPage(interaction) {
switch (buttonId) { let iStorage;
case 'prevGifsPage': if (interaction.client.iStorage.has(interaction.message.interaction.id)) {
return 'previous'; iStorage = interaction.client.iStorage.get(interaction.message.interaction.id)
case 'nextGifsPage': } else {
return 'next'; 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));
} }
} }

View File

@ -36,7 +36,7 @@ module.exports = {
return new MessageActionRow() return new MessageActionRow()
.addComponents(previousButton, confirmButton, nextButton, cancelButton); .addComponents(previousButton, confirmButton, nextButton, cancelButton);
}, },
gifsPageAR() { gifsPageAR(state) {
// Setup the buttons // Setup the buttons
const previousButton = new MessageButton() const previousButton = new MessageButton()
.setCustomId('prevGifsPage') .setCustomId('prevGifsPage')
@ -48,6 +48,15 @@ module.exports = {
.setLabel('➡️') .setLabel('➡️')
.setStyle('SECONDARY'); .setStyle('SECONDARY');
switch (state) {
case 'first':
previousButton.setDisabled(true);
break;
case 'last':
nextButton.setDisabled(true);
break;
}
// Put the buttons into an ActionRow // Put the buttons into an ActionRow
return new MessageActionRow() return new MessageActionRow()
.addComponents(previousButton, nextButton); .addComponents(previousButton, nextButton);

View File

@ -26,6 +26,7 @@ module.exports = (collection, page) => {
return { return {
state: state, state: state,
thisPage: thisPage, thisPage: thisPage,
pages: `Page ${page + 1}/${totalPages}` totalPages: totalPages,
pagesString: `${page + 1}/${totalPages}`
}; };
} }

View File

@ -7,6 +7,7 @@ module.exports = class InteractionStorage {
// Delete this from the interactionStorage after 5 minutes // Delete this from the interactionStorage after 5 minutes
setTimeout(() => { setTimeout(() => {
console.log(`Deleting interactionStorage with id: ${idString}`);
interaction.client.iStorage.delete(idString); interaction.client.iStorage.delete(idString);
}, 300000); }, 300000);

View File

@ -49,7 +49,7 @@ const functions = {
if (!client.iStorage) client.iStorage = new Discord.Collection(); if (!client.iStorage) client.iStorage = new Discord.Collection();
client.iStorage.clear(); client.iStorage.clear();
if (isDev) console.log('Interaction Storage Collection Built'); if (isDev) console.log('Interaction Storage Collection Built');
} },
// Create the collection of slash commands // Create the collection of slash commands
slashCommands(client) { slashCommands(client) {
if (!client.slashCommands) client.slashCommands = new Discord.Collection(); if (!client.slashCommands) client.slashCommands = new Discord.Collection();
@ -267,14 +267,14 @@ const functions = {
return { embeds: [pastasEmbed], ephemeral: true }; return { embeds: [pastasEmbed], ephemeral: true };
}, },
gifs(commandData, gifsString, state) { gifs(commandData, indexedGifs) {
const gifsEmbed = new Discord.MessageEmbed() const gifsEmbed = new Discord.MessageEmbed()
.setAuthor({name: commandData.command}) .setAuthor({name: commandData.command})
.setTimestamp() .setTimestamp()
.setFooter({text: commandData.author}) .setFooter({text: `Page: ${indexedGifs.pagesString}`})
.setDescription(gifsString); .setDescription(indexedGifs.gifsString);
const gifsPageAR = customEmbeds.gifsPageAR(state); const gifsPageAR = customEmbeds.gifsPageAR(indexedGifs.state);
return { embeds: [gifsEmbed], components: [gifsPageAR], ephemeral: true }; return { embeds: [gifsEmbed], components: [gifsPageAR], ephemeral: true };
}, },
text(commandData) { 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: { search: {
gifs(query, client) { gifs(query, client) {
const gifSearcher = new FuzzySearch(client.gifs.map(element => element.name)); const gifSearcher = new FuzzySearch(client.gifs.map(element => element.name));

11
main.js
View File

@ -57,14 +57,12 @@ client.once('ready', async () => {
client.on('interactionCreate', async interaction => { client.on('interactionCreate', async interaction => {
if (interaction.isCommand()) { if (interaction.isCommand()) {
if (isDev) { if (isDev) {
console.log(interaction); console.log('Interaction ID: ' + interaction.id);
} }
const { commandName } = interaction; const { commandName } = interaction;
const idString = `${interaction.channelId}${interaction.member.id}`; if (!client.iStorage.has(interaction.id)) {
new InteractionStorage(interaction.id, interaction);
if (!client.interactionStorage.has(idString)) {
new InteractionStorage(idString, interaction);
} }
if (client.slashCommands.has(commandName)) { if (client.slashCommands.has(commandName)) {
@ -76,7 +74,8 @@ client.on('interactionCreate', async interaction => {
} }
if (interaction.isButton()) { 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 // Get some meta info from strings
const index = strings.temp.gifIndex; const index = strings.temp.gifIndex;
const limit = strings.temp.gifLimit; const limit = strings.temp.gifLimit;

View File

@ -13,17 +13,20 @@ module.exports = {
interaction.reply('For some reason I don\'t have access to the collection of gifs. Sorry about that!'); interaction.reply('For some reason I don\'t have access to the collection of gifs. Sorry about that!');
resolve(); resolve();
} }
let gifList = indexer(interaction.client.gifs, 0); let iStorage = interaction.client.iStorage.get(interaction.id);
let gifsString = new String(); let indexedGifs = indexer(interaction.client.gifs, 0);
indexedGifs.gifsString = new String();
for (const gif of gifList.thisPage) { iStorage.page = 0;
gifsString += `[${gif.name}.gif](${gif.url})\n`;
for (const gif of indexedGifs.thisPage) {
indexedGifs.gifsString += `[${gif.name}.gif](${gif.url})\n`;
} }
const commandData = { const commandData = {
command: "/gifs", command: "/gifs",
author: interaction.member.displayName author: interaction.member.displayName
}; };
interaction.reply(fn.embeds.gifs(commandData, gifsString, gifList.state)); interaction.reply(fn.embeds.gifs(commandData, indexedGifs));
}); });
}, },
}; };