diff --git a/bun.lockb b/bun.lockb index 991cd09..0776f04 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index c72ef9c..39921c4 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "@types/bun": "latest" }, "dependencies": { + "env-var": "^7.5.0", "gotify": "^1.1.0", "ts3-nodejs-library": "^3.4.1", "winston": "^3.8.2" diff --git a/src/app.ts b/src/app.ts index f985a2c..3b8063e 100644 --- a/src/app.ts +++ b/src/app.ts @@ -2,8 +2,10 @@ import { Gotify } from "gotify"; import { QueryProtocol, TeamSpeak, TextMessageTargetMode } from "ts3-nodejs-library" import { createLogger, transports, format } from "winston"; +import { GOTIFY_TITLE, GOTIFY_TOKEN, GOTIFY_URL, LOG_LEVEL, MODE, TS3_HOST, TS3_NICKNAME, TS3_PASSWORD, TS3_QUERY_PORT, TS3_SERVER_PORT, TS3_USERNAME } from "./env"; + const logger = createLogger({ - level: process.env.LOG_LEVEL, + level: LOG_LEVEL, transports: [new transports.Console()], format: format.combine( format.colorize(), @@ -12,22 +14,22 @@ const logger = createLogger({ }) const gotify = new Gotify({ - server: process.env.GOTIFY_URL, + server: GOTIFY_URL, }) const gotifyConfig = { - app: process.env.GOTIFY_TOKEN, - title: process.env.GOTIFY_TITLE || "ts3gotify" + app: GOTIFY_TOKEN, + title: GOTIFY_TITLE } function getModes() { const modeIsProvided = process.env.MODE != undefined return { - connect: modeIsProvided ? process.env.MODE?.includes("connect") || false : true, - disconnect: process.env.MODE?.includes("disconnect") || false, - moved: process.env.MODE?.includes("moved") || false, - message: process.env.MODE?.includes("message") || false + connect: MODE?.includes("connect") || false, + disconnect: MODE?.includes("disconnect") || false, + moved: MODE?.includes("moved") || false, + message: MODE?.includes("message") || false } } @@ -56,13 +58,13 @@ function handleMessage(message: string) { } TeamSpeak.connect({ - host: process.env.TS3_HOST || "info", - queryport: process.env.TS3_QUERY_PORT || 10011, - serverport: process.env.TS3_SERVER_PORT || 9987, + host: TS3_HOST, + queryport: TS3_QUERY_PORT, + serverport: TS3_SERVER_PORT, protocol: QueryProtocol.RAW, - username: process.env.TS3_USERNAME, - password: process.env.TS3_PASSWORD, - nickname: process.env.TS3_NICKNAME || "ts3gotify", + username: TS3_USERNAME, + password: TS3_PASSWORD, + nickname: TS3_NICKNAME, }).then((teamspeak) => { const mode = getModes() diff --git a/src/env.ts b/src/env.ts new file mode 100644 index 0000000..b2d94f8 --- /dev/null +++ b/src/env.ts @@ -0,0 +1,51 @@ +import { from } from "env-var"; + +const envVar = from(process.env, { + asLogLevel: (value) => { + const logLevels = ["error", "info", "debug"]; + if (logLevels.includes(value)) { + return value; + } else { + throw new Error("Invalid log level"); + } + }, + asTs3GotifyMode: (value) => { + const modes = ["connect", "disconnect", "moved", "message"]; + if (modes.includes(value)) { + return value; + } else { + throw new Error("Invalid mode"); + } + }, +}); + +export const LOG_LEVEL = envVar.get("LOG_LEVEL").default("info").asLogLevel(); + +export const TS3_HOST = envVar.get("TS3_HOST").required().asString(); +export const TS3_QUERY_PORT = envVar + .get("TS3_QUERY_PORT") + .default(10011) + .asPortNumber(); +export const TS3_SERVER_PORT = envVar + .get("TS3_SERVER_PORT") + .default(9987) + .asPortNumber(); +export const TS3_USERNAME = envVar.get("TS3_USERNAME").required().asString(); +export const TS3_PASSWORD = envVar.get("TS3_PASSWORD").required().asString(); +export const TS3_NICKNAME = envVar + .get("TS3_NICKNAME") + .default("ts3gotify") + .asString(); + +export const GOTIFY_URL = envVar.get("GOTIFY_URL").required().asUrlString(); +export const GOTIFY_TOKEN = envVar.get("GOTIFY_TOKEN").required().asString(); +export const GOTIFY_TITLE = envVar + .get("GOTIFY_TITLE") + .default("ts3gotify") + .asString(); + +export const MODE = envVar + .get("MODE") + .default("['connect']") + .asJsonArray() + .map((value) => value.asTs3GotifyMode()); diff --git a/src/environment.d.ts b/src/environment.d.ts deleted file mode 100644 index ecf206c..0000000 --- a/src/environment.d.ts +++ /dev/null @@ -1,19 +0,0 @@ -export { }; - -declare global { - namespace NodeJS { - interface ProcessEnv { - LOG_LEVEL?: "error" | "info" | "debug" - TS3_HOST: string; - TS3_QUERY_PORT?: number; - TS3_SERVER_PORT?: number; - TS3_USERNAME: string; - TS3_PASSWORD: string; - TS3_NICKNAME?: string; - GOTIFY_URL: string; - GOTIFY_TOKEN: string; - GOTIFY_TITLE?: string; - MODE?: Array<"connect" | "disconnect" | "moved" | "message"> - } - } -}