MVP for paged /requests

This commit is contained in:
Skylar Grant 2024-09-26 08:37:16 -04:00
parent 709c8cfab7
commit c3fa30ea64
4 changed files with 68 additions and 53 deletions

View File

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

View File

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

View File

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

View File

@ -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');
if (!interaction.client.requests) {
interaction.reply('For some reason I don\'t have access to the collection of requests. Sorry about that!');
return;
}
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 = {
author: interaction.user.tag,
command: interaction.commandName,
requests: [],
command: "/requests",
author: interaction.member.displayName
};
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,
});
}
}
interaction.reply(fn.embeds.requests(commandData));
interaction.reply(fn.embeds.requests(commandData, indexedRequests));
},
};