await authProvider setup

This commit is contained in:
jaydenfyi 2026-01-25 11:02:53 +08:00
parent 054ac2f006
commit a7cf0c660d
3 changed files with 7 additions and 103 deletions

View File

@ -1,7 +1,9 @@
{
"permissions": {
"allow": [
"Bash(pnpm build:*)"
"Bash(pnpm build:*)",
"Bash(pnpm test:*)",
"Bash(pnpm -w run test:*)"
]
}
}

View File

@ -1,98 +0,0 @@
import { StaticAuthProvider } from "@twurple/auth";
import { ChatClient } from "@twurple/chat";
type Args = {
token?: string;
clientId?: string;
username?: string;
channel?: string;
message?: string;
timeoutMs: number;
};
function parseArgs(argv: string[]): Args {
const args: Args = { timeoutMs: 10000 };
for (let i = 0; i < argv.length; i += 1) {
const key = argv[i];
const value = argv[i + 1];
if (!key?.startsWith("--")) continue;
switch (key) {
case "--token":
args.token = value;
i += 1;
break;
case "--client-id":
args.clientId = value;
i += 1;
break;
case "--username":
args.username = value;
i += 1;
break;
case "--channel":
args.channel = value;
i += 1;
break;
case "--message":
args.message = value;
i += 1;
break;
case "--timeout-ms":
args.timeoutMs = Number(value ?? 10000);
i += 1;
break;
default:
break;
}
}
return args;
}
async function run(): Promise<void> {
const args = parseArgs(process.argv.slice(2));
const token = args.token;
const clientId = args.clientId;
const username = args.username;
const channel = args.channel ?? username;
if (!token || !clientId || !username || !channel) {
console.error(
"Usage: npx tsx src/cli/test-connect.ts --token <token> --client-id <id> --username <bot> --channel <channel> [--timeout-ms 10000]",
);
process.exit(2);
}
const normalizedToken = token.startsWith("oauth:") ? token.slice(6) : token;
const authProvider = new StaticAuthProvider(clientId, normalizedToken);
const client = new ChatClient({
authProvider,
requestMembershipEvents: true,
});
const timeout = setTimeout(
() => {
console.error("Timed out waiting for connection.");
process.exit(1);
},
Number.isFinite(args.timeoutMs) ? args.timeoutMs : 10000,
);
try {
await Promise.resolve(client.connect());
await Promise.resolve(client.join(channel));
console.log(`Connected as ${username} and joined #${channel}`);
if (args.message) {
await Promise.resolve(client.say(channel, args.message));
console.log("Message sent.");
}
} finally {
clearTimeout(timeout);
client.quit();
}
}
run().catch((error) => {
console.error(error instanceof Error ? error.message : String(error));
process.exit(1);
});

View File

@ -17,10 +17,10 @@ export class TwitchClientManager {
/**
* Create an auth provider for the account.
*/
private createAuthProvider(
private async createAuthProvider(
account: TwitchAccountConfig,
normalizedToken: string,
): StaticAuthProvider | RefreshingAuthProvider {
): Promise<StaticAuthProvider | RefreshingAuthProvider> {
if (!account.clientId) {
throw new Error("Missing Twitch client ID");
}
@ -34,7 +34,7 @@ export class TwitchClientManager {
// Use addUserForToken to figure out the user ID from the token
// This works whether we have a refresh token or not
authProvider
await authProvider
.addUserForToken({
accessToken: normalizedToken,
refreshToken: account.refreshToken ?? null,
@ -123,7 +123,7 @@ export class TwitchClientManager {
const normalizedToken = normalizeToken(tokenResolution.token);
// Create auth provider
const authProvider = this.createAuthProvider(account, normalizedToken);
const authProvider = await this.createAuthProvider(account, normalizedToken);
const channel = account.channel ?? account.username;