Skip to content

Commit c3ff5d7

Browse files
Add assert script to env vars, edit example file
1 parent c2d9a7c commit c3ff5d7

File tree

9 files changed

+47
-23
lines changed

9 files changed

+47
-23
lines changed

.env.example

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
DISCORD_BOT_TOKEN=foo
1+
# Required vars
2+
DISCORD_BOT_TOKEN=
3+
DISCORD_CLIENT_ID=
4+
MOD_LOG_CHANNEL_ID=
5+
6+
# If you plan to register new commands in dev
7+
# DEV_GUILD_ID=
28

39
# These are not required, you can set them to override the default channels IDs on dev
4-
#MOD_LOG_CHANNEL_ID=123456789012345678
5-
#RULES_CHANNEL_ID=123456789012345678
10+
# RULES_CHANNEL_ID=

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Next.js Discord Bot
22

3-
The source code of the Discord bot for the official [Next.js discord](https://nextjs.org/discord).
3+
The source code of the Discord bot for the official [Next.js Discord](https://nextjs.org/discord).
44

55
## Features
66

env.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
declare global {
2+
namespace NodeJS {
3+
interface ProcessEnv {
4+
// Required env variables
5+
DISCORD_BOT_TOKEN: string;
6+
DISCORD_CLIENT_ID: string;
7+
MOD_LOG_CHANNEL_ID: string;
8+
}
9+
}
10+
}
11+
12+
export {};

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"start": "node dist/index.js",
99
"dev": "ts-node-dev src/index.ts",
1010
"dev:register-commands": "DEV=true ts-node ./src/register-commands.ts",
11+
"register-commands": "ts-node ./src/register-commands.ts",
1112
"build": "tsc",
1213
"lint": "eslint src"
1314
},

src/assert-env-vars.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import assert from 'assert';
2+
import dotenv from 'dotenv';
3+
4+
dotenv.config();
5+
6+
const required = (name: string, val: string | undefined) =>
7+
assert(val, `${name} is not defined`);
8+
9+
const optional = (name: string, val: string | undefined) => {
10+
if (!val) {
11+
console.warn(`Warning: ${name} is not defined, some features may not work`);
12+
}
13+
};
14+
15+
required('DISCORD_BOT_TOKEN', process.env.DISCORD_BOT_TOKEN);
16+
required('DISCORD_CLIENT_ID', process.env.DISCORD_CLIENT_ID);
17+
required('MOD_LOG_CHANNEL_ID', process.env.MOD_LOG_CHANNEL_ID);
18+
optional('MODERATOR_ROLE_ID', process.env.MODERATOR_ROLE_ID);

src/commands/context/report.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ import { isStaff } from '../../utils';
1212
* Logs a message in the mod log channel. If a normal user uses the command it will ping the mods
1313
*/
1414

15-
const MOD_LOG_CHANNEL_ID =
16-
process.env.MOD_LOG_CHANNEL_ID ?? '763149438951882792';
17-
1815
// We will keep a memory cache of warned messages to avoid showing multiple warnings
1916
const warnedMessageIds: string[] = [];
2017

@@ -44,11 +41,11 @@ export const command: ContextMenuCommand = {
4441
return;
4542
}
4643

47-
const channel = client.channels.cache.get(MOD_LOG_CHANNEL_ID);
44+
const channel = client.channels.cache.get(process.env.MOD_LOG_CHANNEL_ID);
4845

4946
if (!channel || !channel.isTextBased()) {
5047
console.error(
51-
`No mod-log channel found (using the ID ${MOD_LOG_CHANNEL_ID})!`
48+
`No mod-log channel found (using the ID ${process.env.MOD_LOG_CHANNEL_ID})!`
5249
);
5350

5451
interaction.reply({

src/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3-
import dotenv from 'dotenv';
43
import discord, { Events, GatewayIntentBits, Partials, User } from 'discord.js';
5-
dotenv.config();
4+
import './assert-env-vars';
65

76
import { FeatureFile } from './types';
87
import { isJsOrTsFile } from './utils';

src/register-commands.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
import dotenv from 'dotenv';
21
import { REST, Routes } from 'discord.js';
3-
import { allCommands } from './commands';
2+
import './assert-env-vars';
43

5-
dotenv.config();
4+
import { allCommands } from './commands';
65

76
(async () => {
8-
if (!process.env.DISCORD_BOT_TOKEN) {
9-
throw new Error('No bot token found!');
10-
}
11-
12-
if (!process.env.DISCORD_CLIENT_ID) {
13-
throw new Error('DISCORD_CLIENT_ID is not set');
14-
}
15-
167
const isDevRegister = process.env.DEV === 'true';
178
const guildId = process.env.DEV_GUILD_ID;
189

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
"forceConsistentCasingInFileNames": true
1313
},
1414
"include": ["src/**/*.ts"],
15-
"exclude": ["node_modules"]
15+
"exclude": ["node_modules"],
16+
"files": ["env.d.ts"]
1617
}

0 commit comments

Comments
 (0)