From 44f3f7af0b68fe8d045b9ac39055939b4506e933 Mon Sep 17 00:00:00 2001 From: Darius Date: Fri, 26 Sep 2025 22:44:33 +0200 Subject: [PATCH] seperate auth and env setup --- package.json | 5 +++-- src/events/event-message.ts | 2 ++ src/setup-auth.ts | 30 ++++++++++++++++++++++++++++++ src/{setup.ts => setup-env.ts} | 21 --------------------- src/util/logger.ts | 6 +++++- 5 files changed, 40 insertions(+), 24 deletions(-) create mode 100644 src/setup-auth.ts rename src/{setup.ts => setup-env.ts} (52%) diff --git a/package.json b/package.json index 52caa54..ebf7146 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,9 @@ "clean": "rimraf dist", "build": "npm run clean && tsc -p .", "start": "node dist/bot.js", - "dev": "nodemon src/bot.ts", - "setup": "nodemon src/setup.ts" + "dev": "nodemon src/bot.ts --verbose", + "setup:env": "nodemon src/setup-env.ts --verbose", + "setup:auth": "nodemon src/setup-auth.ts --verbose" }, "keywords": [], "author": "", diff --git a/src/events/event-message.ts b/src/events/event-message.ts index 4fad0a2..41dd4e7 100644 --- a/src/events/event-message.ts +++ b/src/events/event-message.ts @@ -3,6 +3,7 @@ import type { ChatMessage } from "@twurple/chat"; import { commands } from "../commands/collection.ts"; import type { ICommand } from "../commands/interface.ts"; import { Config } from "../config/config.ts"; +import { logInfo } from "../util/logger.ts"; import { BaseEvent } from "./base-event.ts"; import type { EventName } from "./registry.ts"; @@ -27,6 +28,7 @@ async function checkMessage( text: string, msg: ChatMessage, ) { + logInfo(`message seen: ${channel} - ${user} - ${text}`); const prefix = Config.prefix; if (!text.startsWith(prefix)) return; diff --git a/src/setup-auth.ts b/src/setup-auth.ts new file mode 100644 index 0000000..0976663 --- /dev/null +++ b/src/setup-auth.ts @@ -0,0 +1,30 @@ +import { exchangeCode } from "@twurple/auth"; +import { Config } from "./config/config"; +import { TokenManager } from "./core/token-manager"; +import { TwitchAuth } from "./util/auth"; +import { getUserId, promptForInput } from "./util/general"; +import { logError, logInfo } from "./util/logger"; + +const port = 3000; +const redirectUri = `http://localhost:${port}`; +const scopes = ["chat:read", "chat:edit", "channel:moderate"]; + +const userName = await promptForInput("enter userName to authorize on: "); + +const userId = await getUserId(userName); +if (!userId) { + logError("user not found. please check the configuration"); + throw new Error(); +} + +const auth = new TwitchAuth(redirectUri); +const authUrl = auth.getAuthorizationUrl(scopes); +logInfo("To authorize your Twitch bot, visit this URL:"); +logInfo(authUrl); +const code = await auth.startCallbackServer(port, 120); + +const tokenManager = new TokenManager(userId); + +tokenManager.createTokenFile( + await exchangeCode(Config.client_id, Config.client_secret, code, redirectUri), +); diff --git a/src/setup.ts b/src/setup-env.ts similarity index 52% rename from src/setup.ts rename to src/setup-env.ts index 5903f9e..28604c1 100644 --- a/src/setup.ts +++ b/src/setup-env.ts @@ -1,7 +1,3 @@ -import { exchangeCode } from "@twurple/auth"; -import { Config } from "./config/config.ts"; -import { TokenManager } from "./core/token-manager.ts"; -import { TwitchAuth } from "./util/auth.ts"; import { getUserId, promptForInput } from "./util/general.ts"; import { logError, logInfo, logWarning } from "./util/logger.ts"; @@ -9,9 +5,6 @@ const botname = await promptForInput("enter bot username: "); const developers = ( await promptForInput("enter developer usernames (,separated): ") ).split(","); -const scopes = ["chat:read", "chat:edit", "channel:moderate"]; -const port = 3000; -const redirectUri = `http://localhost:${port}`; const botId = await getUserId(botname); if (!botId) { @@ -33,17 +26,3 @@ logInfo(`Userid of bot '${botname}' => '${botId}'`); logInfo( `Userids of developers '${developers.join(",")}' => '${developerIds.join(",")}'`, ); - -logInfo("--------"); - -const auth = new TwitchAuth(redirectUri); -const authUrl = auth.getAuthorizationUrl(scopes); -logInfo("To authorize your Twitch bot, visit this URL:"); -logInfo(authUrl); -const code = await auth.startCallbackServer(port, 120); - -const tokenManager = new TokenManager(botId); - -tokenManager.createTokenFile( - await exchangeCode(Config.client_id, Config.client_secret, code, redirectUri), -); diff --git a/src/util/logger.ts b/src/util/logger.ts index 24ad46e..68db067 100644 --- a/src/util/logger.ts +++ b/src/util/logger.ts @@ -1,5 +1,7 @@ import chalk from "chalk"; +const verbose = process.argv.includes("--verbose"); + export function logError(...args: unknown[]) { console.log(chalk.red(args)); } @@ -13,5 +15,7 @@ export function logSuccess(...args: unknown[]) { } export function logInfo(...args: unknown[]) { - console.log(chalk.cyan(args)); + if (verbose) { + console.log(chalk.cyan(args)); + } }