Working on storing interaction data in the client

This commit is contained in:
Skylar Grant 2024-09-25 23:22:22 -04:00
parent e4c389be71
commit 8e5931e0d4
8 changed files with 188 additions and 40 deletions

View File

@ -0,0 +1,22 @@
const customEmbeds = require('../CustomModules/Embeds.js');
module.exports = {
baseEvent(interaction) {
console.log(interaction.component.customId);
switch (interaction.component.customId) {
// Any of the gifsPage Buttons
case 'prevGifsPage' || 'nextGifsPage' :
break;
default:
return;
}
},
gifsPage(interaction, gifs, page) {
switch (buttonId) {
case 'prevGifsPage':
return 'previous';
case 'nextGifsPage':
return 'next';
}
}
}

View File

@ -1,5 +1,7 @@
const { MessageActionRow, MessageButton } = require('discord.js');
module.exports = { module.exports = {
gifSearchAR() { gifSearchAR(state) {
// Setup the buttons // Setup the buttons
const previousButton = new MessageButton() const previousButton = new MessageButton()
.setCustomId('prevGif') .setCustomId('prevGif')
@ -21,6 +23,83 @@ module.exports = {
.setLabel('❌') .setLabel('❌')
.setStyle('DANGER'); .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);
},
gifsPageAR() {
// Setup the buttons
const previousButton = new MessageButton()
.setCustomId('prevGifsPage')
.setLabel('⬅️')
.setStyle('SECONDARY');
const nextButton = new MessageButton()
.setCustomId('nextGifsPage')
.setLabel('➡️')
.setStyle('SECONDARY');
// Put the buttons into an ActionRow
return new MessageActionRow()
.addComponents(previousButton, nextButton);
},
requestsPageAR() {
// 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');
// Put the buttons into an ActionRow
return new MessageActionRow()
.addComponents(previousButton, confirmButton, nextButton, cancelButton);
},
pastasPageAR() {
// Setup the buttons
const previousButton = new MessageButton()
.setCustomId('prevPastasPage')
.setLabel('⬅️')
.setStyle('SECONDARY');
const confirmButton = new MessageButton()
.setCustomId('confirmPastasPage')
.setLabel('✅')
.setStyle('PRIMARY');
const nextButton = new MessageButton()
.setCustomId('nextPastasPage')
.setLabel('➡️')
.setStyle('SECONDARY');
const cancelButton = new MessageButton()
.setCustomId('cancelPastasPage')
.setLabel('❌')
.setStyle('DANGER');
// 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, confirmButton, nextButton, cancelButton);

31
CustomModules/Indexer.js Normal file
View File

@ -0,0 +1,31 @@
module.exports = (collection, page) => {
const itemsPerPage = 10;
const index = page * itemsPerPage;
const totalPages = Math.ceil(collection.size / itemsPerPage);
let state = page === 0 ? 'first' : 'middle';
const thisPage = new Array();
// Map the Djs Collection to an Array
const collectionArray = collection.map((command) => command);
for (let i = index; i < index + itemsPerPage; i++) {
if (collectionArray[i]) {
thisPage.push(collectionArray[i]);
} else {
state = 'last';
break;
}
if (i === collectionArray.size - 1) {
state = 'last';
break;
}
}
return {
state: state,
thisPage: thisPage,
pages: `Page ${page + 1}/${totalPages}`
};
}

View File

@ -0,0 +1,15 @@
module.exports = class InteractionStorage {
constructor(idString, interaction) {
this.idString = idString;
// Store in the client
interaction.client.iStorage.set(idString, this);
// Delete this from the interactionStorage after 5 minutes
setTimeout(() => {
interaction.client.iStorage.delete(idString);
}, 300000);
return this;
}
}

View File

@ -26,8 +26,6 @@ module.exports = {
return this; return this;
} }
} }
this.isValid = validCommands.includes(this.command);
return this; return this;
} }
}, },

View File

@ -1,7 +1,6 @@
/* eslint-disable comma-dangle */ /* eslint-disable comma-dangle */
// dotenv for handling environment variables // dotenv for handling environment variables
const dotenv = require('dotenv'); const dotenv = require('dotenv').config();
dotenv.config();
// Assignment of environment variables for database access // Assignment of environment variables for database access
const dbHost = process.env.dbHost; const dbHost = process.env.dbHost;
const dbUser = process.env.dbUser; const dbUser = process.env.dbUser;
@ -9,7 +8,6 @@ const dbName = process.env.dbName;
const dbPass = process.env.dbPass; const dbPass = process.env.dbPass;
const dbPort = process.env.dbPort; const dbPort = process.env.dbPort;
const isDev = process.env.isDev; const isDev = process.env.isDev;
const ownerId = process.env.ownerId; const ownerId = process.env.ownerId;
// filesystem // filesystem
@ -22,10 +20,6 @@ const Discord = require('discord.js');
// Fuzzy text matching for db lookups // Fuzzy text matching for db lookups
const FuzzySearch = require('fuzzy-search'); const FuzzySearch = require('fuzzy-search');
// OpenAI
// const OpenAI = require("openai");
// const openai = new OpenAI();
// Axios for APIs // Axios for APIs
const axios = require('axios'); const axios = require('axios');
@ -34,7 +28,7 @@ const config = require('./config.json');
const strings = require('./strings.json'); const strings = require('./strings.json');
const slashCommandFiles = fs.readdirSync('./slash-commands/').filter(file => file.endsWith('.js')); const slashCommandFiles = fs.readdirSync('./slash-commands/').filter(file => file.endsWith('.js'));
const dotCommandFiles = fs.readdirSync('./dot-commands/').filter(file => file.endsWith('.js')); const dotCommandFiles = fs.readdirSync('./dot-commands/').filter(file => file.endsWith('.js'));
const { CommandData } = require('./CustomModules/NodBot.js'); const customEmbeds = require('./CustomModules/Embeds.js');
// MySQL database connection // MySQL database connection
const mysql = require('mysql'); const mysql = require('mysql');
@ -51,6 +45,11 @@ const db = new mysql.createPool({
const functions = { const functions = {
// Functions for managing and creating Collections // Functions for managing and creating Collections
collections: { collections: {
interactionStorage(client) {
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 // 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();
@ -268,14 +267,15 @@ const functions = {
return { embeds: [pastasEmbed], ephemeral: true }; return { embeds: [pastasEmbed], ephemeral: true };
}, },
gifs(commandData, gifList) { gifs(commandData, gifsString, state) {
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: commandData.author})
.setDescription(gifList.join('\n')); .setDescription(gifsString);
return { embeds: [gifsEmbed] }; const gifsPageAR = customEmbeds.gifsPageAR(state);
return { embeds: [gifsEmbed], components: [gifsPageAR], ephemeral: true };
}, },
text(commandData) { text(commandData) {
return { embeds: [new Discord.MessageEmbed() return { embeds: [new Discord.MessageEmbed()

12
main.js
View File

@ -28,9 +28,12 @@ const fn = require('./functions.js');
const config = require('./config.json'); const config = require('./config.json');
const strings = require('./strings.json'); const strings = require('./strings.json');
const { GifData, CommandData } = require('./CustomModules/NodBot.js'); const { GifData, CommandData } = require('./CustomModules/NodBot.js');
const ButtonHandlers = require('./CustomModules/ButtonHandlers.js');
const InteractionStorage = require('./CustomModules/InteractionStorage.js');
const isDev = process.env.isDev; const isDev = process.env.isDev;
client.once('ready', async () => { client.once('ready', async () => {
fn.collections.interactionStorage(client);
fn.collections.slashCommands(client); fn.collections.slashCommands(client);
fn.collections.dotCommands(client); fn.collections.dotCommands(client);
fn.collections.setvalidCommands(client); fn.collections.setvalidCommands(client);
@ -58,6 +61,12 @@ client.on('interactionCreate', async interaction => {
} }
const { commandName } = interaction; const { commandName } = interaction;
const idString = `${interaction.channelId}${interaction.member.id}`;
if (!client.interactionStorage.has(idString)) {
new InteractionStorage(idString, interaction);
}
if (client.slashCommands.has(commandName)) { if (client.slashCommands.has(commandName)) {
client.slashCommands.get(commandName).execute(interaction); client.slashCommands.get(commandName).execute(interaction);
} else { } else {
@ -67,7 +76,7 @@ client.on('interactionCreate', async interaction => {
} }
if (interaction.isButton()) { if (interaction.isButton()) {
if (interaction.user.id != strings.temp.gifUserId) return; if (isDev) console.log(interaction.id);
// 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;
@ -176,6 +185,7 @@ client.on('interactionCreate', async interaction => {
interaction.update({ content: 'Canceled.', components: [row] }); interaction.update({ content: 'Canceled.', components: [row] });
break; break;
default: default:
ButtonHandlers.baseEvent(interaction);
break; break;
} }
} }

View File

@ -1,36 +1,29 @@
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('gifs') .setName('gifs')
.setDescription('Get a list of currently saved GIFs.'), .setDescription('Get a list of currently saved GIFs.'),
async execute(interaction) { execute(interaction) {
if (!interaction.client.gifs) { return new Promise((resolve, reject) => {
interaction.reply('For some reason I don\'t have access to the collection of gifs. Sorry about that!'); if (!interaction.client.gifs) {
return; interaction.reply('For some reason I don\'t have access to the collection of gifs. Sorry about that!');
} resolve();
// const gifsMap = interaction.client.gifs.map(e => {e.name, e.url}); }
// const commandData = { let gifList = indexer(interaction.client.gifs, 0);
// gifs: [], let gifsString = new String();
// command: 'gifs',
// author: interaction.user.tag, for (const gif of gifList.thisPage) {
// }; gifsString += `[${gif.name}.gif](${gif.url})\n`;
// for (const row of gifsMap) { }
// commandData.gifs.push({ const commandData = {
// id: row.id, command: "/gifs",
// name: row.name, author: interaction.member.displayName
// }); };
// } interaction.reply(fn.embeds.gifs(commandData, gifsString, gifList.state));
let gifList = [];
interaction.client.gifs.forEach(element => {
gifList.push(`[${element.name}](${element.url})`);
}); });
const commandData = {
command: "/gifs",
author: interaction.member.displayName
};
interaction.reply(fn.embeds.gifs(commandData, gifList));
}, },
}; };