Skip to content

Commit 1c47b7c

Browse files
committed
added new env stuff
1 parent f4fdf2e commit 1c47b7c

File tree

3 files changed

+71
-26
lines changed

3 files changed

+71
-26
lines changed

.env.example

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,16 @@ CONFIG_UPDATE_INTERVAL_YOUTUBE='10'
1818
CONFIG_UPDATE_INTERVAL_TWITCH='2'
1919
CONFIG_DISCORD_LOGS_CHANNEL='YOUR_DISCORD_LOGS_CHANNEL'
2020

21-
# Backups
22-
BACKUP_ENABLED=true
21+
# Postgres
22+
POSTGRES_HOST='YOUR_POSTGRES_HOST'
23+
POSTGRES_PORT='YOUR_POSTGRES_PORT'
24+
POSTGRES_USER='YOUR_POSTGRES_USER'
25+
POSTGRES_PASSWORD='YOUR_POSTGRES_PASSWORD'
26+
POSTGRES_DB='YOUR_POSTGRES_DB'
27+
28+
# Postgres Dev
29+
POSTGRES_DEV_HOST='YOUR_POSTGRES_DEV_HOST'
30+
POSTGRES_DEV_PORT='YOUR_POSTGRES_DEV_PORT'
31+
POSTGRES_DEV_USER='YOUR_POSTGRES_DEV_USER'
32+
POSTGRES_DEV_PASSWORD='YOUR_POSTGRES_DEV_PASSWORD'
33+
POSTGRES_DEV_DB='YOUR_POSTGRES_DEV_DB'

src/config.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// FILL IN THIS INFORMATION IN .ENV
2-
export const config: { [key: string]: string | number } = {
2+
export const runningInDevMode: boolean = process.argv.includes("--dev");
3+
export interface Config {
4+
updateIntervalYouTube: number;
5+
updateIntervalTwitch: number;
6+
}
7+
8+
export const config: Config = {
39
updateIntervalYouTube: process.env?.CONFIG_UPDATE_INTERVAL_YOUTUBE
410
? parseInt(process.env?.CONFIG_UPDATE_INTERVAL_YOUTUBE) * 1000
511
: 60_000,
@@ -8,11 +14,44 @@ export const config: { [key: string]: string | number } = {
814
: 60_000,
915
};
1016

11-
export const env: { [key: string]: string | undefined } = {
12-
discordToken: process.argv.includes("--dev")
17+
interface Env {
18+
discordToken: string | undefined;
19+
youtubeApiKey: string | undefined;
20+
twitchClientId: string | undefined;
21+
twitchClientSecret: string | undefined;
22+
}
23+
24+
export const env: Env = {
25+
discordToken: runningInDevMode
1326
? process.env?.DISCORD_DEV_TOKEN
1427
: process.env?.DISCORD_TOKEN,
1528
youtubeApiKey: process.env?.YOUTUBE_API_KEY,
1629
twitchClientId: process.env?.TWITCH_CLIENT_ID,
1730
twitchClientSecret: process.env?.TWITCH_CLIENT_SECRET,
1831
};
32+
33+
interface DatabaseConfig {
34+
host: string | undefined;
35+
port: string | undefined;
36+
user: string | undefined;
37+
password: string | undefined;
38+
database: string | undefined;
39+
}
40+
41+
export const dbCredentials: DatabaseConfig = {
42+
host: runningInDevMode
43+
? process.env?.POSTGRES_DEV_HOST
44+
: process.env?.POSTGRES_HOST,
45+
port: runningInDevMode
46+
? process.env?.POSTGRES_DEV_PORT
47+
: process.env?.POSTGRES_PORT,
48+
user: runningInDevMode
49+
? process.env?.POSTGRES_DEV_USER
50+
: process.env?.POSTGRES_USER,
51+
password: runningInDevMode
52+
? process.env?.POSTGRES_DEV_PASSWORD
53+
: process.env?.POSTGRES_PASSWORD,
54+
database: runningInDevMode
55+
? process.env?.POSTGRES_DEV_DB
56+
: process.env?.POSTGRES_DB,
57+
};

src/index.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
// Check if all the required environment variables are set
2+
import fs from "fs/promises";
3+
import path from "path";
4+
5+
import {
6+
Client,
7+
GatewayIntentBits,
8+
REST,
9+
Routes,
10+
type APIApplicationCommand,
11+
} from "discord.js";
12+
import { CronJob } from "cron";
13+
214
import { env } from "./config.ts";
15+
import commandsMap from "./commands.ts";
16+
import { initTables } from "./utils/database.ts";
17+
import { getTwitchToken } from "./utils/twitch/auth.ts";
18+
import backup from "./utils/backup.ts";
319

420
if (!env.discordToken || env.discordToken === "YOUR_DISCORD_TOKEN") {
521
throw new Error("You MUST provide a discord token in .env!");
@@ -20,27 +36,6 @@ if (
2036
throw new Error("You MUST provide a Twitch client secret in .env!");
2137
}
2238

23-
// If everything is set up correctly, continue with the bot
24-
import {
25-
Client,
26-
GatewayIntentBits,
27-
REST,
28-
Routes,
29-
type APIApplicationCommand,
30-
} from "discord.js";
31-
32-
import commandsMap from "./commands.ts";
33-
34-
import fs from "fs/promises";
35-
import path from "path";
36-
37-
import { initTables } from "./utils/database.ts";
38-
import { getTwitchToken } from "./utils/twitch/auth.ts";
39-
40-
import { CronJob } from "cron";
41-
42-
import backup from "./utils/backup.ts";
43-
4439
// Start the cron jobs
4540
await fs.mkdir(path.resolve(process.cwd(), "backups"), { recursive: true });
4641
new CronJob("0 0 * * *", async () => {

0 commit comments

Comments
 (0)