From 3d0fe87717b540c1d07a0c06c7dbb88b93e9ef2f Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 3 Jul 2022 17:38:34 +0200 Subject: [PATCH] connect bot to discord using discord.js --- src/index.js | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/src/index.js b/src/index.js index a605bed..d59bd40 100644 --- a/src/index.js +++ b/src/index.js @@ -1,31 +1,24 @@ -const fs = require('node:fs'); -const path = require('node:path'); -const { Client, Collection, Intents } = require('discord.js'); -const { token } = require('../config/config.json'); +const { Client, Intents } = require('discord.js'); -const client = new Client({ intents: [Intents.FLAGS.GUILDS] }); +const run = async () => { + console.log('Starting Botanique...'); -/* EVENT */ -const eventsPath = path.join(__dirname, 'events'); -const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); - -for (const file of eventFiles) { - const event = require(path.join(eventsPath, file)); - if (event.once) { - client.once(event.name, (...args) => event.execute(...args)); - } else { - client.on(event.name, async (...args) => event.execute(...args)); + // Load .env if not in prod + if (process.env.NODE_ENV !== 'production') { + const dotenv = await import('dotenv'); + dotenv.config({ path: './config/.env' }); } -} -/* COMMANDS */ -client.commands = new Collection(); -const commandsPath = path.join(__dirname, 'commands'); -const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); + // Client Discord.JS + const client = new Client({ intents: [Intents.FLAGS.GUILDS] }); + client.config = { token_discord: process.env.TOKEN_DISCORD }; + if (client) { + console.log('Client ✅'); + } else { + throw 'Client ❌'; + } -for (const file of commandFiles) { - const command = require(path.join(commandsPath, file)); - client.commands.set(command.data.name, command); -} + await client.login(client.config.token_discord); +}; -client.login(token); +run().catch(error => console.error(error));