v3.3.3: Paged Commands #20

Merged
voidf1sh merged 15 commits from v3.3.3 into main 2024-09-26 13:24:55 +00:00
4 changed files with 68 additions and 53 deletions
Showing only changes of commit c3fa30ea64 - Show all commits

View File

@ -2,6 +2,7 @@ const customEmbeds = require('../CustomModules/Embeds.js');
const InteractionStorage = require('../CustomModules/InteractionStorage.js'); const InteractionStorage = require('../CustomModules/InteractionStorage.js');
const indexer = require('../CustomModules/Indexer.js'); const indexer = require('../CustomModules/Indexer.js');
const fn = require('../functions.js'); const fn = require('../functions.js');
const requests = require('../slash-commands/requests.js');
module.exports = { module.exports = {
baseEvent(interaction) { baseEvent(interaction) {
@ -27,6 +28,12 @@ module.exports = {
case 'nextPastasPage': case 'nextPastasPage':
module.exports.pastasPage(interaction); module.exports.pastasPage(interaction);
break; break;
case 'prevRequestsPage':
module.exports.requestsPage(interaction);
break;
case 'nextRequestsPage':
module.exports.requestsPage(interaction);
break;
default: default:
return; return;
} }
@ -82,5 +89,31 @@ module.exports = {
} }
interaction.update(fn.embeds.pastas({command: "/pastas", author: interaction.member.displayName}, indexedPastas)); 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() return new MessageActionRow()
.addComponents(previousButton, nextButton); .addComponents(previousButton, nextButton);
}, },
requestsPageAR() { requestsPageAR(state) {
// Setup the buttons // Setup the buttons
const previousButton = new MessageButton() const previousButton = new MessageButton()
.setCustomId('prevRequestsPage') .setCustomId('prevRequestsPage')
.setLabel('⬅️') .setLabel('⬅️')
.setStyle('SECONDARY'); .setStyle('SECONDARY');
const confirmButton = new MessageButton()
.setCustomId('confirmRequestsPage')
.setLabel('✅')
.setStyle('PRIMARY');
const nextButton = new MessageButton() const nextButton = new MessageButton()
.setCustomId('nextRequestsPage') .setCustomId('nextRequestsPage')
.setLabel('➡️') .setLabel('➡️')
.setStyle('SECONDARY'); .setStyle('SECONDARY');
const cancelButton = new MessageButton() switch (state) {
.setCustomId('cancelRequestsPage') case 'first':
.setLabel('❌') previousButton.setDisabled(true);
.setStyle('DANGER'); 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, confirmButton, nextButton, cancelButton); .addComponents(previousButton, nextButton);
}, },
pastasPageAR(state) { pastasPageAR(state) {
// Setup the buttons // Setup the buttons

View File

@ -278,24 +278,15 @@ const functions = {
.setTimestamp() .setTimestamp()
.setFooter({text: commandData.author})]}; .setFooter({text: commandData.author})]};
}, },
requests(commandData) { requests(commandData, indexedRequests) {
const requestsEmbed = new Discord.MessageEmbed() const requestsEmbed = new Discord.MessageEmbed()
.setAuthor({name: commandData.command}) .setAuthor({name: commandData.command})
.setTimestamp() .setTimestamp()
.setFooter({text: commandData.author}); .setFooter({text: `Page: ${indexedRequests.pagesString}`})
.setDescription(indexedRequests.requestsString);
const requestsArray = []; const requestsPageAR = customEmbeds.requestsPageAR(indexedRequests.state);
return { embeds: [requestsEmbed], components: [requestsPageAR], ephemeral: true };
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 };
}, },
strain(strainInfo, interaction) { strain(strainInfo, interaction) {
const strainEmbed = new Discord.MessageEmbed() const strainEmbed = new Discord.MessageEmbed()

View File

@ -1,39 +1,31 @@
const { SlashCommandBuilder } = require('@discordjs/builders'); const { SlashCommandBuilder } = require('@discordjs/builders');
const { config } = require('dotenv'); const { config } = require('dotenv');
const fn = require('../functions.js'); const fn = require('../functions.js');
const indexer = require('../CustomModules/Indexer.js');
module.exports = { module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName('requests') .setName('requests')
.setDescription('Get a list of Active requests from the database') .setDescription('Get a list of Active requests from the database'),
.addStringOption(option =>
option
.setName('page')
.setDescription('Page Number')
.setRequired(true)),
async execute(interaction) { 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 = { const commandData = {
author: interaction.user.tag, command: "/requests",
command: interaction.commandName, author: interaction.member.displayName
requests: [],
}; };
const requestsMap = interaction.client.requests.map(e => { interaction.reply(fn.embeds.requests(commandData, indexedRequests));
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));
}, },
}; };