connect bot to discord using discord.js

This commit is contained in:
Mylloon 2022-07-03 17:38:34 +02:00
parent c8ea429968
commit 3d0fe87717
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -1,31 +1,24 @@
const fs = require('node:fs'); const { Client, Intents } = require('discord.js');
const path = require('node:path');
const { Client, Collection, Intents } = require('discord.js');
const { token } = require('../config/config.json');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] }); const run = async () => {
console.log('Starting Botanique...');
/* EVENT */ // Load .env if not in prod
const eventsPath = path.join(__dirname, 'events'); if (process.env.NODE_ENV !== 'production') {
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); const dotenv = await import('dotenv');
dotenv.config({ path: './config/.env' });
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));
} }
}
/* COMMANDS */ // Client Discord.JS
client.commands = new Collection(); const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const commandsPath = path.join(__dirname, 'commands'); client.config = { token_discord: process.env.TOKEN_DISCORD };
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); if (client) {
console.log('Client ✅');
} else {
throw 'Client ❌';
}
for (const file of commandFiles) { await client.login(client.config.token_discord);
const command = require(path.join(commandsPath, file)); };
client.commands.set(command.data.name, command);
}
client.login(token); run().catch(error => console.error(error));