/edit pasta, setFooter, PastaData Classification

This commit is contained in:
Skylar Grant 2023-08-08 14:34:46 -04:00
parent cb834cb7ee
commit f3051ec944
4 changed files with 71 additions and 32 deletions

View File

@ -1,15 +1,52 @@
module.exports = { module.exports = {
GifData: class { GifData: class {
constructor() { constructor() {
this.gifName = ""; this.id = 0;
this.gifUrl = ""; this.name = "";
this.url = "";
} }
setInfo(name, url) {
if ((!name) || (!url)) throw `Expected a name and url, receieved {name: ${name}, url: ${url}}`;
this.name = name;
this.url = url;
return this; // Initial GifData configuration
// Can also be used to update the data piecemeal
setInfo(name, url, id) {
// Check for existing or incoming name
if ((this.name === "") && (typeof name !== 'string')) throw `Error: This Gif doesn't have existing name, and no name is going to be set.`;
// Check for existing content or incoming content
if ((this.url === "") && (typeof url !== 'string')) throw `Error: This Gif doesn't have existing url, and no url is going to be set.`;
// Property is set if the variable is the right type,
// otherwise it keeps the existing property
this.id = typeof id === 'number' ? id : this.id;
this.name = typeof name === 'string' ? name : this.name;
this.url = typeof url === 'string' ? url : this.url;
return this; // For chaining
}
},
PastaData: class {
constructor() {
this.id = 0;
this.name = "";
this.content = "";
this.iconUrl = "";
}
// Initial PastaData configuration
// Can also be used to update the data piecemeal
setInfo(name, content, iconUrl, id) {
// Check for existing or incoming name
if ((this.name === "") && (typeof name !== 'string')) throw `Error: This Pasta doesn't have existing name, and no name is going to be set.`;
// Check for existing content or incoming content
if ((this.content === "") && (typeof content !== 'string')) throw `Error: This Pasta doesn't have existing content, and no content is going to be set.`;
// Property is set if the variable is the right type,
// otherwise it keeps the existing property
this.id = typeof id === 'number' ? id : this.id;
this.name = typeof name === 'string' ? name : this.name;
this.content = typeof content === 'string' ? content : this.content;
this.iconUrl = typeof iconUrl === 'string' ? iconUrl : this.iconUrl;
return this; // For chaining
} }
} }
} }

View File

@ -6,14 +6,12 @@ module.exports = {
usage: '<Copypasta Name>.pasta', usage: '<Copypasta Name>.pasta',
execute(message, commandData) { execute(message, commandData) {
const client = message.client; const client = message.client;
let replyBody = ''; let pastaData;
let iconUrl;
if (!client.pastas.has(commandData.args)) { if (!client.pastas.has(commandData.args)) {
commandData.content = 'Sorry I couldn\'t find that pasta.'; commandData.content = 'Sorry I couldn\'t find that pasta.';
} else { } else {
commandData.content = client.pastas.get(commandData.args).content; pastaData = client.pastas.get(commandData.args);
commandData.iconUrl = client.pastas.get(commandData.args).iconUrl;
} }
message.reply(fn.embeds.pasta(commandData)); message.reply(fn.embeds.pasta(commandData, pastaData));
} }
} }

View File

@ -45,7 +45,7 @@ const dotCommandFiles = fs.readdirSync('./dot-commands/').filter(file => file.en
// MySQL database connection // MySQL database connection
const mysql = require('mysql'); const mysql = require('mysql');
const { GifData } = require('./CustomModules/NodBot'); const { GifData, PastaData } = require('./CustomModules/NodBot');
const db = new mysql.createPool({ const db = new mysql.createPool({
connectionLimit: 10, connectionLimit: 10,
host: dbHost, host: dbHost,
@ -129,13 +129,8 @@ const functions = {
if (!client.pastas) client.pastas = new Discord.Collection(); if (!client.pastas) client.pastas = new Discord.Collection();
client.pastas.clear(); client.pastas.clear();
for (const row of rows) { for (const row of rows) {
const pasta = { const pastaData = new PastaData().setInfo(row.name, row.content, row.iconurl, row.id);
id: row.id, client.pastas.set(pastaData.name, pastaData);
name: row.name,
content: row.content,
iconUrl: row.iconurl,
};
client.pastas.set(pasta.name, pasta);
} }
if (isDev) console.log('Pastas Collection Built'); if (isDev) console.log('Pastas Collection Built');
}, },
@ -183,7 +178,7 @@ const functions = {
const commandData = {}; const commandData = {};
// Split the message content at the final instance of a period // Split the message content at the final instance of a period
const finalPeriod = message.content.lastIndexOf('.'); const finalPeriod = message.content.lastIndexOf('.');
if(isDev) console.log(message.content); // if(isDev) console.log(message.content);
// If the final period is the last character, or doesn't exist // If the final period is the last character, or doesn't exist
if (finalPeriod < 0) { if (finalPeriod < 0) {
if (isDev) console.log(finalPeriod); if (isDev) console.log(finalPeriod);
@ -195,7 +190,7 @@ const functions = {
commandData.args = message.content.slice(0,finalPeriod).toLowerCase(); commandData.args = message.content.slice(0,finalPeriod).toLowerCase();
// Get the last part of the message, everything after the final period // Get the last part of the message, everything after the final period
commandData.command = message.content.slice(finalPeriod).replace('.','').toLowerCase(); commandData.command = message.content.slice(finalPeriod).replace('.','').toLowerCase();
commandData.author = `${message.author.username}#${message.author.discriminator}`; commandData.author = `${message.author.username}`;
return this.checkCommand(commandData); return this.checkCommand(commandData);
}, },
checkCommand(commandData) { checkCommand(commandData) {
@ -273,22 +268,22 @@ const functions = {
.setAuthor({name: `${commandData.args}.${commandData.command}`}) .setAuthor({name: `${commandData.args}.${commandData.command}`})
.setImage(commandData.embed_url) .setImage(commandData.embed_url)
.setTimestamp() .setTimestamp()
.setFooter(commandData.author)]}; .setFooter({text: commandData.author})]};
}, },
pasta(commandData) { pasta(commandData, pastaData) {
return { embeds: [ new Discord.MessageEmbed() return { embeds: [ new Discord.MessageEmbed()
.setAuthor({name: `${commandData.args}.${commandData.command}`}) .setAuthor({name: `${commandData.args}.${commandData.command}`})
.setDescription(commandData.content) .setDescription(pastaData.content)
.setThumbnail(commandData.iconUrl) .setThumbnail(pastaData.iconUrl)
.setTimestamp() .setTimestamp()
.setFooter(commandData.author)]}; .setFooter({text: commandData.author})]};
}, },
pastas(commandData) { pastas(commandData) {
const pastasArray = []; const pastasArray = [];
const pastasEmbed = new Discord.MessageEmbed() const pastasEmbed = new Discord.MessageEmbed()
.setAuthor({name: commandData.command}) .setAuthor({name: commandData.command})
.setTimestamp() .setTimestamp()
.setFooter(commandData.author); .setFooter({text: commandData.author});
for (const row of commandData.pastas) { for (const row of commandData.pastas) {
pastasArray.push(`#${row.id} - ${row.name}.pasta`); pastasArray.push(`#${row.id} - ${row.name}.pasta`);
@ -303,7 +298,7 @@ const functions = {
const gifsEmbed = new Discord.MessageEmbed() const gifsEmbed = new Discord.MessageEmbed()
.setAuthor({name: commandData.command}) .setAuthor({name: commandData.command})
.setTimestamp() .setTimestamp()
.setFooter(commandData.author) .setFooter({text: commandData.author})
.setDescription(gifList.join('\n')); .setDescription(gifList.join('\n'));
return { embeds: [gifsEmbed] }; return { embeds: [gifsEmbed] };
@ -313,13 +308,13 @@ const functions = {
.setAuthor({name: commandData.command}) .setAuthor({name: commandData.command})
.setDescription(commandData.content) .setDescription(commandData.content)
.setTimestamp() .setTimestamp()
.setFooter(commandData.author)]}; .setFooter({text: commandData.author})]};
}, },
requests(commandData) { requests(commandData) {
const requestsEmbed = new Discord.MessageEmbed() const requestsEmbed = new Discord.MessageEmbed()
.setAuthor({name: commandData.command}) .setAuthor({name: commandData.command})
.setTimestamp() .setTimestamp()
.setFooter(commandData.author); .setFooter({text: commandData.author});
const requestsArray = []; const requestsArray = [];

View File

@ -8,7 +8,7 @@ const tenor = require('tenorjs').client({
const { SlashCommandBuilder } = require('@discordjs/builders'); const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageActionRow, MessageButton } = require('discord.js'); const { MessageActionRow, MessageButton } = require('discord.js');
const { GifData } = require('../CustomModules/NodBot'); const { GifData, PastaData } = require('../CustomModules/NodBot');
const fn = require('../functions.js'); const fn = require('../functions.js');
const strings = require('../strings.json'); const strings = require('../strings.json');
const { emoji } = strings; const { emoji } = strings;
@ -17,6 +17,7 @@ module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName('edit') .setName('edit')
.setDescription('Edit content in Nodbot\'s database.') .setDescription('Edit content in Nodbot\'s database.')
// GIF
.addSubcommand(subcommand => .addSubcommand(subcommand =>
subcommand subcommand
.setName('gif') .setName('gif')
@ -35,6 +36,7 @@ module.exports = {
.setRequired(true) .setRequired(true)
) )
) )
// Pasta
.addSubcommand(subcommand => .addSubcommand(subcommand =>
subcommand subcommand
.setName('pasta') .setName('pasta')
@ -75,6 +77,7 @@ module.exports = {
// Pasta // Pasta
case "pasta": case "pasta":
//TODO //TODO
await this.editPasta(interaction, interaction.options.getString('name'), interaction.options.getString('content'));
break; break;
// Strain // Strain
case "strain": case "strain":
@ -97,5 +100,11 @@ module.exports = {
await fn.upload.gif(gifData, interaction.client); await fn.upload.gif(gifData, interaction.client);
await fn.download.gifs(interaction.client); await fn.download.gifs(interaction.client);
await interaction.editReply(`I've updated ${gifData.name}.gif`); await interaction.editReply(`I've updated ${gifData.name}.gif`);
},
async editPasta(interaction, name, content) {
const pastaData = new PastaData().setInfo(name, content);
await fn.upload.pasta(pastaData, interaction.client);
await fn.download.gifs(interaction.client);
await interaction.editReply(`I've updated ${name}.pasta`);
} }
}; };