diff --git a/modules/Databases.js b/modules/Databases.js new file mode 100644 index 0000000..7d5c344 --- /dev/null +++ b/modules/Databases.js @@ -0,0 +1,79 @@ +// Custom module for working with mySQL databases dev'd by voidf1sh +// Import the mySQL module +const mysql = require('mysql'); +// Import environment variables for database connections +const dotenv = require('dotenv'); +dotenv.config(); + +module.exports = { + getData(queryParts) { + // Return a Promise so we can resolve with data later + return new Promise((resolve, reject) => { + // Set up the database connection + const db = mysql.createConnection({ + host: process.env.DBHOST, + user: process.env.DBUSER, + password: process.env.DBPASS, + database: process.env.DBNAME, + port: process.env.DBPORT + }); + // Open the connection + db.connect((err) => { + if (err) { + reject(`Error connecting to the database: ${err.message}`); + return; + } + }); + + db.query(queryParts.rawQuery, queryParts.values, (err, results) => { + if (err) { + reject("Error fetching the data: " + err.message + "\nOffending Query: " + query); + // Close the database connection + db.end(); + return; + } + // If an empty set is returned + if (results.length == 0) { + reject("No results were returned."); + // Close the database connection + db.end(); + return; + } + // Close the database connection + db.end(); + resolve(results); + }); + }); + }, + setData(queryParts) { + // Return a Promise so we can resolve with data later + return new Promise((resolve, reject) => { + // Set up the database connection + const db = mysql.createConnection({ + host: process.env.DBHOST, + user: process.env.DBUSER, + password: process.env.DBPASS, + database: process.env.DBNAME, + port: process.env.DBPORT + }); + // Open the connection + db.connect((err) => { + if (err) { + reject(`Error connecting to the database: ${err.message}`); + return; + } + }); + db.query(queryParts.rawQuery, queryParts.values, (err, results) => { + if (err) { + reject(`Error setting the data: ${err.message}\nOffending Query: ${query}`); + // Close the database connection + db.end(); + return; + } + // Close the database connection + db.end(); + resolve(`Query executed successfully: ${results.affectedRows} row changed.`); + }); + }); + } +} \ No newline at end of file diff --git a/modules/NodBot.js b/modules/NodBot.js new file mode 100644 index 0000000..62e37b7 --- /dev/null +++ b/modules/NodBot.js @@ -0,0 +1,355 @@ +// Custom classes for NodBot + +const Collection = require('@discordjs/collection'); + +module.exports = { + CommandData: class { + constructor() { + this.name = ""; + this.type = ""; // "dot" or "slash" + this.isValid = false; + this.guildId = ""; + } + }, + GlobalContentManager: class { + constructor(collections) { + const { pastas, joints, mds, gifs, strains } = collections; + this.pastas = (pastas instanceof Collection) ? pastas : new Collection(); + this.joints = (joints instanceof Collection) ? joints: new Collection(); + this.mds = (mds instanceof Collection) ? mds : new Collection(); + this.gifs = (gifs instanceof Collection) ? gifs : new Collection(); + this.strains = (strains instanceof Collection) ? strains : new Collection(); + this.guildId = "GLOBAL"; + + this.queryBuilders = { + insert: { + pasta(title, content) { + // Upload a copypasta to the database + const rawQuery = "INSERT INTO ?? (??, ??, ??) VALUES (?, ?, ?)"; + const values = ["pastas", "title", "content", "guild_id", title, content, this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + joint(content) { + // Upload a stoner catchphrase to the database + const rawQuery = "INSERT INTO ?? (??, ??) VALUES (?, ?)"; + const values = ["joints", "content", "guild_id", content, this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + md(content) { + // Upload medical advice to the database + const rawQuery = "INSERT INTO ?? (??, ??) VALUES (?, ?)"; + const values = ["mds", "content", "guild_id", content, this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + media(title, url) { + // Upload an embeddable media url to the database + const rawQuery = "INSERT INTO ?? (??, ??, ??) VALUES (?, ?, ?)"; + const values = ["media", "title", "url", "guild_id", title, url, this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + strain(name, type, effects, flavor, rating) { + // Upload an embeddable media url to the database + const rawQuery = "INSERT INTO ?? (??, ??, ??) VALUES (?, ?, ?)"; + const values = ["media", "title", "url", "guild_id", title, url, this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + } + }, + select: { + pasta(title) { + // Fetch a copypasta from the database + const rawQuery = "SELECT * FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["pastas", "title", title, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + joint(id) { + // Fetch a stoner catchphrase from the database + const rawQuery = "SELECT * FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["joints", "id", id, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + md(id) { + // Fetch medical advice from the database + const rawQuery = "SELECT * FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["mds", "id", id, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + media(title) { + // Fetch an embeddable media url from the database + const rawQuery = "SELECT * FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["media", "title", title, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + } + }, + update: { + pasta(title, content) { + // Update a copypasta in the database + const rawQuery = "UPDATE ?? SET ? WHERE ?? = ? AND ?? = ?"; + const values = ["pastas", {content: content}, "title", title, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + joint(id, content) { + // Update a stoner catchphrase in the database + const rawQuery = "UPDATE ?? SET ? WHERE ?? = ? AND ?? = ?"; + const values = ["joints", {content: content}, "id", id, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + md(id, content) { + // Update medical advice in the database + const rawQuery = "UPDATE ?? SET ? WHERE ?? = ? AND ?? = ?"; + const values = ["mds", {content: content}, "id", id, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + media(title, url) { + // Update an embeddable media url in the database + const rawQuery = "UPDATE ?? SET ? WHERE ?? = ? AND ?? = ?"; + const values = ["media", {url: url}, "title", title, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + } + }, + delete: { + pasta(title) { + // Delete a copypasta from the database + const rawQuery = "DELETE FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["pastas", "title", title, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + joint(id) { + // Delete a stoner catchphrase from the database + const rawQuery = "DELETE FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["joints", "id", id, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + md(id) { + // Delete medical advice from the database + const rawQuery = "DELETE FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["mds", "id", id, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + media(title) { + // Delete an embeddable media url from the database + const rawQuery = "DELETE FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["media", "title", title, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + } + } + } + } + + }, + GuildContentManager: class { + constructor(collections) { + const { pastas, joints, mds, gifs } = collections; + this.pastas = (pastas instanceof Collection) ? pastas : new Collection(); + this.joints = (joints instanceof Collection) ? joints: new Collection(); + this.mds = (mds instanceof Collection) ? mds : new Collection(); + this.gifs = (gifs instanceof Collection) ? gifs : new Collection(); + this.guildId = ""; + + this.queryBuilders = { + insert: { + pasta(title, content) { + // Upload a copypasta to the database + const rawQuery = "INSERT INTO ?? (??, ??, ??) VALUES (?, ?, ?)"; + const values = ["pastas", "title", "content", "guild_id", title, content, this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + joint(content) { + // Upload a stoner catchphrase to the database + const rawQuery = "INSERT INTO ?? (??, ??) VALUES (?, ?)"; + const values = ["joints", "content", "guild_id", content, this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + md(content) { + // Upload medical advice to the database + const rawQuery = "INSERT INTO ?? (??, ??) VALUES (?, ?)"; + const values = ["mds", "content", "guild_id", content, this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + media(title, url) { + // Upload an embeddable media url to the database + const rawQuery = "INSERT INTO ?? (??, ??, ??) VALUES (?, ?, ?)"; + const values = ["media", "title", "url", "guild_id", title, url, this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + } + }, + select: { + pasta(title) { + // Fetch a copypasta from the database + const rawQuery = "SELECT * FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["pastas", "title", title, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + joint(id) { + // Fetch a stoner catchphrase from the database + const rawQuery = "SELECT * FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["joints", "id", id, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + md(id) { + // Fetch medical advice from the database + const rawQuery = "SELECT * FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["mds", "id", id, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + media(title) { + // Fetch an embeddable media url from the database + const rawQuery = "SELECT * FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["media", "title", title, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + } + }, + update: { + pasta(title, content) { + // Update a copypasta in the database + const rawQuery = "UPDATE ?? SET ? WHERE ?? = ? AND ?? = ?"; + const values = ["pastas", {content: content}, "title", title, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + joint(id, content) { + // Update a stoner catchphrase in the database + const rawQuery = "UPDATE ?? SET ? WHERE ?? = ? AND ?? = ?"; + const values = ["joints", {content: content}, "id", id, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + md(id, content) { + // Update medical advice in the database + const rawQuery = "UPDATE ?? SET ? WHERE ?? = ? AND ?? = ?"; + const values = ["mds", {content: content}, "id", id, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + media(title, url) { + // Update an embeddable media url in the database + const rawQuery = "UPDATE ?? SET ? WHERE ?? = ? AND ?? = ?"; + const values = ["media", {url: url}, "title", title, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + } + }, + delete: { + pasta(title) { + // Delete a copypasta from the database + const rawQuery = "DELETE FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["pastas", "title", title, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + joint(id) { + // Delete a stoner catchphrase from the database + const rawQuery = "DELETE FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["joints", "id", id, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + md(id) { + // Delete medical advice from the database + const rawQuery = "DELETE FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["mds", "id", id, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + }, + media(title) { + // Delete an embeddable media url from the database + const rawQuery = "DELETE FROM ?? WHERE ?? = ? AND ?? = ?"; + const values = ["media", "title", title, "guild_id", this.guildId]; + return { + rawQuery: rawQuery, + values: values + } + } + } + } + } + } +} \ No newline at end of file