Split into multiple files (#18)

Reviewed-on: https://git.kennel.ml/ConfrerieDuKassoulait/Botanique/pulls/18
This commit is contained in:
Anri 2022-07-03 18:43:31 +02:00
parent fd2df7aee9
commit 9f24b73ef8
10 changed files with 2129 additions and 137 deletions

View file

@ -5,11 +5,12 @@
"es6": true
},
"parserOptions": {
"ecmaVersion": 2021
"ecmaVersion": 2021,
"sourceType": "module"
},
"rules": {
"arrow-spacing": ["warn", { "before": true, "after": true }],
"brace-style": ["error", "stroustrup", { "allowSingleLine": true }],
"brace-style": ["error"],
"comma-dangle": ["error", "always-multiline"],
"comma-spacing": "error",
"comma-style": "error",

1
config/example.env Normal file
View file

@ -0,0 +1 @@
TOKEN_DISCORD="your-token-goes-here"

View file

@ -1 +0,0 @@
DISCORD_TOKEN=your-token-goes-here

View file

@ -1,16 +0,0 @@
const { Client, Intents } = require('discord.js');
const dotenv = require('dotenv');
// Load .env
dotenv.config();
// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
// When the client is ready
client.once('ready', () => {
console.log('Prêt !');
});
// Login to Discord with token
client.login(process.env.DISCORD_TOKEN);

2134
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -2,9 +2,11 @@
"name": "botanique",
"version": "0.0.1",
"description": "Bot discord",
"main": "index.js",
"main": "src/index.js",
"scripts": {
"main": "node ."
"main": "node .",
"debug": "nodemon .",
"eslint": "npx eslint src"
},
"repository": {
"type": "git",
@ -13,10 +15,12 @@
"author": "La confrérie du Kassoulait",
"license": "GPL-3.0-only",
"dependencies": {
"discord.js": "^13.6.0",
"dotenv": "^16.0.0"
"discord.js": "^13.8.1"
},
"devDependencies": {
"eslint": "^8.13.0"
}
"dotenv": "^16.0.1",
"eslint": "^8.13.0",
"nodemon": "^2.0.18"
},
"type": "module"
}

View file

@ -0,0 +1,5 @@
export const once = true;
export default async () => {
console.log('Connecté à Discord !');
};

36
src/events/loader.js Normal file
View file

@ -0,0 +1,36 @@
import { readdir } from 'fs/promises';
export default async client => {
const events_categories = (await readdir('./src/events'))
.filter(element => !element.endsWith('.js'));
events_categories.forEach(async event_category => {
// Retrieve events
const events = await readdir(`./src/events/${event_category}`);
// Load them into the client
Promise.all(
events.map(async event_file => {
const { once, default: execute } = await import(
`../events/${event_category}/${event_file}`
);
// Remove .js
let event_type = event_file.split('.');
if (event_type.pop() !== 'js') {
throw `Unknown file in ${event_category}: ${event_file}`;
}
event_type = event_type.join('.');
if (once) {
return client.once(event_type, (...args) => {
execute(...args, client);
});
}
return client.on(event_type, (...args) => {
execute(...args, client);
});
}),
);
});
};

32
src/index.js Normal file
View file

@ -0,0 +1,32 @@
import loadClient from './utils/client.js';
import loadEvents from './events/loader.js';
const run = async () => {
console.log('Starting Botanique...');
// Load .env if not in prod
if (process.env.NODE_ENV !== 'production') {
(await import('dotenv')).config({ path: './config/.env' });
}
// Client Discord.JS
const client = await loadClient()
.catch(() => {
throw 'Client ❌';
});
console.log('Client ✅');
await loadEvents(client)
.then(() => console.log('Events ✅'))
.catch(() => {
throw 'Events ❌';
});
await client.login(client.config.token_discord);
console.log(
`Botanique "${client.user.username}" ${client.config.version} started!`
);
};
run().catch(error => console.error(error));

20
src/utils/client.js Normal file
View file

@ -0,0 +1,20 @@
import { Client, Intents } from 'discord.js';
import { readFileSync } from 'fs';
const { version } = JSON.parse(readFileSync('./package.json'));
// Création du client et de ses propriétés
export default async () => {
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
],
});
client.config = {
version: version,
token_discord: process.env.TOKEN_DISCORD,
};
return client;
};