@gakr-gakr/twitch 0.1.0
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/README.md +89 -0
- package/api.ts +21 -0
- package/autobot.plugin.json +15 -0
- package/channel-plugin-api.ts +1 -0
- package/index.ts +16 -0
- package/package.json +50 -0
- package/runtime-api.ts +22 -0
- package/setup-entry.ts +9 -0
- package/setup-plugin-api.ts +3 -0
- package/src/access-control.ts +195 -0
- package/src/actions.ts +175 -0
- package/src/client-manager-registry.ts +109 -0
- package/src/config-schema.ts +88 -0
- package/src/config.ts +177 -0
- package/src/monitor.ts +311 -0
- package/src/outbound.ts +242 -0
- package/src/plugin.ts +220 -0
- package/src/probe.ts +130 -0
- package/src/resolver.ts +139 -0
- package/src/runtime.ts +9 -0
- package/src/send.ts +191 -0
- package/src/setup-surface.ts +526 -0
- package/src/status.ts +179 -0
- package/src/token.ts +93 -0
- package/src/twitch-client.ts +281 -0
- package/src/types.ts +104 -0
- package/src/utils/markdown.ts +98 -0
- package/src/utils/twitch.ts +81 -0
- package/tsconfig.json +16 -0
package/src/resolver.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Twitch resolver adapter for channel/user name resolution.
|
|
3
|
+
*
|
|
4
|
+
* This module implements the ChannelResolverAdapter interface to resolve
|
|
5
|
+
* Twitch usernames to user IDs via the Twitch Helix API.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { ApiClient } from "@twurple/api";
|
|
9
|
+
import { StaticAuthProvider } from "@twurple/auth";
|
|
10
|
+
import { formatErrorMessage } from "autobot/plugin-sdk/error-runtime";
|
|
11
|
+
import { normalizeLowercaseStringOrEmpty } from "autobot/plugin-sdk/string-coerce-runtime";
|
|
12
|
+
import type { ChannelResolveKind, ChannelResolveResult } from "./types.js";
|
|
13
|
+
import type { ChannelLogSink, TwitchAccountConfig } from "./types.js";
|
|
14
|
+
import { normalizeToken } from "./utils/twitch.js";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Normalize a Twitch username - strip @ prefix and convert to lowercase
|
|
18
|
+
*/
|
|
19
|
+
function normalizeUsername(input: string): string {
|
|
20
|
+
const trimmed = input.trim();
|
|
21
|
+
if (trimmed.startsWith("@")) {
|
|
22
|
+
return normalizeLowercaseStringOrEmpty(trimmed.slice(1));
|
|
23
|
+
}
|
|
24
|
+
return normalizeLowercaseStringOrEmpty(trimmed);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create a logger that includes the Twitch prefix
|
|
29
|
+
*/
|
|
30
|
+
function createLogger(logger?: ChannelLogSink): ChannelLogSink {
|
|
31
|
+
return {
|
|
32
|
+
info: (msg: string) => logger?.info(msg),
|
|
33
|
+
warn: (msg: string) => logger?.warn(msg),
|
|
34
|
+
error: (msg: string) => logger?.error(msg),
|
|
35
|
+
debug: (msg: string) => logger?.debug?.(msg) ?? (() => {}),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Resolve Twitch usernames to user IDs via the Helix API
|
|
41
|
+
*
|
|
42
|
+
* @param inputs - Array of usernames or user IDs to resolve
|
|
43
|
+
* @param account - Twitch account configuration with auth credentials
|
|
44
|
+
* @param kind - Type of target to resolve ("user" or "group")
|
|
45
|
+
* @param logger - Optional logger
|
|
46
|
+
* @returns Promise resolving to array of ChannelResolveResult
|
|
47
|
+
*/
|
|
48
|
+
export async function resolveTwitchTargets(
|
|
49
|
+
inputs: string[],
|
|
50
|
+
account: TwitchAccountConfig,
|
|
51
|
+
_kind: ChannelResolveKind,
|
|
52
|
+
logger?: ChannelLogSink,
|
|
53
|
+
): Promise<ChannelResolveResult[]> {
|
|
54
|
+
const log = createLogger(logger);
|
|
55
|
+
|
|
56
|
+
if (!account.clientId || !account.accessToken) {
|
|
57
|
+
log.error("Missing Twitch client ID or accessToken");
|
|
58
|
+
return inputs.map((input) => ({
|
|
59
|
+
input,
|
|
60
|
+
resolved: false,
|
|
61
|
+
note: "missing Twitch credentials",
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const normalizedToken = normalizeToken(account.accessToken);
|
|
66
|
+
|
|
67
|
+
const authProvider = new StaticAuthProvider(account.clientId, normalizedToken);
|
|
68
|
+
const apiClient = new ApiClient({ authProvider });
|
|
69
|
+
|
|
70
|
+
const results: ChannelResolveResult[] = [];
|
|
71
|
+
|
|
72
|
+
for (const input of inputs) {
|
|
73
|
+
const normalized = normalizeUsername(input);
|
|
74
|
+
|
|
75
|
+
if (!normalized) {
|
|
76
|
+
results.push({
|
|
77
|
+
input,
|
|
78
|
+
resolved: false,
|
|
79
|
+
note: "empty input",
|
|
80
|
+
});
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const looksLikeUserId = /^\d+$/.test(normalized);
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
if (looksLikeUserId) {
|
|
88
|
+
const user = await apiClient.users.getUserById(normalized);
|
|
89
|
+
|
|
90
|
+
if (user) {
|
|
91
|
+
results.push({
|
|
92
|
+
input,
|
|
93
|
+
resolved: true,
|
|
94
|
+
id: user.id,
|
|
95
|
+
name: user.name,
|
|
96
|
+
});
|
|
97
|
+
log.debug?.(`Resolved user ID ${normalized} -> ${user.name}`);
|
|
98
|
+
} else {
|
|
99
|
+
results.push({
|
|
100
|
+
input,
|
|
101
|
+
resolved: false,
|
|
102
|
+
note: "user ID not found",
|
|
103
|
+
});
|
|
104
|
+
log.warn(`User ID ${normalized} not found`);
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
const user = await apiClient.users.getUserByName(normalized);
|
|
108
|
+
|
|
109
|
+
if (user) {
|
|
110
|
+
results.push({
|
|
111
|
+
input,
|
|
112
|
+
resolved: true,
|
|
113
|
+
id: user.id,
|
|
114
|
+
name: user.name,
|
|
115
|
+
note: user.displayName !== user.name ? `display: ${user.displayName}` : undefined,
|
|
116
|
+
});
|
|
117
|
+
log.debug?.(`Resolved username ${normalized} -> ${user.id} (${user.name})`);
|
|
118
|
+
} else {
|
|
119
|
+
results.push({
|
|
120
|
+
input,
|
|
121
|
+
resolved: false,
|
|
122
|
+
note: "username not found",
|
|
123
|
+
});
|
|
124
|
+
log.warn(`Username ${normalized} not found`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
} catch (error) {
|
|
128
|
+
const errorMessage = formatErrorMessage(error);
|
|
129
|
+
results.push({
|
|
130
|
+
input,
|
|
131
|
+
resolved: false,
|
|
132
|
+
note: `API error: ${errorMessage}`,
|
|
133
|
+
});
|
|
134
|
+
log.error(`Failed to resolve ${input}: ${errorMessage}`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return results;
|
|
139
|
+
}
|
package/src/runtime.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { PluginRuntime } from "autobot/plugin-sdk/core";
|
|
2
|
+
import { createPluginRuntimeStore } from "autobot/plugin-sdk/runtime-store";
|
|
3
|
+
|
|
4
|
+
const { setRuntime: setTwitchRuntime, getRuntime: getTwitchRuntime } =
|
|
5
|
+
createPluginRuntimeStore<PluginRuntime>({
|
|
6
|
+
pluginId: "twitch",
|
|
7
|
+
errorMessage: "Twitch runtime not initialized",
|
|
8
|
+
});
|
|
9
|
+
export { getTwitchRuntime, setTwitchRuntime };
|
package/src/send.ts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Twitch message sending functions with dependency injection support.
|
|
3
|
+
*
|
|
4
|
+
* These functions are the primary interface for sending messages to Twitch.
|
|
5
|
+
* They support dependency injection via the `deps` parameter for testability.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
createMessageReceiptFromOutboundResults,
|
|
10
|
+
type MessageReceipt,
|
|
11
|
+
} from "autobot/plugin-sdk/channel-message";
|
|
12
|
+
import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
|
|
13
|
+
import { formatErrorMessage } from "autobot/plugin-sdk/error-runtime";
|
|
14
|
+
import { getClientManager as getRegistryClientManager } from "./client-manager-registry.js";
|
|
15
|
+
import { resolveTwitchAccountContext } from "./config.js";
|
|
16
|
+
import { stripMarkdownForTwitch } from "./utils/markdown.js";
|
|
17
|
+
import { generateMessageId, normalizeTwitchChannel } from "./utils/twitch.js";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Result from sending a message to Twitch.
|
|
21
|
+
*/
|
|
22
|
+
export interface SendMessageResult {
|
|
23
|
+
/** Whether the send was successful */
|
|
24
|
+
ok: boolean;
|
|
25
|
+
/** The message ID (generated for tracking) */
|
|
26
|
+
messageId: string;
|
|
27
|
+
/** Receipt for visible sends; empty when no Twitch message was sent */
|
|
28
|
+
receipt: MessageReceipt;
|
|
29
|
+
/** Error message if the send failed */
|
|
30
|
+
error?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function createTwitchSendReceipt(params: {
|
|
34
|
+
messageId: string;
|
|
35
|
+
channel?: string | null;
|
|
36
|
+
visible?: boolean;
|
|
37
|
+
}): MessageReceipt {
|
|
38
|
+
const messageId = params.messageId.trim();
|
|
39
|
+
const conversationId = params.channel?.trim();
|
|
40
|
+
const hasVisibleMessage = params.visible === true && messageId && messageId !== "skipped";
|
|
41
|
+
return createMessageReceiptFromOutboundResults({
|
|
42
|
+
results: hasVisibleMessage
|
|
43
|
+
? [
|
|
44
|
+
{
|
|
45
|
+
channel: "twitch",
|
|
46
|
+
messageId,
|
|
47
|
+
...(conversationId ? { conversationId } : {}),
|
|
48
|
+
},
|
|
49
|
+
]
|
|
50
|
+
: [],
|
|
51
|
+
kind: "text",
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Internal send function used by the outbound adapter.
|
|
57
|
+
*
|
|
58
|
+
* This function has access to the full AutoBot config and handles
|
|
59
|
+
* account resolution, markdown stripping, and actual message sending.
|
|
60
|
+
*
|
|
61
|
+
* @param channel - The channel name
|
|
62
|
+
* @param text - The message text
|
|
63
|
+
* @param cfg - Full AutoBot configuration
|
|
64
|
+
* @param accountId - Account ID to use
|
|
65
|
+
* @param stripMarkdown - Whether to strip markdown (default: true)
|
|
66
|
+
* @param logger - Logger instance
|
|
67
|
+
* @returns Result with message ID and status
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* const result = await sendMessageTwitchInternal(
|
|
71
|
+
* "#mychannel",
|
|
72
|
+
* "Hello Twitch!",
|
|
73
|
+
* autobotConfig,
|
|
74
|
+
* "default",
|
|
75
|
+
* true,
|
|
76
|
+
* console,
|
|
77
|
+
* );
|
|
78
|
+
*/
|
|
79
|
+
export async function sendMessageTwitchInternal(
|
|
80
|
+
channel: string,
|
|
81
|
+
text: string,
|
|
82
|
+
cfg: AutoBotConfig,
|
|
83
|
+
accountId?: string,
|
|
84
|
+
stripMarkdown: boolean = true,
|
|
85
|
+
logger: Console = console,
|
|
86
|
+
): Promise<SendMessageResult> {
|
|
87
|
+
const {
|
|
88
|
+
account,
|
|
89
|
+
configured,
|
|
90
|
+
availableAccountIds,
|
|
91
|
+
accountId: resolvedAccountId,
|
|
92
|
+
} = resolveTwitchAccountContext(cfg, accountId);
|
|
93
|
+
if (!account) {
|
|
94
|
+
return {
|
|
95
|
+
ok: false,
|
|
96
|
+
messageId: generateMessageId(),
|
|
97
|
+
receipt: createTwitchSendReceipt({ messageId: "", channel, visible: false }),
|
|
98
|
+
error: `Account not found: ${accountId ?? "(default)"}. Available accounts: ${availableAccountIds.join(", ") || "none"}`,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (!configured) {
|
|
103
|
+
return {
|
|
104
|
+
ok: false,
|
|
105
|
+
messageId: generateMessageId(),
|
|
106
|
+
receipt: createTwitchSendReceipt({ messageId: "", channel, visible: false }),
|
|
107
|
+
error:
|
|
108
|
+
`Account ${resolvedAccountId} is not properly configured. ` +
|
|
109
|
+
"Required: username, clientId, and token (config or env for default account).",
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const normalizedChannel = channel || account.channel;
|
|
114
|
+
if (!normalizedChannel) {
|
|
115
|
+
return {
|
|
116
|
+
ok: false,
|
|
117
|
+
messageId: generateMessageId(),
|
|
118
|
+
receipt: createTwitchSendReceipt({
|
|
119
|
+
messageId: "",
|
|
120
|
+
channel: normalizedChannel,
|
|
121
|
+
visible: false,
|
|
122
|
+
}),
|
|
123
|
+
error: "No channel specified and no default channel in account config",
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const deliveryChannel = normalizeTwitchChannel(normalizedChannel);
|
|
127
|
+
|
|
128
|
+
const cleanedText = stripMarkdown ? stripMarkdownForTwitch(text) : text;
|
|
129
|
+
if (!cleanedText) {
|
|
130
|
+
return {
|
|
131
|
+
ok: true,
|
|
132
|
+
messageId: "skipped",
|
|
133
|
+
receipt: createTwitchSendReceipt({
|
|
134
|
+
messageId: "skipped",
|
|
135
|
+
channel: deliveryChannel,
|
|
136
|
+
visible: false,
|
|
137
|
+
}),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const clientManager = getRegistryClientManager(resolvedAccountId);
|
|
142
|
+
if (!clientManager) {
|
|
143
|
+
return {
|
|
144
|
+
ok: false,
|
|
145
|
+
messageId: generateMessageId(),
|
|
146
|
+
receipt: createTwitchSendReceipt({
|
|
147
|
+
messageId: "",
|
|
148
|
+
channel: deliveryChannel,
|
|
149
|
+
visible: false,
|
|
150
|
+
}),
|
|
151
|
+
error: `Client manager not found for account: ${resolvedAccountId}. Please start the Twitch gateway first.`,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
const result = await clientManager.sendMessage(
|
|
157
|
+
account,
|
|
158
|
+
deliveryChannel,
|
|
159
|
+
cleanedText,
|
|
160
|
+
cfg,
|
|
161
|
+
resolvedAccountId,
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
if (!result.ok) {
|
|
165
|
+
const messageId = result.messageId ?? generateMessageId();
|
|
166
|
+
return {
|
|
167
|
+
ok: false,
|
|
168
|
+
messageId,
|
|
169
|
+
receipt: createTwitchSendReceipt({ messageId, channel: deliveryChannel, visible: false }),
|
|
170
|
+
error: result.error ?? "Send failed",
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const messageId = result.messageId ?? generateMessageId();
|
|
175
|
+
return {
|
|
176
|
+
ok: true,
|
|
177
|
+
messageId,
|
|
178
|
+
receipt: createTwitchSendReceipt({ messageId, channel: deliveryChannel, visible: true }),
|
|
179
|
+
};
|
|
180
|
+
} catch (error) {
|
|
181
|
+
const errorMsg = formatErrorMessage(error);
|
|
182
|
+
const messageId = generateMessageId();
|
|
183
|
+
logger.error(`Failed to send message: ${errorMsg}`);
|
|
184
|
+
return {
|
|
185
|
+
ok: false,
|
|
186
|
+
messageId,
|
|
187
|
+
receipt: createTwitchSendReceipt({ messageId, channel: deliveryChannel, visible: false }),
|
|
188
|
+
error: errorMsg,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
}
|