seperate auth and env setup
This commit is contained in:
@@ -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": "",
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
30
src/setup-auth.ts
Normal file
30
src/setup-auth.ts
Normal 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),
|
||||
);
|
||||
@@ -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),
|
||||
);
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user