Moving things from config.json to environment variables=

This commit is contained in:
Skylar Grant 2022-05-28 17:08:41 -04:00
parent 65375d008b
commit 138e458e82
6 changed files with 32 additions and 26 deletions

View File

@ -4,7 +4,8 @@ dotenv.config();
const { REST } = require('@discordjs/rest'); const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9'); const { Routes } = require('discord-api-types/v9');
const { guildId, clientId } = require('./config.json'); const clientId = process.env.clientId;
const { guildId } = require('./config.json');
const token = process.env.TOKEN; const token = process.env.TOKEN;
const rest = new REST({ version: '9' }).setToken(token); const rest = new REST({ version: '9' }).setToken(token);

View File

@ -4,7 +4,8 @@ dotenv.config();
const { REST } = require('@discordjs/rest'); const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9'); const { Routes } = require('discord-api-types/v9');
const { guildId, clientId } = require('./config.json'); const { guildId } = require('./config.json');
const clientId = process.env.clientId;
const token = process.env.TOKEN; const token = process.env.TOKEN;
const fs = require('fs'); const fs = require('fs');

View File

@ -4,7 +4,8 @@ dotenv.config();
const { REST } = require('@discordjs/rest'); const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9'); const { Routes } = require('discord-api-types/v9');
const { guildId, clientId } = require('./config.json'); const { guildId } = require('./config.json');
const clientId = process.env.clientId;
const token = process.env.TOKEN; const token = process.env.TOKEN;
const fs = require('fs'); const fs = require('fs');

View File

@ -1,8 +1,4 @@
{ {
"isDev": false,
"clientId": "732326394054574120",
"guildId": "760701839427108874", "guildId": "760701839427108874",
"devChannelId": "890752931643654144",
"guildName": "CumbHub",
"validCommands": [] "validCommands": []
} }

View File

@ -2,29 +2,30 @@
// dotenv for handling environment variables // dotenv for handling environment variables
const dotenv = require('dotenv'); const dotenv = require('dotenv');
dotenv.config(); dotenv.config();
// Assignment of environment variables // 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;
const dbName = process.env.dbName; 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;
// filesystem // filesystem
const fs = require('fs'); const fs = require('fs');
// D.js // Discord.js
const Discord = require('discord.js'); 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');
// Various imports // Various imports from other files
const config = require('./config.json'); 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'));
// MySQL // MySQL database connection
const mysql = require('mysql'); const mysql = require('mysql');
const db = new mysql.createPool({ const db = new mysql.createPool({
connectionLimit: 10, connectionLimit: 10,
@ -36,7 +37,9 @@ const db = new mysql.createPool({
}); });
const functions = { const functions = {
// Functions for managing and creating Collections
collections: { collections: {
// 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();
client.slashCommands.clear(); client.slashCommands.clear();
@ -46,13 +49,13 @@ const functions = {
client.slashCommands.set(slashCommand.data.name, slashCommand); client.slashCommands.set(slashCommand.data.name, slashCommand);
} }
} }
if (config.isDev) console.log('Slash Commands Collection Built'); if (isDev) console.log('Slash Commands Collection Built');
}, },
setvalidCommands(client) { setvalidCommands(client) {
for (const entry of client.dotCommands.map(command => command.name)) { for (const entry of client.dotCommands.map(command => command.name)) {
config.validCommands.push(entry); config.validCommands.push(entry);
} }
if (config.isDev) console.log('Valid Commands Added to Config'); if (isDev) console.log('Valid Commands Added to Config');
}, },
dotCommands(client) { dotCommands(client) {
if (!client.dotCommands) client.dotCommands = new Discord.Collection(); if (!client.dotCommands) client.dotCommands = new Discord.Collection();
@ -61,7 +64,7 @@ const functions = {
const dotCommand = require(`./dot-commands/${file}`); const dotCommand = require(`./dot-commands/${file}`);
client.dotCommands.set(dotCommand.name, dotCommand); client.dotCommands.set(dotCommand.name, dotCommand);
} }
if (config.isDev) console.log('Dot Commands Collection Built'); if (isDev) console.log('Dot Commands Collection Built');
}, },
gifs(rows, client) { gifs(rows, client) {
if (!client.gifs) client.gifs = new Discord.Collection(); if (!client.gifs) client.gifs = new Discord.Collection();
@ -74,7 +77,7 @@ const functions = {
}; };
client.gifs.set(gif.name, gif); client.gifs.set(gif.name, gif);
} }
if (config.isDev) console.log('GIFs Collection Built'); if (isDev) console.log('GIFs Collection Built');
}, },
joints(rows, client) { joints(rows, client) {
if (!client.joints) client.joints = new Discord.Collection(); if (!client.joints) client.joints = new Discord.Collection();
@ -86,7 +89,7 @@ const functions = {
}; };
client.joints.set(joint.id, joint); client.joints.set(joint.id, joint);
} }
if (config.isDev) console.log('Joints Collection Built'); if (isDev) console.log('Joints Collection Built');
}, },
pastas(rows, client) { pastas(rows, client) {
if (!client.pastas) client.pastas = new Discord.Collection(); if (!client.pastas) client.pastas = new Discord.Collection();
@ -100,7 +103,7 @@ const functions = {
}; };
client.pastas.set(pasta.name, pasta); client.pastas.set(pasta.name, pasta);
} }
if (config.isDev) console.log('Pastas Collection Built'); if (isDev) console.log('Pastas Collection Built');
}, },
requests(rows, client) { requests(rows, client) {
if (!client.requests) client.requests = new Discord.Collection(); if (!client.requests) client.requests = new Discord.Collection();
@ -113,7 +116,7 @@ const functions = {
}; };
client.requests.set(request.id, request); client.requests.set(request.id, request);
} }
if (config.isDev) console.log('Requests Collection Built'); if (isDev) console.log('Requests Collection Built');
}, },
strains(rows, client) { strains(rows, client) {
if (!client.strains) client.strains = new Discord.Collection(); if (!client.strains) client.strains = new Discord.Collection();
@ -125,7 +128,7 @@ const functions = {
}; };
client.strains.set(strain.name, strain); client.strains.set(strain.name, strain);
} }
if (config.isDev) console.log('Strains Collection Built'); if (isDev) console.log('Strains Collection Built');
} }
}, },
dot: { dot: {
@ -133,15 +136,15 @@ 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 (finalPeriod < 0) { // If the final period is the last character, or doesn't exist
if (finalPeriod <= 0) {
commandData.isCommand = false; commandData.isCommand = false;
commandData.args = message.content.slice(0,finalPeriod).toLowerCase();
commandData.command = message.content.slice(finalPeriod).replace('.','').toLowerCase();
commandData.author = `${message.author.username}#${message.author.discriminator}`;
return commandData; return commandData;
} }
commandData.isCommand = true; commandData.isCommand = true;
// Get the first part of the message, everything leading up to the final period
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
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}#${message.author.discriminator}`;
return this.checkCommand(commandData); return this.checkCommand(commandData);
@ -150,6 +153,8 @@ const functions = {
if (commandData.isCommand) { if (commandData.isCommand) {
const validCommands = require('./config.json').validCommands; const validCommands = require('./config.json').validCommands;
commandData.isValid = validCommands.includes(commandData.command); commandData.isValid = validCommands.includes(commandData.command);
// Add exceptions for messages that contain only a link
if (commandData.args.startsWith('http')) commandData.isValid = false;
} }
else { else {
commandData.isValid = false; commandData.isValid = false;

View File

@ -4,6 +4,7 @@
const dotenv = require('dotenv'); const dotenv = require('dotenv');
dotenv.config(); dotenv.config();
const token = process.env.TOKEN; const token = process.env.TOKEN;
const statusChannelId = process.env.statusChannelId;
// Discord.JS // Discord.JS
const { Client, Intents } = require('discord.js'); const { Client, Intents } = require('discord.js');
@ -26,7 +27,7 @@ const { MessageActionRow, MessageButton } = require('discord.js');
const fn = require('./functions.js'); 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');
config.isDev = process.env.isDev; const isDev = process.env.isDev;
client.once('ready', () => { client.once('ready', () => {
fn.collections.slashCommands(client); fn.collections.slashCommands(client);
@ -38,7 +39,7 @@ client.once('ready', () => {
fn.download.requests(client); fn.download.requests(client);
fn.download.strains(client); fn.download.strains(client);
console.log('Ready!'); console.log('Ready!');
client.channels.fetch(config.devChannelId).then(channel => { client.channels.fetch(statusChannelId).then(channel => {
channel.send(`I'm ready! ${new Date().toISOString()}`); channel.send(`I'm ready! ${new Date().toISOString()}`);
}); });
}); });
@ -46,7 +47,7 @@ client.once('ready', () => {
// slash-commands // slash-commands
client.on('interactionCreate', async interaction => { client.on('interactionCreate', async interaction => {
if (interaction.isCommand()) { if (interaction.isCommand()) {
if (config.isDev) { if (isDev) {
console.log(interaction); console.log(interaction);
} }
const { commandName } = interaction; const { commandName } = interaction;
@ -189,6 +190,7 @@ client.on('messageCreate', message => {
if (lowerContent.includes('big') && lowerContent.includes('doinks')) message.reply('gang.'); if (lowerContent.includes('big') && lowerContent.includes('doinks')) message.reply('gang.');
if (lowerContent.includes('ligma')) message.reply('ligma balls, goteem'); if (lowerContent.includes('ligma')) message.reply('ligma balls, goteem');
// Break the message down into its components and analyze it
const commandData = fn.dot.getCommandData(message); const commandData = fn.dot.getCommandData(message);
console.log(commandData); console.log(commandData);