@openclaw/twitch 2026.2.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/README.md +89 -0
- package/index.ts +20 -0
- package/openclaw.plugin.json +9 -0
- package/package.json +20 -0
- package/src/access-control.test.ts +491 -0
- package/src/access-control.ts +166 -0
- package/src/actions.ts +174 -0
- package/src/client-manager-registry.ts +115 -0
- package/src/config-schema.ts +84 -0
- package/src/config.test.ts +87 -0
- package/src/config.ts +116 -0
- package/src/monitor.ts +273 -0
- package/src/onboarding.test.ts +316 -0
- package/src/onboarding.ts +417 -0
- package/src/outbound.test.ts +403 -0
- package/src/outbound.ts +187 -0
- package/src/plugin.test.ts +39 -0
- package/src/plugin.ts +274 -0
- package/src/probe.test.ts +196 -0
- package/src/probe.ts +119 -0
- package/src/resolver.ts +137 -0
- package/src/runtime.ts +14 -0
- package/src/send.test.ts +276 -0
- package/src/send.ts +136 -0
- package/src/status.test.ts +270 -0
- package/src/status.ts +179 -0
- package/src/test-fixtures.ts +30 -0
- package/src/token.test.ts +171 -0
- package/src/token.ts +91 -0
- package/src/twitch-client.test.ts +589 -0
- package/src/twitch-client.ts +277 -0
- package/src/types.ts +143 -0
- package/src/utils/markdown.ts +98 -0
- package/src/utils/twitch.ts +78 -0
- package/test/setup.ts +7 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import type { TwitchAccountConfig, TwitchChatMessage } from "./types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Result of checking access control for a Twitch message
|
|
5
|
+
*/
|
|
6
|
+
export type TwitchAccessControlResult = {
|
|
7
|
+
allowed: boolean;
|
|
8
|
+
reason?: string;
|
|
9
|
+
matchKey?: string;
|
|
10
|
+
matchSource?: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Check if a Twitch message should be allowed based on account configuration
|
|
15
|
+
*
|
|
16
|
+
* This function implements the access control logic for incoming Twitch messages,
|
|
17
|
+
* checking allowlists, role-based restrictions, and mention requirements.
|
|
18
|
+
*
|
|
19
|
+
* Priority order:
|
|
20
|
+
* 1. If `requireMention` is true, message must mention the bot
|
|
21
|
+
* 2. If `allowFrom` is set, sender must be in the allowlist (by user ID)
|
|
22
|
+
* 3. If `allowedRoles` is set (and `allowFrom` is not), sender must have at least one role
|
|
23
|
+
*
|
|
24
|
+
* Note: `allowFrom` is a hard allowlist. When set, only those user IDs are allowed.
|
|
25
|
+
* Use `allowedRoles` as an alternative when you don't want to maintain an allowlist.
|
|
26
|
+
*
|
|
27
|
+
* Available roles:
|
|
28
|
+
* - "moderator": Moderators
|
|
29
|
+
* - "owner": Channel owner/broadcaster
|
|
30
|
+
* - "vip": VIPs
|
|
31
|
+
* - "subscriber": Subscribers
|
|
32
|
+
* - "all": Anyone in the chat
|
|
33
|
+
*/
|
|
34
|
+
export function checkTwitchAccessControl(params: {
|
|
35
|
+
message: TwitchChatMessage;
|
|
36
|
+
account: TwitchAccountConfig;
|
|
37
|
+
botUsername: string;
|
|
38
|
+
}): TwitchAccessControlResult {
|
|
39
|
+
const { message, account, botUsername } = params;
|
|
40
|
+
|
|
41
|
+
if (account.requireMention ?? true) {
|
|
42
|
+
const mentions = extractMentions(message.message);
|
|
43
|
+
if (!mentions.includes(botUsername.toLowerCase())) {
|
|
44
|
+
return {
|
|
45
|
+
allowed: false,
|
|
46
|
+
reason: "message does not mention the bot (requireMention is enabled)",
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (account.allowFrom && account.allowFrom.length > 0) {
|
|
52
|
+
const allowFrom = account.allowFrom;
|
|
53
|
+
const senderId = message.userId;
|
|
54
|
+
|
|
55
|
+
if (!senderId) {
|
|
56
|
+
return {
|
|
57
|
+
allowed: false,
|
|
58
|
+
reason: "sender user ID not available for allowlist check",
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (allowFrom.includes(senderId)) {
|
|
63
|
+
return {
|
|
64
|
+
allowed: true,
|
|
65
|
+
matchKey: senderId,
|
|
66
|
+
matchSource: "allowlist",
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
allowed: false,
|
|
72
|
+
reason: "sender is not in allowFrom allowlist",
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (account.allowedRoles && account.allowedRoles.length > 0) {
|
|
77
|
+
const allowedRoles = account.allowedRoles;
|
|
78
|
+
|
|
79
|
+
// "all" grants access to everyone
|
|
80
|
+
if (allowedRoles.includes("all")) {
|
|
81
|
+
return {
|
|
82
|
+
allowed: true,
|
|
83
|
+
matchKey: "all",
|
|
84
|
+
matchSource: "role",
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const hasAllowedRole = checkSenderRoles({
|
|
89
|
+
message,
|
|
90
|
+
allowedRoles,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
if (!hasAllowedRole) {
|
|
94
|
+
return {
|
|
95
|
+
allowed: false,
|
|
96
|
+
reason: `sender does not have any of the required roles: ${allowedRoles.join(", ")}`,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
allowed: true,
|
|
102
|
+
matchKey: allowedRoles.join(","),
|
|
103
|
+
matchSource: "role",
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
allowed: true,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Check if the sender has any of the allowed roles
|
|
114
|
+
*/
|
|
115
|
+
function checkSenderRoles(params: { message: TwitchChatMessage; allowedRoles: string[] }): boolean {
|
|
116
|
+
const { message, allowedRoles } = params;
|
|
117
|
+
const { isMod, isOwner, isVip, isSub } = message;
|
|
118
|
+
|
|
119
|
+
for (const role of allowedRoles) {
|
|
120
|
+
switch (role) {
|
|
121
|
+
case "moderator":
|
|
122
|
+
if (isMod) {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
break;
|
|
126
|
+
case "owner":
|
|
127
|
+
if (isOwner) {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
break;
|
|
131
|
+
case "vip":
|
|
132
|
+
if (isVip) {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
break;
|
|
136
|
+
case "subscriber":
|
|
137
|
+
if (isSub) {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Extract @mentions from a Twitch chat message
|
|
149
|
+
*
|
|
150
|
+
* Returns a list of lowercase usernames that were mentioned in the message.
|
|
151
|
+
* Twitch mentions are in the format @username.
|
|
152
|
+
*/
|
|
153
|
+
export function extractMentions(message: string): string[] {
|
|
154
|
+
const mentionRegex = /@(\w+)/g;
|
|
155
|
+
const mentions: string[] = [];
|
|
156
|
+
let match: RegExpExecArray | null;
|
|
157
|
+
|
|
158
|
+
while ((match = mentionRegex.exec(message)) !== null) {
|
|
159
|
+
const username = match[1];
|
|
160
|
+
if (username) {
|
|
161
|
+
mentions.push(username.toLowerCase());
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return mentions;
|
|
166
|
+
}
|
package/src/actions.ts
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Twitch message actions adapter.
|
|
3
|
+
*
|
|
4
|
+
* Handles tool-based actions for Twitch, such as sending messages.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { DEFAULT_ACCOUNT_ID, getAccountConfig } from "./config.js";
|
|
8
|
+
import { twitchOutbound } from "./outbound.js";
|
|
9
|
+
import type { ChannelMessageActionAdapter, ChannelMessageActionContext } from "./types.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Create a tool result with error content.
|
|
13
|
+
*/
|
|
14
|
+
function errorResponse(error: string) {
|
|
15
|
+
return {
|
|
16
|
+
content: [
|
|
17
|
+
{
|
|
18
|
+
type: "text" as const,
|
|
19
|
+
text: JSON.stringify({ ok: false, error }),
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
details: { ok: false },
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Read a string parameter from action arguments.
|
|
28
|
+
*
|
|
29
|
+
* @param args - Action arguments
|
|
30
|
+
* @param key - Parameter key
|
|
31
|
+
* @param options - Options for reading the parameter
|
|
32
|
+
* @returns The parameter value or undefined if not found
|
|
33
|
+
*/
|
|
34
|
+
function readStringParam(
|
|
35
|
+
args: Record<string, unknown>,
|
|
36
|
+
key: string,
|
|
37
|
+
options: { required?: boolean; trim?: boolean } = {},
|
|
38
|
+
): string | undefined {
|
|
39
|
+
const value = args[key];
|
|
40
|
+
if (value === undefined || value === null) {
|
|
41
|
+
if (options.required) {
|
|
42
|
+
throw new Error(`Missing required parameter: ${key}`);
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Convert value to string safely
|
|
48
|
+
if (typeof value === "string") {
|
|
49
|
+
return options.trim !== false ? value.trim() : value;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
53
|
+
const str = String(value);
|
|
54
|
+
return options.trim !== false ? str.trim() : str;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
throw new Error(`Parameter ${key} must be a string, number, or boolean`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Supported Twitch actions */
|
|
61
|
+
const TWITCH_ACTIONS = new Set(["send" as const]);
|
|
62
|
+
type TwitchAction = typeof TWITCH_ACTIONS extends Set<infer U> ? U : never;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Twitch message actions adapter.
|
|
66
|
+
*/
|
|
67
|
+
export const twitchMessageActions: ChannelMessageActionAdapter = {
|
|
68
|
+
/**
|
|
69
|
+
* List available actions for this channel.
|
|
70
|
+
*/
|
|
71
|
+
listActions: () => [...TWITCH_ACTIONS],
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Check if an action is supported.
|
|
75
|
+
*/
|
|
76
|
+
supportsAction: ({ action }) => TWITCH_ACTIONS.has(action as TwitchAction),
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Extract tool send parameters from action arguments.
|
|
80
|
+
*
|
|
81
|
+
* Parses and validates the "to" and "message" parameters for sending.
|
|
82
|
+
*
|
|
83
|
+
* @param params - Arguments from the tool call
|
|
84
|
+
* @returns Parsed send parameters or null if invalid
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* const result = twitchMessageActions.extractToolSend!({
|
|
88
|
+
* args: { to: "#mychannel", message: "Hello!" }
|
|
89
|
+
* });
|
|
90
|
+
* // Returns: { to: "#mychannel", message: "Hello!" }
|
|
91
|
+
*/
|
|
92
|
+
extractToolSend: ({ args }) => {
|
|
93
|
+
try {
|
|
94
|
+
const to = readStringParam(args, "to", { required: true });
|
|
95
|
+
const message = readStringParam(args, "message", { required: true });
|
|
96
|
+
|
|
97
|
+
if (!to || !message) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return { to, message };
|
|
102
|
+
} catch {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Handle an action execution.
|
|
109
|
+
*
|
|
110
|
+
* Processes the "send" action to send messages to Twitch.
|
|
111
|
+
*
|
|
112
|
+
* @param ctx - Action context including action type, parameters, and config
|
|
113
|
+
* @returns Tool result with content or null if action not supported
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* const result = await twitchMessageActions.handleAction!({
|
|
117
|
+
* action: "send",
|
|
118
|
+
* params: { message: "Hello Twitch!", to: "#mychannel" },
|
|
119
|
+
* cfg: openclawConfig,
|
|
120
|
+
* accountId: "default",
|
|
121
|
+
* });
|
|
122
|
+
*/
|
|
123
|
+
handleAction: async (ctx: ChannelMessageActionContext) => {
|
|
124
|
+
if (ctx.action !== "send") {
|
|
125
|
+
return {
|
|
126
|
+
content: [{ type: "text" as const, text: "Unsupported action" }],
|
|
127
|
+
details: { ok: false, error: "Unsupported action" },
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const message = readStringParam(ctx.params, "message", { required: true });
|
|
132
|
+
const to = readStringParam(ctx.params, "to", { required: false });
|
|
133
|
+
const accountId = ctx.accountId ?? DEFAULT_ACCOUNT_ID;
|
|
134
|
+
|
|
135
|
+
const account = getAccountConfig(ctx.cfg, accountId);
|
|
136
|
+
if (!account) {
|
|
137
|
+
return errorResponse(
|
|
138
|
+
`Account not found: ${accountId}. Available accounts: ${Object.keys(ctx.cfg.channels?.twitch?.accounts ?? {}).join(", ") || "none"}`,
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Use the channel from account config (or override with `to` parameter)
|
|
143
|
+
const targetChannel = to || account.channel;
|
|
144
|
+
if (!targetChannel) {
|
|
145
|
+
return errorResponse("No channel specified and no default channel in account config");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (!twitchOutbound.sendText) {
|
|
149
|
+
return errorResponse("sendText not implemented");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
const result = await twitchOutbound.sendText({
|
|
154
|
+
cfg: ctx.cfg,
|
|
155
|
+
to: targetChannel,
|
|
156
|
+
text: message ?? "",
|
|
157
|
+
accountId,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
content: [
|
|
162
|
+
{
|
|
163
|
+
type: "text" as const,
|
|
164
|
+
text: JSON.stringify(result),
|
|
165
|
+
},
|
|
166
|
+
],
|
|
167
|
+
details: { ok: true },
|
|
168
|
+
};
|
|
169
|
+
} catch (error) {
|
|
170
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
171
|
+
return errorResponse(errorMsg);
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client manager registry for Twitch plugin.
|
|
3
|
+
*
|
|
4
|
+
* Manages the lifecycle of TwitchClientManager instances across the plugin,
|
|
5
|
+
* ensuring proper cleanup when accounts are stopped or reconfigured.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { TwitchClientManager } from "./twitch-client.js";
|
|
9
|
+
import type { ChannelLogSink } from "./types.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Registry entry tracking a client manager and its associated account.
|
|
13
|
+
*/
|
|
14
|
+
type RegistryEntry = {
|
|
15
|
+
/** The client manager instance */
|
|
16
|
+
manager: TwitchClientManager;
|
|
17
|
+
/** The account ID this manager is for */
|
|
18
|
+
accountId: string;
|
|
19
|
+
/** Logger for this entry */
|
|
20
|
+
logger: ChannelLogSink;
|
|
21
|
+
/** When this entry was created */
|
|
22
|
+
createdAt: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Global registry of client managers.
|
|
27
|
+
* Keyed by account ID.
|
|
28
|
+
*/
|
|
29
|
+
const registry = new Map<string, RegistryEntry>();
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Get or create a client manager for an account.
|
|
33
|
+
*
|
|
34
|
+
* @param accountId - The account ID
|
|
35
|
+
* @param logger - Logger instance
|
|
36
|
+
* @returns The client manager
|
|
37
|
+
*/
|
|
38
|
+
export function getOrCreateClientManager(
|
|
39
|
+
accountId: string,
|
|
40
|
+
logger: ChannelLogSink,
|
|
41
|
+
): TwitchClientManager {
|
|
42
|
+
const existing = registry.get(accountId);
|
|
43
|
+
if (existing) {
|
|
44
|
+
return existing.manager;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const manager = new TwitchClientManager(logger);
|
|
48
|
+
registry.set(accountId, {
|
|
49
|
+
manager,
|
|
50
|
+
accountId,
|
|
51
|
+
logger,
|
|
52
|
+
createdAt: Date.now(),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
logger.info(`Registered client manager for account: ${accountId}`);
|
|
56
|
+
return manager;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Get an existing client manager for an account.
|
|
61
|
+
*
|
|
62
|
+
* @param accountId - The account ID
|
|
63
|
+
* @returns The client manager, or undefined if not registered
|
|
64
|
+
*/
|
|
65
|
+
export function getClientManager(accountId: string): TwitchClientManager | undefined {
|
|
66
|
+
return registry.get(accountId)?.manager;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Disconnect and remove a client manager from the registry.
|
|
71
|
+
*
|
|
72
|
+
* @param accountId - The account ID
|
|
73
|
+
* @returns Promise that resolves when cleanup is complete
|
|
74
|
+
*/
|
|
75
|
+
export async function removeClientManager(accountId: string): Promise<void> {
|
|
76
|
+
const entry = registry.get(accountId);
|
|
77
|
+
if (!entry) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Disconnect the client manager
|
|
82
|
+
await entry.manager.disconnectAll();
|
|
83
|
+
|
|
84
|
+
// Remove from registry
|
|
85
|
+
registry.delete(accountId);
|
|
86
|
+
entry.logger.info(`Unregistered client manager for account: ${accountId}`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Disconnect and remove all client managers from the registry.
|
|
91
|
+
*
|
|
92
|
+
* @returns Promise that resolves when all cleanup is complete
|
|
93
|
+
*/
|
|
94
|
+
export async function removeAllClientManagers(): Promise<void> {
|
|
95
|
+
const promises = [...registry.keys()].map((accountId) => removeClientManager(accountId));
|
|
96
|
+
await Promise.all(promises);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get the number of registered client managers.
|
|
101
|
+
*
|
|
102
|
+
* @returns The count of registered managers
|
|
103
|
+
*/
|
|
104
|
+
export function getRegisteredClientManagerCount(): number {
|
|
105
|
+
return registry.size;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Clear all client managers without disconnecting.
|
|
110
|
+
*
|
|
111
|
+
* This is primarily for testing purposes.
|
|
112
|
+
*/
|
|
113
|
+
export function _clearAllClientManagersForTest(): void {
|
|
114
|
+
registry.clear();
|
|
115
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { MarkdownConfigSchema } from "openclaw/plugin-sdk";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Twitch user roles that can be allowed to interact with the bot
|
|
6
|
+
*/
|
|
7
|
+
const TwitchRoleSchema = z.enum(["moderator", "owner", "vip", "subscriber", "all"]);
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Twitch account configuration schema
|
|
11
|
+
*/
|
|
12
|
+
const TwitchAccountSchema = z.object({
|
|
13
|
+
/** Twitch username */
|
|
14
|
+
username: z.string(),
|
|
15
|
+
/** Twitch OAuth access token (requires chat:read and chat:write scopes) */
|
|
16
|
+
accessToken: z.string(),
|
|
17
|
+
/** Twitch client ID (from Twitch Developer Portal or twitchtokengenerator.com) */
|
|
18
|
+
clientId: z.string().optional(),
|
|
19
|
+
/** Channel name to join */
|
|
20
|
+
channel: z.string().min(1),
|
|
21
|
+
/** Enable this account */
|
|
22
|
+
enabled: z.boolean().optional(),
|
|
23
|
+
/** Allowlist of Twitch user IDs who can interact with the bot (use IDs for safety, not usernames) */
|
|
24
|
+
allowFrom: z.array(z.string()).optional(),
|
|
25
|
+
/** Roles allowed to interact with the bot (e.g., ["moderator", "vip", "subscriber"]) */
|
|
26
|
+
allowedRoles: z.array(TwitchRoleSchema).optional(),
|
|
27
|
+
/** Require @mention to trigger bot responses */
|
|
28
|
+
requireMention: z.boolean().optional(),
|
|
29
|
+
/** Outbound response prefix override for this channel/account. */
|
|
30
|
+
responsePrefix: z.string().optional(),
|
|
31
|
+
/** Twitch client secret (required for token refresh via RefreshingAuthProvider) */
|
|
32
|
+
clientSecret: z.string().optional(),
|
|
33
|
+
/** Refresh token (required for automatic token refresh) */
|
|
34
|
+
refreshToken: z.string().optional(),
|
|
35
|
+
/** Token expiry time in seconds (optional, for token refresh tracking) */
|
|
36
|
+
expiresIn: z.number().nullable().optional(),
|
|
37
|
+
/** Timestamp when token was obtained (optional, for token refresh tracking) */
|
|
38
|
+
obtainmentTimestamp: z.number().optional(),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Base configuration properties shared by both single and multi-account modes
|
|
43
|
+
*/
|
|
44
|
+
const TwitchConfigBaseSchema = z.object({
|
|
45
|
+
name: z.string().optional(),
|
|
46
|
+
enabled: z.boolean().optional(),
|
|
47
|
+
markdown: MarkdownConfigSchema.optional(),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Simplified single-account configuration schema
|
|
52
|
+
*
|
|
53
|
+
* Use this for single-account setups. Properties are at the top level,
|
|
54
|
+
* creating an implicit "default" account.
|
|
55
|
+
*/
|
|
56
|
+
const SimplifiedSchema = z.intersection(TwitchConfigBaseSchema, TwitchAccountSchema);
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Multi-account configuration schema
|
|
60
|
+
*
|
|
61
|
+
* Use this for multi-account setups. Each key is an account ID (e.g., "default", "secondary").
|
|
62
|
+
*/
|
|
63
|
+
const MultiAccountSchema = z.intersection(
|
|
64
|
+
TwitchConfigBaseSchema,
|
|
65
|
+
z
|
|
66
|
+
.object({
|
|
67
|
+
/** Per-account configuration (for multi-account setups) */
|
|
68
|
+
accounts: z.record(z.string(), TwitchAccountSchema),
|
|
69
|
+
})
|
|
70
|
+
.refine((val) => Object.keys(val.accounts || {}).length > 0, {
|
|
71
|
+
message: "accounts must contain at least one entry",
|
|
72
|
+
}),
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Twitch plugin configuration schema
|
|
77
|
+
*
|
|
78
|
+
* Supports two mutually exclusive patterns:
|
|
79
|
+
* 1. Simplified single-account: username, accessToken, clientId, channel at top level
|
|
80
|
+
* 2. Multi-account: accounts object with named account configs
|
|
81
|
+
*
|
|
82
|
+
* The union ensures clear discrimination between the two modes.
|
|
83
|
+
*/
|
|
84
|
+
export const TwitchConfigSchema = z.union([SimplifiedSchema, MultiAccountSchema]);
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { getAccountConfig } from "./config.js";
|
|
3
|
+
|
|
4
|
+
describe("getAccountConfig", () => {
|
|
5
|
+
const mockMultiAccountConfig = {
|
|
6
|
+
channels: {
|
|
7
|
+
twitch: {
|
|
8
|
+
accounts: {
|
|
9
|
+
default: {
|
|
10
|
+
username: "testbot",
|
|
11
|
+
accessToken: "oauth:test123",
|
|
12
|
+
},
|
|
13
|
+
secondary: {
|
|
14
|
+
username: "secondbot",
|
|
15
|
+
accessToken: "oauth:secondary",
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const mockSimplifiedConfig = {
|
|
23
|
+
channels: {
|
|
24
|
+
twitch: {
|
|
25
|
+
username: "testbot",
|
|
26
|
+
accessToken: "oauth:test123",
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
it("returns account config for valid account ID (multi-account)", () => {
|
|
32
|
+
const result = getAccountConfig(mockMultiAccountConfig, "default");
|
|
33
|
+
|
|
34
|
+
expect(result).not.toBeNull();
|
|
35
|
+
expect(result?.username).toBe("testbot");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("returns account config for default account (simplified config)", () => {
|
|
39
|
+
const result = getAccountConfig(mockSimplifiedConfig, "default");
|
|
40
|
+
|
|
41
|
+
expect(result).not.toBeNull();
|
|
42
|
+
expect(result?.username).toBe("testbot");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("returns non-default account from multi-account config", () => {
|
|
46
|
+
const result = getAccountConfig(mockMultiAccountConfig, "secondary");
|
|
47
|
+
|
|
48
|
+
expect(result).not.toBeNull();
|
|
49
|
+
expect(result?.username).toBe("secondbot");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("returns null for non-existent account ID", () => {
|
|
53
|
+
const result = getAccountConfig(mockMultiAccountConfig, "nonexistent");
|
|
54
|
+
|
|
55
|
+
expect(result).toBeNull();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("returns null when core config is null", () => {
|
|
59
|
+
const result = getAccountConfig(null, "default");
|
|
60
|
+
|
|
61
|
+
expect(result).toBeNull();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("returns null when core config is undefined", () => {
|
|
65
|
+
const result = getAccountConfig(undefined, "default");
|
|
66
|
+
|
|
67
|
+
expect(result).toBeNull();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("returns null when channels are not defined", () => {
|
|
71
|
+
const result = getAccountConfig({}, "default");
|
|
72
|
+
|
|
73
|
+
expect(result).toBeNull();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("returns null when twitch is not defined", () => {
|
|
77
|
+
const result = getAccountConfig({ channels: {} }, "default");
|
|
78
|
+
|
|
79
|
+
expect(result).toBeNull();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("returns null when accounts are not defined", () => {
|
|
83
|
+
const result = getAccountConfig({ channels: { twitch: {} } }, "default");
|
|
84
|
+
|
|
85
|
+
expect(result).toBeNull();
|
|
86
|
+
});
|
|
87
|
+
});
|