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 { 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 rest = new REST({ version: '9' }).setToken(token);

View File

@ -4,7 +4,8 @@ dotenv.config();
const { REST } = require('@discordjs/rest');
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 fs = require('fs');

View File

@ -4,7 +4,8 @@ dotenv.config();
const { REST } = require('@discordjs/rest');
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 fs = require('fs');

View File

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

View File

@ -2,29 +2,30 @@
// dotenv for handling environment variables
const dotenv = require('dotenv');
dotenv.config();
// Assignment of environment variables
// Assignment of environment variables for database access
const dbHost = process.env.dbHost;
const dbUser = process.env.dbUser;
const dbName = process.env.dbName;
const dbPass = process.env.dbPass;
const dbPort = process.env.dbPort;
const isDev = process.env.isDev;
// filesystem
const fs = require('fs');
// D.js
// Discord.js
const Discord = require('discord.js');
// Fuzzy text matching for db lookups
const FuzzySearch = require('fuzzy-search');
// Various imports
// Various imports from other files
const config = require('./config.json');
const strings = require('./strings.json');
const slashCommandFiles = fs.readdirSync('./slash-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 db = new mysql.createPool({
connectionLimit: 10,
@ -36,7 +37,9 @@ const db = new mysql.createPool({
});
const functions = {
// Functions for managing and creating Collections
collections: {
// Create the collection of slash commands
slashCommands(client) {
if (!client.slashCommands) client.slashCommands = new Discord.Collection();
client.slashCommands.clear();
@ -46,13 +49,13 @@ const functions = {
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) {
for (const entry of client.dotCommands.map(command => command.name)) {
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) {
if (!client.dotCommands) client.dotCommands = new Discord.Collection();
@ -61,7 +64,7 @@ const functions = {
const dotCommand = require(`./dot-commands/${file}`);
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) {
if (!client.gifs) client.gifs = new Discord.Collection();
@ -74,7 +77,7 @@ const functions = {
};
client.gifs.set(gif.name, gif);
}
if (config.isDev) console.log('GIFs Collection Built');
if (isDev) console.log('GIFs Collection Built');
},
joints(rows, client) {
if (!client.joints) client.joints = new Discord.Collection();
@ -86,7 +89,7 @@ const functions = {
};
client.joints.set(joint.id, joint);
}
if (config.isDev) console.log('Joints Collection Built');
if (isDev) console.log('Joints Collection Built');
},
pastas(rows, client) {
if (!client.pastas) client.pastas = new Discord.Collection();
@ -100,7 +103,7 @@ const functions = {
};
client.pastas.set(pasta.name, pasta);
}
if (config.isDev) console.log('Pastas Collection Built');
if (isDev) console.log('Pastas Collection Built');
},
requests(rows, client) {
if (!client.requests) client.requests = new Discord.Collection();
@ -113,7 +116,7 @@ const functions = {
};
client.requests.set(request.id, request);
}
if (config.isDev) console.log('Requests Collection Built');
if (isDev) console.log('Requests Collection Built');
},
strains(rows, client) {
if (!client.strains) client.strains = new Discord.Collection();
@ -125,7 +128,7 @@ const functions = {
};
client.strains.set(strain.name, strain);
}
if (config.isDev) console.log('Strains Collection Built');
if (isDev) console.log('Strains Collection Built');
}
},
dot: {
@ -133,15 +136,15 @@ const functions = {
const commandData = {};
// Split the message content at the final instance of a period
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.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;
}
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();
// Get the last part of the message, everything after the final period
commandData.command = message.content.slice(finalPeriod).replace('.','').toLowerCase();
commandData.author = `${message.author.username}#${message.author.discriminator}`;
return this.checkCommand(commandData);
@ -150,6 +153,8 @@ const functions = {
if (commandData.isCommand) {
const validCommands = require('./config.json').validCommands;
commandData.isValid = validCommands.includes(commandData.command);
// Add exceptions for messages that contain only a link
if (commandData.args.startsWith('http')) commandData.isValid = false;
}
else {
commandData.isValid = false;

View File

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