#Can someone help me to centralize my DB connection?

2 messages · Page 1 of 1 (latest)

rich tendon
#
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const dotenv = require('dotenv');
const { Sequelize } = require('sequelize');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

dotenv.config();

const sequelize = new Sequelize({
    dialect: 'sqlite',
    storage: ''
  });



client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const filePath = path.join(commandsPath, file);
    const command = require(filePath);
    if ('data' in command && 'execute' in command) {
        client.commands.set(command.data.name, command);
    } else {
        console.log(`[WARNING] Der Befehl in ${filePath} fehlt eine erforderliche "data" oder "execute" Eigenschaft.`);
    }
}

const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
    const filePath = path.join(eventsPath, file);
    const event = require(filePath);
    if (event.once) {
        client.once(event.name, (...args) => event.execute(...args));
    } else {
        client.on(event.name, (...args) => event.execute(...args));
    }
}


(async () => {
    try {
        await sequelize.authenticate();
        console.log('The connection to the database has been successfully established.');
    } catch (error) {
        console.error('The connection to the database has failed:', error);
    } finally {
        sequelize.close();
    }
})();


client.login(process.env.token);

shut phoenix