From cc3fd9569c8602b6f9c68b6e9532c2e052097f80 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Thu, 13 Jul 2023 10:40:53 +0200 Subject: [PATCH] feat: Added .env file, and moved some properties from config.json to .env file --- .env.example | 5 +++++ .gitignore | 1 + config.json.example | 11 +++++------ deploy-commands.js | 15 ++++++++++----- deploy-system-commands.js | 7 ++++--- index.js | 11 +++++++++-- package-lock.json | 12 ++++++++++++ package.json | 1 + services/logger.js | 6 +++--- 9 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..1ba5f03 --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +NODE_ENV=development +MINIMUM_LOG_LEVEL=debug +MINIMUM_LOG_LEVEL_CONSOLE=info +DISCORD_BOT_TOKEN=YOUR_DISCORD_BOT_TOKEN +DISCORD_CLIENT_ID=YOUR_DISCOR_BOT_CLIENT_ID diff --git a/.gitignore b/.gitignore index 358b1b1..ca6ed79 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /node_modules /config.json +/.env /app-*.log diff --git a/config.json.example b/config.json.example index 7051373..b623a7a 100644 --- a/config.json.example +++ b/config.json.example @@ -1,7 +1,8 @@ { - "clientId": "Your bot Application ID here.", - "token": "Your bot auth token here.", - "systemServerGuildIds": ["Server id where system commands can be used", "Another server id where system commands can be used"], + "systemServerGuildIds": [ + "Server id where system commands can be used", + "Another server id where system commands can be used" + ], "embedColors": { "colorSuccess": "#23A55A", "colorWarning": "#F0B232", @@ -28,9 +29,7 @@ "leftChar": "▬", "rightChar": "▬" }, - "minimumLogLevel": "debug", - "minimumLogLevelConsole": "info", - "filterThreadAmount": "4", + "filterThreadAmount": "2", "filterList": [ { "label": "Bass boost", diff --git a/deploy-commands.js b/deploy-commands.js index 1ee277e..f936d26 100644 --- a/deploy-commands.js +++ b/deploy-commands.js @@ -1,7 +1,7 @@ const fs = require('node:fs'); const logger = require('./services/logger.js'); const { REST, Routes } = require('discord.js'); -const { token, clientId } = require('./config.json'); +require('dotenv').config(); const commands = []; const commandFiles = fs @@ -13,7 +13,9 @@ for (const file of commandFiles) { commands.push(command.data.toJSON()); } -const rest = new REST({ version: '10' }).setToken(token); +const rest = new REST({ version: '10' }).setToken( + process.env.DISCORD_BOT_TOKEN +); (async () => { try { @@ -27,9 +29,12 @@ const rest = new REST({ version: '10' }).setToken(token); logger.info('Started refreshing application (/) bot commands.'); - await rest.put(Routes.applicationCommands(clientId), { - body: commands - }); + await rest.put( + Routes.applicationCommands(process.env.DISCORD_CLIENT_ID), + { + body: commands + } + ); logger.info('Successfully refreshed application (/) bot commands.'); } catch (error) { diff --git a/deploy-system-commands.js b/deploy-system-commands.js index e6940af..b4de7aa 100644 --- a/deploy-system-commands.js +++ b/deploy-system-commands.js @@ -1,7 +1,8 @@ const fs = require('node:fs'); const logger = require('./services/logger.js'); const { REST, Routes } = require('discord.js'); -const { token, clientId, systemServerGuildIds } = require('./config.json'); +const { systemServerGuildIds } = require('./config.json'); +require('dotenv').config(); const systemCommands = []; const systemCommandFiles = fs @@ -13,7 +14,7 @@ for (const file of systemCommandFiles) { systemCommands.push(systemCommand.data.toJSON()); } -const rest = new REST({ version: '10' }).setToken(token); +const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_BOT_TOKEN); (async () => { try { @@ -29,7 +30,7 @@ const rest = new REST({ version: '10' }).setToken(token); for (const systemServerGuildId of systemServerGuildIds) { await rest.put( - Routes.applicationGuildCommands(clientId, systemServerGuildId), + Routes.applicationGuildCommands(process.env.DISCORD_CLIENT_ID, systemServerGuildId), { body: systemCommands } diff --git a/index.js b/index.js index 11ecc78..d8115c2 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,8 @@ const logger = require('./services/logger.js'); const { EmbedBuilder } = require('discord.js'); const { Player, onBeforeCreateStream } = require('discord-player'); const { stream } = require('yt-stream'); -const { token, embedColors } = require('./config.json'); +const { embedColors } = require('./config.json'); +require('dotenv').config(); // Setup required permissions for the bot to work const client = new Discord.Client({ @@ -51,6 +52,12 @@ onBeforeCreateStream(async (track) => { return null; }); +if (process.env.NODE_ENV === 'development') { + player.events.on('debug', (message) => { + logger.debug(message); + }); +} + player.events.on('error', (queue, error) => { // Emitted when the player queue encounters error logger.error(error, 'Player queue error event'); @@ -180,4 +187,4 @@ client.on('interactionCreate', async (interaction) => { } }); -client.login(token); +client.login(process.env.DISCORD_BOT_TOKEN); diff --git a/package-lock.json b/package-lock.json index 33542cc..fd86bc9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "@discordjs/voice": "^0.16.0", "discord-player": "^6.6.1", "discord.js": "^14.11.0", + "dotenv": "^16.3.1", "pino": "^8.14.1", "pino-pretty": "^10.0.1", "yt-stream": "^1.4.8" @@ -660,6 +661,17 @@ "url": "https://github.com/fb55/domutils?sponsor=1" } }, + "node_modules/dotenv": { + "version": "16.3.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", + "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/motdotla/dotenv?sponsor=1" + } + }, "node_modules/emoji-regex": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", diff --git a/package.json b/package.json index b17edcf..5773df2 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "@discordjs/voice": "^0.16.0", "discord-player": "^6.6.1", "discord.js": "^14.11.0", + "dotenv": "^16.3.1", "pino": "^8.14.1", "pino-pretty": "^10.0.1", "yt-stream": "^1.4.8" diff --git a/services/logger.js b/services/logger.js index 7aba62b..71b3fe2 100644 --- a/services/logger.js +++ b/services/logger.js @@ -1,5 +1,5 @@ const pino = require('pino'); -const { minimumLogLevel, minimumLogLevelConsole } = require('../config.json'); +require('dotenv').config(); const transport = pino.transport({ targets: [ @@ -19,7 +19,7 @@ const transport = pino.transport({ options: { destination: `./app-error.log` } }, { - level: minimumLogLevelConsole, + level: process.env.MINIMUM_LOG_LEVEL_CONSOLE, target: 'pino-pretty' } ] @@ -27,7 +27,7 @@ const transport = pino.transport({ module.exports = pino( { - level: minimumLogLevel, + level: process.env.MINIMUM_LOG_LEVEL, timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`, base: undefined },