Code zentralisieren?

Kann mir jemand helfen meinen Code zu zentralisieren? Ich möchte die DB connection aus dem code raus gezogen wird danke.

const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');
const { Sequelize } = require('sequelize');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: ''
});
client.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
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] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
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(token);
JavaScript, Code, Programmiersprache, Environment, node.js, Discord, Discord Bot

Meistgelesene Fragen zum Thema Discord