fix monitor

This commit is contained in:
jaydenfyi 2026-01-25 10:30:08 +08:00
parent 960bb108d0
commit 1d12c9e91f
3 changed files with 29 additions and 8 deletions

View File

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

View File

@ -122,13 +122,14 @@ async function processTwitchMessage(params: {
payload, payload,
channel: message.channel, channel: message.channel,
account, account,
accountId,
config,
tableMode,
runtime, runtime,
statusSink, statusSink,
}); });
}, },
}, },
runtime,
tableMode,
}); });
} }
@ -139,26 +140,37 @@ async function deliverTwitchReply(params: {
payload: ReplyPayload; payload: ReplyPayload;
channel: string; channel: string;
account: TwitchAccountConfig; account: TwitchAccountConfig;
accountId: string;
config: unknown;
tableMode: "off" | "plain" | "markdown" | "bullets" | "code";
runtime: TwitchRuntimeEnv; runtime: TwitchRuntimeEnv;
statusSink?: (patch: { lastInboundAt?: number; lastOutboundAt?: number }) => void; statusSink?: (patch: { lastInboundAt?: number; lastOutboundAt?: number }) => void;
}): Promise<void> { }): Promise<void> {
const { payload, channel, account, runtime, statusSink } = params; const { payload, channel, account, accountId, config, tableMode, runtime, statusSink } = params;
try { try {
const clientManager = getOrCreateClientManager(account.accountId ?? "default", { const clientManager = getOrCreateClientManager(accountId, {
info: (msg) => runtime.log?.(`[twitch] ${msg}`), info: (msg) => runtime.log?.(`[twitch] ${msg}`),
warn: (msg) => runtime.log?.(`[twitch] ${msg}`), warn: (msg) => runtime.log?.(`[twitch] ${msg}`),
error: (msg) => runtime.error?.(`[twitch] ${msg}`), error: (msg) => runtime.error?.(`[twitch] ${msg}`),
debug: (msg) => runtime.log?.(`[twitch] ${msg}`), debug: (msg) => runtime.log?.(`[twitch] ${msg}`),
}); });
const client = await clientManager.getClient(account, null, account.accountId ?? "default"); const client = await clientManager.getClient(
account,
config as Parameters<typeof clientManager.getClient>[1],
accountId,
);
if (!client) { if (!client) {
runtime.error?.(`[twitch] No client available for sending reply`); runtime.error?.(`[twitch] No client available for sending reply`);
return; return;
} }
// Send the reply // Send the reply
if (!payload.text) {
runtime.error?.(`[twitch] No text to send in reply payload`);
return;
}
await client.say(channel, payload.text); await client.say(channel, payload.text);
statusSink?.({ lastOutboundAt: Date.now() }); statusSink?.({ lastOutboundAt: Date.now() });
} catch (err) { } catch (err) {
@ -253,7 +265,5 @@ export async function monitorTwitchProvider(
// Handle abort signal // Handle abort signal
abortSignal.addEventListener("abort", stop, { once: true }); abortSignal.addEventListener("abort", stop, { once: true });
runtime.log?.(`[twitch] Monitor started for account ${accountId}`);
return { stop }; return { stop };
} }

View File

@ -228,10 +228,14 @@ export class TwitchClientManager {
/** /**
* Set a message handler for an account * Set a message handler for an account
* @returns A function that removes the handler when called
*/ */
onMessage(account: TwitchAccountConfig, handler: (message: TwitchChatMessage) => void): void { onMessage(account: TwitchAccountConfig, handler: (message: TwitchChatMessage) => void): () => void {
const key = this.getAccountKey(account); const key = this.getAccountKey(account);
this.messageHandlers.set(key, handler); this.messageHandlers.set(key, handler);
return () => {
this.messageHandlers.delete(key);
};
} }
/** /**