From f5879c849e8c21ef6486500e40b9c92bbd9ffa5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Melaine=20G=C3=A9rard?= Date: Wed, 30 Oct 2024 13:27:59 +0100 Subject: [PATCH] :sparkles: Passage en fichier .env/variables d'environnement --- .env.example | 2 ++ .gitignore | 5 +++-- data/config.example.json | 3 --- package-lock.json | 13 +++++++++++++ package.json | 1 + src/base/classes/CustomClient.ts | 6 +++++- src/base/classes/LoadConfig.ts | 12 ++++++++++++ 7 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 .env.example delete mode 100644 data/config.example.json create mode 100644 src/base/classes/LoadConfig.ts diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..571a58c --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +TOKEN="BOT_TOKEN" +GUILD_ID="1234567890" diff --git a/.gitignore b/.gitignore index 88f4cd7..8579ee8 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,6 @@ dist/ *.iml tsconfig.tsbuildinfo -data/*.json -!data/*.example.json \ No newline at end of file +.env +.env.* +!.env.example \ No newline at end of file diff --git a/data/config.example.json b/data/config.example.json deleted file mode 100644 index 4822252..0000000 --- a/data/config.example.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "token": "YOUR_DISCORD_TOKEN" -} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 6bc3dc9..b26df43 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "discord.js": "^14.16.2", + "dotenv": "^16.4.5", "glob": "^11.0.0", "typescript": "^5.6.2" } @@ -312,6 +313,18 @@ "url": "https://github.com/discordjs/discord.js?sponsor" } }, + "node_modules/dotenv": { + "version": "16.4.5", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", + "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/eastasianwidth": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", diff --git a/package.json b/package.json index ad1c673..a9f17df 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "license": "ISC", "dependencies": { "discord.js": "^14.16.2", + "dotenv": "^16.4.5", "glob": "^11.0.0", "typescript": "^5.6.2" } diff --git a/src/base/classes/CustomClient.ts b/src/base/classes/CustomClient.ts index 6e47339..f9d1aca 100644 --- a/src/base/classes/CustomClient.ts +++ b/src/base/classes/CustomClient.ts @@ -4,6 +4,7 @@ import {Client, Collection, GatewayIntentBits} from "discord.js"; import {Handler} from "./Handler"; import {Command} from "./Command"; import {SubCommand} from "./SubCommand"; +import {Config} from "./LoadConfig"; export class CustomClient extends Client implements ICustomClient { config: IConfig; @@ -18,8 +19,11 @@ export class CustomClient extends Client implements ICustomClient { return GatewayIntentBits[a as keyof typeof GatewayIntentBits]; }), }) + // On ajoute le support de dotenv pour la config plutôt qu'un fichier JSON + require('dotenv').config(); - this.config = require(`${process.cwd()}/data/config.json`); + // On charge dynamiquement la config + this.config = Config.load(); this.handler = new Handler(this); this.commands = new Collection(); this.subCommands = new Collection(); diff --git a/src/base/classes/LoadConfig.ts b/src/base/classes/LoadConfig.ts new file mode 100644 index 0000000..7acbff1 --- /dev/null +++ b/src/base/classes/LoadConfig.ts @@ -0,0 +1,12 @@ +import dotenv from 'dotenv'; +import type { IConfig } from '../interfaces/IConfig'; + +export class Config { + public static load(): IConfig { + dotenv.config(); + return new Proxy({} as IConfig, { + get: (_, prop: string): string | undefined => + process.env[prop.split(/(?=[A-Z])/).join('_').toUpperCase()] + }); + } +} \ No newline at end of file