From d40708f878da8a0cc76b1cda68d3f19ae057c60a Mon Sep 17 00:00:00 2001 From: tresabhi Date: Thu, 20 Apr 2023 14:09:22 +0000 Subject: [PATCH] feat: switched to `.env` --- .gitignore | 2 +- README.md | 2 +- config.template.json | 4 ---- package.json | 1 + src/behaviors/interactionCreate.ts | 13 ++++++------- src/commands/verify.ts | 8 +++++--- src/index.ts | 8 ++++++-- src/utilities/tokenRequirements.ts | 12 ++++++++++++ yarn.lock | 5 +++++ 9 files changed, 37 insertions(+), 18 deletions(-) delete mode 100644 config.template.json create mode 100644 src/utilities/tokenRequirements.ts diff --git a/.gitignore b/.gitignore index 9d1d7bad2..75454837d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -config.json node_modules package-lock.json +.env diff --git a/README.md b/README.md index 2b7abd7b8..66371f679 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ yarn ### Configuring -Clone `config.template.json` to `config.json` and add your own Discord token and Wargaming application id. Also, replace entries in the `discord.json` file to match constants from your Discord server. +Add an `.env` file to the root of the project with your `DISCORD_TOKEN` and `b001f1746167d1f1102720ff336a1f9b` ### Running diff --git a/config.template.json b/config.template.json deleted file mode 100644 index 693542246..000000000 --- a/config.template.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "discord_token": "", - "wargaming_application_id": "" -} diff --git a/package.json b/package.json index cc710939e..4a57e486f 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "@faker-js/faker": "^7.6.0", "@types/node": "^18.15.11", "discord.js": "^14.9.0", + "dotenv": "^16.0.3", "ts-node": "^10.9.1", "typescript": "^5.0.4" } diff --git a/src/behaviors/interactionCreate.ts b/src/behaviors/interactionCreate.ts index a652d8ac4..818951765 100644 --- a/src/behaviors/interactionCreate.ts +++ b/src/behaviors/interactionCreate.ts @@ -1,4 +1,3 @@ -import discord from '../../discord.json' assert { type: 'json' }; import { CacheType, Collection, @@ -8,8 +7,11 @@ import { Routes, SlashCommandBuilder, } from 'discord.js'; +import { config } from 'dotenv'; import { readdirSync } from 'fs'; -import config from '../../config.json' assert { type: 'json' }; +import discord from '../../discord.json' assert { type: 'json' }; + +config(); export interface CommandRegistry { data: SlashCommandBuilder; @@ -19,7 +21,7 @@ export interface CommandRegistry { const commandCollection = new Collection(); const commands: RESTPostAPIChatInputApplicationCommandsJSONBody[] = []; const commandFolders = readdirSync('src/commands/'); -const rest = new REST().setToken(config.discord_token); +const rest = new REST().setToken(process.env.DISCORD_TOKEN!); for (const file of commandFolders) { const command = (await import(`../commands/${file}`)) as CommandRegistry; @@ -31,10 +33,7 @@ try { console.log(`Started refreshing ${commands.length} command(s).`); const data = (await rest.put( - Routes.applicationGuildCommands( - discord.client_id, - discord.guild_id, - ), + Routes.applicationGuildCommands(discord.client_id, discord.guild_id), { body: commands }, )) as { length: number }; diff --git a/src/commands/verify.ts b/src/commands/verify.ts index 1d7b05810..d42df3895 100644 --- a/src/commands/verify.ts +++ b/src/commands/verify.ts @@ -4,10 +4,12 @@ import { EmbedBuilder, SlashCommandBuilder, } from 'discord.js'; -import config from '../../config.json' assert { type: 'json' }; +import { config } from 'dotenv'; import discord from '../../discord.json' assert { type: 'json' }; import AccountList from '../types/accountList.js'; +config(); + const SERVER_NAMES = { com: 'North America', eu: 'Europe', asia: 'Asia' }; export async function execute( @@ -17,13 +19,13 @@ export async function execute( const server = interaction.options.getString('server')!; const serverName = SERVER_NAMES[server as keyof typeof SERVER_NAMES]; const players = (await fetch( - `https://api.wotblitz.${server}/wotb/account/list/?application_id=${config.wargaming_application_id}&search=${ign}`, + `https://api.wotblitz.${server}/wotb/account/list/?application_id=${process.env.WARGAMING_APPLICATION_ID}&search=${ign}`, ).then((response) => response.json())) as AccountList; if (players?.data?.[0].nickname === ign) { // good match const clanData = (await fetch( - `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${config.wargaming_application_id}&account_id=${players.data[0].account_id}&extra=clan`, + `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${process.env.WARGAMING_APPLICATION_ID}&account_id=${players.data[0].account_id}&extra=clan`, ).then((response) => response.json())) as { data: { [key: number]: { clan: { tag: string } | null } }; }; diff --git a/src/index.ts b/src/index.ts index 09bf2a2bf..8d2fa29c3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,12 @@ import { Client, GatewayIntentBits } from 'discord.js'; -import config from '../config.json' assert { type: 'json' }; +import { config } from 'dotenv'; import guildMemberAdd from './behaviors/guildMemberAdd.js'; import interactionCreate from './behaviors/interactionCreate.js'; import ready from './behaviors/ready.js'; +import tokenRequirements from './utilities/tokenRequirements.js'; + +config(); +tokenRequirements(); const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers], @@ -12,4 +16,4 @@ client.on('error', console.error); client.on('ready', ready); client.on('guildMemberAdd', guildMemberAdd); client.on('interactionCreate', interactionCreate); -client.login(config.discord_token); +client.login(process.env.DISCORD_TOKEN); diff --git a/src/utilities/tokenRequirements.ts b/src/utilities/tokenRequirements.ts new file mode 100644 index 000000000..d8905cdb2 --- /dev/null +++ b/src/utilities/tokenRequirements.ts @@ -0,0 +1,12 @@ +import { config } from 'dotenv'; + +config(); + +export default function tokenRequirements() { + if (!process.env.DISCORD_TOKEN) { + throw new Error('DISCORD_TOKEN is not defined'); + } + if (!process.env.WARGAMING_APPLICATION_ID) { + throw new Error('WARGAMING_APPLICATION_ID is not defined'); + } +} diff --git a/yarn.lock b/yarn.lock index 282935377..1ae871fee 100644 --- a/yarn.lock +++ b/yarn.lock @@ -187,6 +187,11 @@ discord.js@^14.9.0: undici "^5.21.0" ws "^8.13.0" +dotenv@^16.0.3: + version "16.0.3" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.3.tgz#115aec42bac5053db3c456db30cc243a5a836a07" + integrity sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ== + fast-deep-equal@^3.1.3: version "3.1.3" resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"