🎨 index.js is now splitted

This commit is contained in:
Axel Olausson Holtenäs 2022-03-13 12:53:09 +01:00
parent 4eb808e921
commit f4be4416f8
No known key found for this signature in database
GPG key ID: E3AE7E194AE017ED
7 changed files with 64 additions and 44 deletions

View file

@ -1,5 +1,7 @@
const logger = require('../handlers/logger');
const { database, deployCommands } = require('../helpers');
module.exports = {
name: 'ready',
once: true,
@ -14,5 +16,7 @@ module.exports = {
],
status: 'online',
});
await deployCommands();
},
};

13
src/handlers/commands.js Normal file
View file

@ -0,0 +1,13 @@
const fs = require('fs'); // fs
const { Collection } = require('discord.js'); // discord.js
module.exports = async (client) => {
client.commands = new Collection();
const commandFiles = fs.readdirSync('./src/commands');
for (const file of commandFiles) {
// eslint-disable-next-line import/no-dynamic-require, global-require
const command = require(`../commands/${file}`);
client.commands.set(command.data.name, command);
}
};

16
src/handlers/events.js Normal file
View file

@ -0,0 +1,16 @@
const fs = require('fs'); // fs
module.exports = async (client) => {
const eventFiles = fs
.readdirSync('./src/events')
.filter((file) => file.endsWith('.js'));
for (const file of eventFiles) {
const event = require(`../events/${file}`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
};

5
src/handlers/index.js Normal file
View file

@ -0,0 +1,5 @@
const events = require('./events');
const commands = require('./commands');
const locale = require('./locale');
module.exports = { events, commands, locale };

View file

@ -1,22 +1,16 @@
/* eslint-disable no-restricted-syntax */
const config = require('../config.json');
const logger = require('./handlers/logger');
const config = require('../../config.json');
const logger = require('../handlers/logger');
const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
module.exports = async () => {
// eslint-disable-next-line global-require
const fs = require('fs');
// eslint-disable-next-line global-require
const { REST } = require('@discordjs/rest');
// eslint-disable-next-line global-require
const { Routes } = require('discord-api-types/v9');
const commands = [];
const commandFiles = fs.readdirSync('./src/commands');
for (const file of commandFiles) {
// eslint-disable-next-line import/no-dynamic-require, global-require
const command = require(`./commands/${file}`);
const command = require(`../commands/${file}`);
commands.push(command.data.toJSON());
}

4
src/helpers/index.js Normal file
View file

@ -0,0 +1,4 @@
const database = require('./database');
const deployCommands = require('./deployCommands');
module.exports = { database, deployCommands };

View file

@ -1,37 +1,21 @@
const fs = require('fs');
const { Client, Collection, Intents } = require('discord.js');
// Dependencies
const { Client, Intents } = require('discord.js'); // discord.js
require('./deploy-commands')();
require('./helpers/database')();
require('./handlers/locale')();
const { database } = require('./helpers'); // helpers
const { events, commands, locale } = require('./handlers'); // handlers
const config = require('../config.json');
const config = require('../config.json'); // config.json
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
});
(async () => {
// Initialize discord.js client
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
});
const eventFiles = fs
.readdirSync('./src/events')
.filter((file) => file.endsWith('.js'));
await database();
await locale();
await events(client);
await commands(client);
client.commands = new Collection();
const commandFiles = fs.readdirSync('./src/commands');
for (const file of commandFiles) {
// eslint-disable-next-line import/no-dynamic-require, global-require
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
}
for (const file of eventFiles) {
// eslint-disable-next-line import/no-dynamic-require, global-require
const event = require(`./events/${file}`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
client.login(config.bot.token);
await client.login(config.bot.token);
})();