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 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));
}
}

View File

@ -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);

View File

@ -26,6 +26,7 @@ module.exports = (collection, page) => {
return {
state: state,
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
setTimeout(() => {
console.log(`Deleting interactionStorage with id: ${idString}`);
interaction.client.iStorage.delete(idString);
}, 300000);

View File

@ -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));

11
main.js
View File

@ -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;

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!');
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));
});
},
};