Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use shapeshift for env variables #371

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@sapphire/framework": "^4.5.1",
"@sapphire/plugin-i18next": "^5.0.4",
"@sapphire/plugin-logger": "^3.0.5",
"@sapphire/shapeshift": "^3.9.2",
"@sapphire/time-utilities": "^1.7.10",
"common-tags": "^1.8.2",
"discord-player": "^6.6.1",
Expand Down
2 changes: 2 additions & 0 deletions src/augment.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type {PrismaClient} from "@prisma/client";
import {Player} from "discord-player";
import type {parseEnv} from "./lib/Env";

export * from "@sapphire/pieces";
declare module "@sapphire/pieces" {
export interface Container {
player: Player;
prisma: PrismaClient;
env: ReturnType<typeof parseEnv>;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "@sapphire/plugin-logger/register";
import "@sapphire/plugin-i18next/register";
import "@kaname-png/plugin-statcord/register";
import {PrismaClient} from "@prisma/client";
import {parseEnv} from "./lib/Env";
config();

const client = new SapphireClient({
Expand All @@ -28,11 +29,12 @@ container.player = Player.singleton(client, {
});
container.logger = new Logger(container, {level: LogLevel.Debug});
container.prisma = new PrismaClient();
container.env = parseEnv();

async function main() {
await container.player.extractors.loadDefault();

await client.login(process.env.TOKEN).finally(() => container.prisma.$disconnect());
await client.login(container.env.TOKEN).finally(() => container.prisma.$disconnect());
}

void main();
20 changes: 20 additions & 0 deletions src/lib/Env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {s} from "@sapphire/shapeshift";

const envWebhookURL = s.string.url({allowedProtocols: ["https:"], allowedDomains: ["discord.com"]});

const envVariables = s.object({
TOKEN: s.string,
CONSOLE_URL: envWebhookURL,
GUILDS_URL: envWebhookURL,
GUILD: s.string,
OWNER: s.string,
CLIENT: s.string,
DATABASE_URL: s.string.url({
allowedProtocols: ["postgres:", "mysql:", "file:", "mongodb:", "sqlserver:"]
}),
STATCORD_API_KEY: s.string
});

export function parseEnv(env: Record<string, string | undefined> = process.env) {
return envVariables.parse(env);
}
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ __metadata:
languageName: node
linkType: hard

"@sapphire/shapeshift@npm:^3.8.2":
"@sapphire/shapeshift@npm:^3.8.2, @sapphire/shapeshift@npm:^3.9.2":
version: 3.9.2
resolution: "@sapphire/shapeshift@npm:3.9.2"
dependencies:
Expand Down Expand Up @@ -3223,6 +3223,7 @@ __metadata:
"@sapphire/framework": ^4.5.1
"@sapphire/plugin-i18next": ^5.0.4
"@sapphire/plugin-logger": ^3.0.5
"@sapphire/shapeshift": ^3.9.2
"@sapphire/time-utilities": ^1.7.10
"@sapphire/ts-config": 4.0.1
"@types/common-tags": 1.8.1
Expand Down