seperate auth and env setup

This commit is contained in:
Darius
2025-09-26 22:44:33 +02:00
parent 42ad079589
commit 44f3f7af0b
5 changed files with 40 additions and 24 deletions

View File

@@ -7,8 +7,9 @@
"clean": "rimraf dist", "clean": "rimraf dist",
"build": "npm run clean && tsc -p .", "build": "npm run clean && tsc -p .",
"start": "node dist/bot.js", "start": "node dist/bot.js",
"dev": "nodemon src/bot.ts", "dev": "nodemon src/bot.ts --verbose",
"setup": "nodemon src/setup.ts" "setup:env": "nodemon src/setup-env.ts --verbose",
"setup:auth": "nodemon src/setup-auth.ts --verbose"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",

View File

@@ -3,6 +3,7 @@ import type { ChatMessage } from "@twurple/chat";
import { commands } from "../commands/collection.ts"; import { commands } from "../commands/collection.ts";
import type { ICommand } from "../commands/interface.ts"; import type { ICommand } from "../commands/interface.ts";
import { Config } from "../config/config.ts"; import { Config } from "../config/config.ts";
import { logInfo } from "../util/logger.ts";
import { BaseEvent } from "./base-event.ts"; import { BaseEvent } from "./base-event.ts";
import type { EventName } from "./registry.ts"; import type { EventName } from "./registry.ts";
@@ -27,6 +28,7 @@ async function checkMessage(
text: string, text: string,
msg: ChatMessage, msg: ChatMessage,
) { ) {
logInfo(`message seen: ${channel} - ${user} - ${text}`);
const prefix = Config.prefix; const prefix = Config.prefix;
if (!text.startsWith(prefix)) return; if (!text.startsWith(prefix)) return;

30
src/setup-auth.ts Normal file
View File

@@ -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),
);

View File

@@ -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 { getUserId, promptForInput } from "./util/general.ts";
import { logError, logInfo, logWarning } from "./util/logger.ts"; import { logError, logInfo, logWarning } from "./util/logger.ts";
@@ -9,9 +5,6 @@ const botname = await promptForInput("enter bot username: ");
const developers = ( const developers = (
await promptForInput("enter developer usernames (,separated): ") await promptForInput("enter developer usernames (,separated): ")
).split(","); ).split(",");
const scopes = ["chat:read", "chat:edit", "channel:moderate"];
const port = 3000;
const redirectUri = `http://localhost:${port}`;
const botId = await getUserId(botname); const botId = await getUserId(botname);
if (!botId) { if (!botId) {
@@ -33,17 +26,3 @@ logInfo(`Userid of bot '${botname}' => '${botId}'`);
logInfo( logInfo(
`Userids of developers '${developers.join(",")}' => '${developerIds.join(",")}'`, `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),
);

View File

@@ -1,5 +1,7 @@
import chalk from "chalk"; import chalk from "chalk";
const verbose = process.argv.includes("--verbose");
export function logError(...args: unknown[]) { export function logError(...args: unknown[]) {
console.log(chalk.red(args)); console.log(chalk.red(args));
} }
@@ -13,5 +15,7 @@ export function logSuccess(...args: unknown[]) {
} }
export function logInfo(...args: unknown[]) { export function logInfo(...args: unknown[]) {
if (verbose) {
console.log(chalk.cyan(args)); console.log(chalk.cyan(args));
}
} }