@sdeck/twitch 0.0.0-stage → 1.0.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 +27 -2
- package/dist/actions.d.ts +4 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/api.d.ts +60 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/events.d.ts +22 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1391 -0
- package/dist/index.js.map +17 -0
- package/dist/model.d.ts +117 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/plugin.d.ts +44 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/state.d.ts +18 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/triggers.d.ts +2 -0
- package/dist/triggers.d.ts.map +1 -0
- package/package.json +63 -4
- package/src/actions.ts +597 -0
- package/src/api.ts +307 -0
- package/src/events.ts +170 -0
- package/src/index.tsx +52 -0
- package/src/model.ts +195 -0
- package/src/plugin.ts +193 -0
- package/src/state.ts +75 -0
- package/src/triggers.ts +140 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1391 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/actions.ts
|
|
3
|
+
import { setting as setting2 } from "@sdeck/plugin";
|
|
4
|
+
import {
|
|
5
|
+
captionLayer,
|
|
6
|
+
iconLayer,
|
|
7
|
+
iconLook,
|
|
8
|
+
iconSize,
|
|
9
|
+
rectLayer,
|
|
10
|
+
surface,
|
|
11
|
+
switchLook,
|
|
12
|
+
textLayer
|
|
13
|
+
} from "@sdeck/schema";
|
|
14
|
+
|
|
15
|
+
// src/api.ts
|
|
16
|
+
var SCOPES = [
|
|
17
|
+
"bits:read",
|
|
18
|
+
"channel:edit:commercial",
|
|
19
|
+
"channel:manage:ads",
|
|
20
|
+
"channel:manage:broadcast",
|
|
21
|
+
"channel:manage:raids",
|
|
22
|
+
"channel:read:ads",
|
|
23
|
+
"channel:read:redemptions",
|
|
24
|
+
"channel:read:subscriptions",
|
|
25
|
+
"clips:edit",
|
|
26
|
+
"moderator:manage:announcements",
|
|
27
|
+
"moderator:manage:chat_messages",
|
|
28
|
+
"moderator:manage:chat_settings",
|
|
29
|
+
"moderator:manage:shield_mode",
|
|
30
|
+
"moderator:manage:shoutouts",
|
|
31
|
+
"moderator:read:followers",
|
|
32
|
+
"user:read:chat",
|
|
33
|
+
"user:write:chat"
|
|
34
|
+
];
|
|
35
|
+
var TWITCH = {
|
|
36
|
+
id: "https://id.twitch.tv/oauth2",
|
|
37
|
+
api: "https://api.twitch.tv/helix",
|
|
38
|
+
events: "wss://eventsub.wss.twitch.tv/ws"
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
class SignedOut extends Error {
|
|
42
|
+
constructor(message = "Sign in to Twitch from the plugin's settings") {
|
|
43
|
+
super(message);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class TwitchError extends Error {
|
|
48
|
+
status;
|
|
49
|
+
constructor(status, message) {
|
|
50
|
+
super(message);
|
|
51
|
+
this.status = status;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
var VALIDATE_MS = 3600000;
|
|
55
|
+
async function read(response) {
|
|
56
|
+
const body = await response.json().catch(() => ({}));
|
|
57
|
+
return { status: response.status, body: body ?? {} };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
class TwitchClient {
|
|
61
|
+
#options;
|
|
62
|
+
#endpoints;
|
|
63
|
+
#session;
|
|
64
|
+
#starting;
|
|
65
|
+
#era = 0;
|
|
66
|
+
constructor(options) {
|
|
67
|
+
this.#options = options;
|
|
68
|
+
this.#endpoints = options.endpoints ?? TWITCH;
|
|
69
|
+
}
|
|
70
|
+
get signedIn() {
|
|
71
|
+
return this.#options.tokens.get() !== undefined;
|
|
72
|
+
}
|
|
73
|
+
get eventsUrl() {
|
|
74
|
+
return this.#endpoints.events;
|
|
75
|
+
}
|
|
76
|
+
async signIn() {
|
|
77
|
+
const { status, body } = await this.#post("device", { scopes: SCOPES.join(" ") });
|
|
78
|
+
const url = String(body.verification_uri ?? "");
|
|
79
|
+
const device = String(body.device_code ?? "");
|
|
80
|
+
if (status !== 200 || !url || !device) {
|
|
81
|
+
throw new Error(/client/i.test(String(body.message)) ? "Twitch does not accept this client id; check it, and that the app's client type is Public" : `Twitch gave no code: ${body.message ?? status}`);
|
|
82
|
+
}
|
|
83
|
+
const stop = new AbortController;
|
|
84
|
+
const end = Date.now() + Number(body.expires_in ?? 1800) * 1000;
|
|
85
|
+
const everyMs = Number(body.interval ?? 5) * 1000;
|
|
86
|
+
const wait = async () => {
|
|
87
|
+
for (;; ) {
|
|
88
|
+
await new Promise((resolve) => setTimeout(resolve, everyMs));
|
|
89
|
+
if (stop.signal.aborted)
|
|
90
|
+
throw new Error("sign-in cancelled");
|
|
91
|
+
if (Date.now() > end)
|
|
92
|
+
throw new Error("the sign-in took too long; start again");
|
|
93
|
+
const answer = await this.#post("token", {
|
|
94
|
+
scopes: SCOPES.join(" "),
|
|
95
|
+
device_code: device,
|
|
96
|
+
grant_type: "urn:ietf:params:oauth:grant-type:device_code"
|
|
97
|
+
}).catch(() => {
|
|
98
|
+
return;
|
|
99
|
+
});
|
|
100
|
+
if (stop.signal.aborted)
|
|
101
|
+
throw new Error("sign-in cancelled");
|
|
102
|
+
if (!answer || answer.status >= 500)
|
|
103
|
+
continue;
|
|
104
|
+
if (answer.body.message === "authorization_pending")
|
|
105
|
+
continue;
|
|
106
|
+
if (answer.status !== 200)
|
|
107
|
+
throw new Error(`Twitch said: ${answer.body.message}`);
|
|
108
|
+
this.#forget();
|
|
109
|
+
await this.#keep(answer.body, this.#era);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
return {
|
|
114
|
+
url,
|
|
115
|
+
code: String(body.user_code ?? ""),
|
|
116
|
+
done: wait(),
|
|
117
|
+
cancel: () => stop.abort()
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
async signOut() {
|
|
121
|
+
const token = this.#session?.token;
|
|
122
|
+
this.#forget();
|
|
123
|
+
await this.#options.tokens.set(undefined);
|
|
124
|
+
if (token)
|
|
125
|
+
await this.#post("revoke", { token }).catch(() => {});
|
|
126
|
+
}
|
|
127
|
+
async user() {
|
|
128
|
+
return (await this.#active()).user;
|
|
129
|
+
}
|
|
130
|
+
async request(method, path, query = {}, body) {
|
|
131
|
+
return (await this.call(method, path, query, body)).data;
|
|
132
|
+
}
|
|
133
|
+
async call(method, path, query = {}, body) {
|
|
134
|
+
const url = new URL(`${this.#endpoints.api}${path}`);
|
|
135
|
+
for (const [key, value] of Object.entries(query))
|
|
136
|
+
url.searchParams.set(key, value);
|
|
137
|
+
for (let attempt = 0;; attempt++) {
|
|
138
|
+
const { token } = await this.#active();
|
|
139
|
+
const response = await fetch(url, {
|
|
140
|
+
method,
|
|
141
|
+
headers: {
|
|
142
|
+
authorization: `Bearer ${token}`,
|
|
143
|
+
"client-id": this.#options.clientId(),
|
|
144
|
+
...body !== undefined && { "content-type": "application/json" }
|
|
145
|
+
},
|
|
146
|
+
...body !== undefined && { body: JSON.stringify(body) },
|
|
147
|
+
signal: AbortSignal.timeout(1e4)
|
|
148
|
+
});
|
|
149
|
+
const answer = await read(response);
|
|
150
|
+
const message = String(answer.body.message ?? "");
|
|
151
|
+
if (response.ok) {
|
|
152
|
+
return { ...answer.body, data: Array.isArray(answer.body.data) ? answer.body.data : [] };
|
|
153
|
+
}
|
|
154
|
+
if (response.status === 401) {
|
|
155
|
+
if (/scope/i.test(message)) {
|
|
156
|
+
throw new TwitchError(401, "Sign in to Twitch again to allow this (it asks for more now)");
|
|
157
|
+
}
|
|
158
|
+
if (attempt === 0) {
|
|
159
|
+
if (this.#session?.token === token)
|
|
160
|
+
this.#session = undefined;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
throw new TwitchError(response.status, message || `Twitch answered ${response.status}`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
#forget() {
|
|
168
|
+
this.#era++;
|
|
169
|
+
this.#session = undefined;
|
|
170
|
+
this.#starting = undefined;
|
|
171
|
+
}
|
|
172
|
+
async#active() {
|
|
173
|
+
const now = Date.now();
|
|
174
|
+
const session = this.#session;
|
|
175
|
+
if (session && now < session.expires && now < session.validated + VALIDATE_MS)
|
|
176
|
+
return session;
|
|
177
|
+
if (!this.#starting) {
|
|
178
|
+
const starting = this.#renew(session).finally(() => {
|
|
179
|
+
if (this.#starting === starting)
|
|
180
|
+
this.#starting = undefined;
|
|
181
|
+
});
|
|
182
|
+
this.#starting = starting;
|
|
183
|
+
}
|
|
184
|
+
return this.#starting;
|
|
185
|
+
}
|
|
186
|
+
async#renew(session) {
|
|
187
|
+
const era = this.#era;
|
|
188
|
+
if (session && Date.now() < session.expires && await this.#validate(session.token)) {
|
|
189
|
+
session.validated = Date.now();
|
|
190
|
+
return session;
|
|
191
|
+
}
|
|
192
|
+
const refresh = this.#options.tokens.get();
|
|
193
|
+
if (!refresh)
|
|
194
|
+
throw new SignedOut;
|
|
195
|
+
const { status, body } = await this.#post("token", {
|
|
196
|
+
grant_type: "refresh_token",
|
|
197
|
+
refresh_token: refresh
|
|
198
|
+
});
|
|
199
|
+
if (status === 200)
|
|
200
|
+
return this.#keep(body, era);
|
|
201
|
+
if (status !== 400 && status !== 401) {
|
|
202
|
+
throw new TwitchError(status, `Twitch answered ${status} to renewing the sign-in`);
|
|
203
|
+
}
|
|
204
|
+
if (era === this.#era)
|
|
205
|
+
await this.signOut();
|
|
206
|
+
throw new SignedOut("The Twitch sign-in expired: sign in again");
|
|
207
|
+
}
|
|
208
|
+
async#keep(body, era) {
|
|
209
|
+
if (era !== this.#era)
|
|
210
|
+
throw new SignedOut;
|
|
211
|
+
const token = String(body.access_token);
|
|
212
|
+
await this.#options.tokens.set(String(body.refresh_token));
|
|
213
|
+
const user = await this.#validate(token);
|
|
214
|
+
if (!user)
|
|
215
|
+
throw new SignedOut("Twitch did not accept its own token: sign in again");
|
|
216
|
+
if (era !== this.#era)
|
|
217
|
+
throw new SignedOut;
|
|
218
|
+
const lifetime = Math.max(0, Number(body.expires_in ?? 14400) - 60);
|
|
219
|
+
const session = { token, expires: Date.now() + lifetime * 1000, validated: Date.now(), user };
|
|
220
|
+
this.#session = session;
|
|
221
|
+
return session;
|
|
222
|
+
}
|
|
223
|
+
async#validate(token) {
|
|
224
|
+
const response = await fetch(`${this.#endpoints.id}/validate`, {
|
|
225
|
+
headers: { authorization: `OAuth ${token}` },
|
|
226
|
+
signal: AbortSignal.timeout(1e4)
|
|
227
|
+
});
|
|
228
|
+
const { status, body } = await read(response);
|
|
229
|
+
if (status === 401)
|
|
230
|
+
return;
|
|
231
|
+
if (status !== 200)
|
|
232
|
+
throw new TwitchError(status, `Twitch answered ${status}`);
|
|
233
|
+
return { id: String(body.user_id), login: String(body.login) };
|
|
234
|
+
}
|
|
235
|
+
async#post(path, fields) {
|
|
236
|
+
const clientId = this.#options.clientId();
|
|
237
|
+
if (!clientId)
|
|
238
|
+
throw new SignedOut("Enter the client id of your Twitch app first");
|
|
239
|
+
const response = await fetch(`${this.#endpoints.id}/${path}`, {
|
|
240
|
+
method: "POST",
|
|
241
|
+
body: new URLSearchParams({ client_id: clientId, ...fields }),
|
|
242
|
+
signal: AbortSignal.timeout(1e4)
|
|
243
|
+
});
|
|
244
|
+
return read(response);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// src/model.ts
|
|
249
|
+
var text = (value) => typeof value === "string" ? value : "";
|
|
250
|
+
var count = (value) => typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
251
|
+
function parseStream(raw) {
|
|
252
|
+
const startedAt = Date.parse(text(raw?.started_at)) || 0;
|
|
253
|
+
return { live: raw !== undefined, viewers: count(raw?.viewer_count), startedAt };
|
|
254
|
+
}
|
|
255
|
+
function parseChannel(raw) {
|
|
256
|
+
return {
|
|
257
|
+
title: text(raw?.title),
|
|
258
|
+
category: text(raw?.game_name),
|
|
259
|
+
categoryId: text(raw?.game_id)
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
var CHAT_MODES = {
|
|
263
|
+
emote_mode: "Emotes only",
|
|
264
|
+
subscriber_mode: "Subscribers only",
|
|
265
|
+
follower_mode: "Followers only",
|
|
266
|
+
slow_mode: "Slow mode",
|
|
267
|
+
unique_chat_mode: "No repeated messages"
|
|
268
|
+
};
|
|
269
|
+
function parseChat(raw) {
|
|
270
|
+
return {
|
|
271
|
+
emote_mode: raw?.emote_mode === true,
|
|
272
|
+
subscriber_mode: raw?.subscriber_mode === true,
|
|
273
|
+
follower_mode: raw?.follower_mode === true,
|
|
274
|
+
slow_mode: raw?.slow_mode === true,
|
|
275
|
+
unique_chat_mode: raw?.unique_chat_mode === true,
|
|
276
|
+
slowSeconds: count(raw?.slow_mode_wait_time) || 30,
|
|
277
|
+
followMinutes: count(raw?.follower_mode_duration)
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
function chatModeBody(mode, on, times) {
|
|
281
|
+
return {
|
|
282
|
+
[mode]: on,
|
|
283
|
+
...on && mode === "slow_mode" && { slow_mode_wait_time: times.slowSeconds },
|
|
284
|
+
...on && mode === "follower_mode" && { follower_mode_duration: times.followMinutes }
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
function parseTime(value) {
|
|
288
|
+
if (typeof value === "number")
|
|
289
|
+
return value * 1000;
|
|
290
|
+
if (typeof value !== "string" || value === "")
|
|
291
|
+
return 0;
|
|
292
|
+
return /^\d+$/.test(value) ? Number(value) * 1000 : Date.parse(value) || 0;
|
|
293
|
+
}
|
|
294
|
+
var parseAds = (raw) => ({
|
|
295
|
+
nextAt: parseTime(raw?.next_ad_at),
|
|
296
|
+
snoozes: count(raw?.snooze_count)
|
|
297
|
+
});
|
|
298
|
+
function formatDuration(seconds) {
|
|
299
|
+
const s = Math.max(0, Math.floor(seconds));
|
|
300
|
+
const two = (n) => String(n).padStart(2, "0");
|
|
301
|
+
const hours = Math.floor(s / 3600);
|
|
302
|
+
const minutes = Math.floor(s % 3600 / 60);
|
|
303
|
+
return hours > 0 ? `${hours}:${two(minutes)}:${two(s % 60)}` : `${minutes}:${two(s % 60)}`;
|
|
304
|
+
}
|
|
305
|
+
function channelLogin(input) {
|
|
306
|
+
const name = input.trim().replace(/^(?:https?:\/\/)?(?:www\.|m\.)?twitch\.tv\//i, "").replace(/^@/, "").replace(/[/?#].*$/, "");
|
|
307
|
+
return /^\w{2,25}$/.test(name) ? name.toLowerCase() : undefined;
|
|
308
|
+
}
|
|
309
|
+
var RANKS = ["everyone", "subscriber", "vip", "moderator", "broadcaster"];
|
|
310
|
+
function hasRole(badges, role) {
|
|
311
|
+
const ranks = badges.map((badge) => RANKS.indexOf(badge === "founder" ? "subscriber" : badge));
|
|
312
|
+
return Math.max(0, ...ranks) >= RANKS.indexOf(role);
|
|
313
|
+
}
|
|
314
|
+
function commandWords(message, command) {
|
|
315
|
+
const wanted = command.trim().toLowerCase();
|
|
316
|
+
const said = message.trim();
|
|
317
|
+
if (!wanted || !said.toLowerCase().startsWith(wanted))
|
|
318
|
+
return;
|
|
319
|
+
const rest = said.slice(wanted.length);
|
|
320
|
+
return rest === "" || /^\s/.test(rest) ? rest.trim() : undefined;
|
|
321
|
+
}
|
|
322
|
+
var tier = (value) => Math.max(1, Math.round(Number(value) / 1000) || 1);
|
|
323
|
+
var person = (data) => ({
|
|
324
|
+
user: text(data.user_name) || "Anonymous",
|
|
325
|
+
login: text(data.user_login)
|
|
326
|
+
});
|
|
327
|
+
var EVENTS = {
|
|
328
|
+
"stream.online": () => ({}),
|
|
329
|
+
"stream.offline": () => ({}),
|
|
330
|
+
"channel.follow": person,
|
|
331
|
+
"channel.subscribe": (d) => ({
|
|
332
|
+
...person(d),
|
|
333
|
+
tier: tier(d.tier),
|
|
334
|
+
gift: d.is_gift === true,
|
|
335
|
+
months: 1,
|
|
336
|
+
message: ""
|
|
337
|
+
}),
|
|
338
|
+
"channel.subscription.message": (d) => ({
|
|
339
|
+
...person(d),
|
|
340
|
+
tier: tier(d.tier),
|
|
341
|
+
gift: false,
|
|
342
|
+
months: count(d.cumulative_months),
|
|
343
|
+
message: text(d.message?.text)
|
|
344
|
+
}),
|
|
345
|
+
"channel.subscription.gift": (d) => ({
|
|
346
|
+
...person(d),
|
|
347
|
+
tier: tier(d.tier),
|
|
348
|
+
total: count(d.total)
|
|
349
|
+
}),
|
|
350
|
+
"channel.cheer": (d) => ({
|
|
351
|
+
...person(d),
|
|
352
|
+
bits: count(d.bits),
|
|
353
|
+
message: text(d.message)
|
|
354
|
+
}),
|
|
355
|
+
"channel.raid": (d) => ({
|
|
356
|
+
user: text(d.from_broadcaster_user_name),
|
|
357
|
+
login: text(d.from_broadcaster_user_login),
|
|
358
|
+
viewers: count(d.viewers)
|
|
359
|
+
}),
|
|
360
|
+
"channel.channel_points_custom_reward_redemption.add": (d) => ({
|
|
361
|
+
...person(d),
|
|
362
|
+
reward: text(d.reward?.title),
|
|
363
|
+
cost: count(d.reward?.cost),
|
|
364
|
+
input: text(d.user_input)
|
|
365
|
+
}),
|
|
366
|
+
"channel.ad_break.begin": (d) => ({
|
|
367
|
+
seconds: count(Number(d.duration_seconds)),
|
|
368
|
+
automatic: String(d.is_automatic) === "true"
|
|
369
|
+
}),
|
|
370
|
+
"channel.chat.message": (d) => ({
|
|
371
|
+
user: text(d.chatter_user_name),
|
|
372
|
+
login: text(d.chatter_user_login),
|
|
373
|
+
message: text(d.message?.text),
|
|
374
|
+
badges: (Array.isArray(d.badges) ? d.badges : []).map((badge) => text(badge?.set_id))
|
|
375
|
+
})
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
// src/plugin.ts
|
|
379
|
+
import { createPlugin, ManagedConnection, Permissions, Shared, setting } from "@sdeck/plugin";
|
|
380
|
+
import { z } from "zod";
|
|
381
|
+
|
|
382
|
+
// src/events.ts
|
|
383
|
+
import { PermanentError } from "@sdeck/plugin";
|
|
384
|
+
function subscriptions({ id }) {
|
|
385
|
+
const own = { broadcaster_user_id: id };
|
|
386
|
+
return [
|
|
387
|
+
{ type: "stream.online", version: "1", condition: own },
|
|
388
|
+
{ type: "stream.offline", version: "1", condition: own },
|
|
389
|
+
{ type: "channel.follow", version: "2", condition: { ...own, moderator_user_id: id } },
|
|
390
|
+
{ type: "channel.subscribe", version: "1", condition: own },
|
|
391
|
+
{ type: "channel.subscription.message", version: "1", condition: own },
|
|
392
|
+
{ type: "channel.subscription.gift", version: "1", condition: own },
|
|
393
|
+
{ type: "channel.cheer", version: "1", condition: own },
|
|
394
|
+
{ type: "channel.raid", version: "1", condition: { to_broadcaster_user_id: id } },
|
|
395
|
+
{
|
|
396
|
+
type: "channel.channel_points_custom_reward_redemption.add",
|
|
397
|
+
version: "1",
|
|
398
|
+
condition: own
|
|
399
|
+
},
|
|
400
|
+
{ type: "channel.ad_break.begin", version: "1", condition: own },
|
|
401
|
+
{ type: "channel.chat.message", version: "1", condition: { ...own, user_id: id } }
|
|
402
|
+
];
|
|
403
|
+
}
|
|
404
|
+
var GRACE_MS = 5000;
|
|
405
|
+
var WELCOME_MS = 1e4;
|
|
406
|
+
var REMEMBERED = 200;
|
|
407
|
+
async function connectEvents(client, onEvent, lost, signal, log = () => {}) {
|
|
408
|
+
const user = await client.user().catch((error) => {
|
|
409
|
+
throw error instanceof SignedOut ? new PermanentError(error.message) : error;
|
|
410
|
+
});
|
|
411
|
+
const seen = new Set;
|
|
412
|
+
let current;
|
|
413
|
+
let closed = false;
|
|
414
|
+
let silence;
|
|
415
|
+
const close = () => {
|
|
416
|
+
closed = true;
|
|
417
|
+
clearTimeout(silence);
|
|
418
|
+
current?.close();
|
|
419
|
+
};
|
|
420
|
+
const fail = (error) => {
|
|
421
|
+
if (closed)
|
|
422
|
+
return;
|
|
423
|
+
close();
|
|
424
|
+
lost(error);
|
|
425
|
+
};
|
|
426
|
+
const open = (url) => new Promise((resolve, reject) => {
|
|
427
|
+
const socket = new WebSocket(url);
|
|
428
|
+
let keepaliveMs = 1e4;
|
|
429
|
+
const timer = setTimeout(() => {
|
|
430
|
+
reject(new Error("Twitch did not answer"));
|
|
431
|
+
socket.close();
|
|
432
|
+
}, WELCOME_MS);
|
|
433
|
+
const heard = () => {
|
|
434
|
+
if (socket !== current)
|
|
435
|
+
return;
|
|
436
|
+
clearTimeout(silence);
|
|
437
|
+
silence = setTimeout(() => fail(new Error("the connection to Twitch went quiet")), keepaliveMs + GRACE_MS);
|
|
438
|
+
};
|
|
439
|
+
socket.onmessage = ({ data }) => {
|
|
440
|
+
if (closed)
|
|
441
|
+
return;
|
|
442
|
+
let message;
|
|
443
|
+
try {
|
|
444
|
+
message = JSON.parse(String(data));
|
|
445
|
+
} catch {
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
const { metadata = {}, payload = {} } = message;
|
|
449
|
+
if (metadata.message_type === "session_welcome") {
|
|
450
|
+
clearTimeout(timer);
|
|
451
|
+
keepaliveMs = (payload.session?.keepalive_timeout_seconds ?? 10) * 1000;
|
|
452
|
+
const before = current;
|
|
453
|
+
current = socket;
|
|
454
|
+
before?.close();
|
|
455
|
+
heard();
|
|
456
|
+
resolve(String(payload.session?.id));
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
heard();
|
|
460
|
+
const id = metadata.message_id;
|
|
461
|
+
if (id) {
|
|
462
|
+
if (seen.has(id))
|
|
463
|
+
return;
|
|
464
|
+
seen.add(id);
|
|
465
|
+
if (seen.size > REMEMBERED)
|
|
466
|
+
seen.delete(seen.values().next().value);
|
|
467
|
+
}
|
|
468
|
+
const type = metadata.subscription_type ?? payload.subscription?.type ?? "";
|
|
469
|
+
if (metadata.message_type === "notification") {
|
|
470
|
+
onEvent({ type, data: payload.event ?? {} });
|
|
471
|
+
} else if (metadata.message_type === "session_reconnect") {
|
|
472
|
+
const next = payload.session?.reconnect_url;
|
|
473
|
+
if (next)
|
|
474
|
+
open(next).catch(fail);
|
|
475
|
+
} else if (metadata.message_type === "revocation") {
|
|
476
|
+
const status = payload.subscription?.status;
|
|
477
|
+
if (status === "authorization_revoked") {
|
|
478
|
+
fail(new PermanentError("Twitch access was taken away: sign in again"));
|
|
479
|
+
} else
|
|
480
|
+
log(`Twitch ended ${type}: ${status}`);
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
socket.onclose = ({ code }) => {
|
|
484
|
+
clearTimeout(timer);
|
|
485
|
+
reject(new Error("cannot reach Twitch"));
|
|
486
|
+
if (socket === current)
|
|
487
|
+
fail(new Error(`Twitch closed the connection (${code})`));
|
|
488
|
+
};
|
|
489
|
+
socket.onerror = () => {};
|
|
490
|
+
if (signal.aborted)
|
|
491
|
+
socket.close();
|
|
492
|
+
else
|
|
493
|
+
signal.addEventListener("abort", () => socket.close(), { once: true });
|
|
494
|
+
});
|
|
495
|
+
const session = await open(client.eventsUrl);
|
|
496
|
+
const wanted = subscriptions(user);
|
|
497
|
+
const transport = { method: "websocket", session_id: session };
|
|
498
|
+
const refused = [];
|
|
499
|
+
await Promise.all(wanted.map((subscription) => client.request("POST", "/eventsub/subscriptions", {}, { ...subscription, transport }).catch((error) => refused.push(`${subscription.type}: ${error.message}`))));
|
|
500
|
+
if (refused.length === wanted.length) {
|
|
501
|
+
close();
|
|
502
|
+
throw new Error(`Twitch accepted no subscription (${refused[0]})`);
|
|
503
|
+
}
|
|
504
|
+
if (refused.length > 0)
|
|
505
|
+
log("some Twitch events are not available:", refused.join("; "));
|
|
506
|
+
return { value: user, close };
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// src/plugin.ts
|
|
510
|
+
var plugin = createPlugin({
|
|
511
|
+
id: "@sdeck/twitch",
|
|
512
|
+
name: "Twitch",
|
|
513
|
+
version: "1.0.0",
|
|
514
|
+
description: "Run your Twitch channel: chat modes, clips, markers, ads, raids and more, with live values and triggers.",
|
|
515
|
+
permissions: [Permissions.network],
|
|
516
|
+
settings: {
|
|
517
|
+
clientId: setting.text("Client ID", {
|
|
518
|
+
description: "Of your own app in the Twitch developer console, with client type Public.",
|
|
519
|
+
required: true,
|
|
520
|
+
validate: z.string().trim().regex(/^[a-z0-9]{20,40}$/, "should be about 30 small letters and digits")
|
|
521
|
+
})
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
var TOKEN = "refreshToken";
|
|
525
|
+
var twitch = new TwitchClient({
|
|
526
|
+
clientId: () => plugin.context?.settings().clientId.trim() ?? "",
|
|
527
|
+
tokens: {
|
|
528
|
+
get: () => plugin.context?.secrets.get(TOKEN),
|
|
529
|
+
set: async (token) => {
|
|
530
|
+
await plugin.context?.secrets.set(TOKEN, token);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
var refreshers = new Set;
|
|
535
|
+
function polled(everyMs, readNow) {
|
|
536
|
+
let again = () => {};
|
|
537
|
+
const shared = new Shared((emit) => {
|
|
538
|
+
let stopped = false;
|
|
539
|
+
let timer;
|
|
540
|
+
let latest = 0;
|
|
541
|
+
const tick = async () => {
|
|
542
|
+
clearTimeout(timer);
|
|
543
|
+
const mine = ++latest;
|
|
544
|
+
try {
|
|
545
|
+
const value = await readNow(await twitch.user());
|
|
546
|
+
if (stopped || mine !== latest)
|
|
547
|
+
return;
|
|
548
|
+
emit(value);
|
|
549
|
+
} catch (error) {
|
|
550
|
+
if (stopped || mine !== latest)
|
|
551
|
+
return;
|
|
552
|
+
emit(undefined);
|
|
553
|
+
const { message } = error;
|
|
554
|
+
plugin.context?.status(message, error instanceof SignedOut ? "info" : "error");
|
|
555
|
+
}
|
|
556
|
+
timer = setTimeout(tick, everyMs);
|
|
557
|
+
};
|
|
558
|
+
again = () => void tick();
|
|
559
|
+
const mine = again;
|
|
560
|
+
refreshers.add(mine);
|
|
561
|
+
mine();
|
|
562
|
+
return () => {
|
|
563
|
+
stopped = true;
|
|
564
|
+
clearTimeout(timer);
|
|
565
|
+
refreshers.delete(mine);
|
|
566
|
+
again = () => {};
|
|
567
|
+
};
|
|
568
|
+
});
|
|
569
|
+
return { shared, refresh: () => again() };
|
|
570
|
+
}
|
|
571
|
+
var own = (user) => ({ broadcaster_id: user.id });
|
|
572
|
+
var mod = (user) => ({ ...own(user), moderator_id: user.id });
|
|
573
|
+
var readChannel = async (user) => parseChannel((await twitch.request("GET", "/channels", own(user)))[0]);
|
|
574
|
+
var readChat = async (user) => parseChat((await twitch.request("GET", "/chat/settings", mod(user)))[0]);
|
|
575
|
+
var readShield = async (user) => (await twitch.request("GET", "/moderation/shield_mode", mod(user)))[0]?.is_active === true;
|
|
576
|
+
var stream = polled(20000, async (user) => parseStream((await twitch.request("GET", "/streams", { user_id: user.id }))[0]));
|
|
577
|
+
var channel = polled(60000, readChannel);
|
|
578
|
+
var chat = polled(20000, readChat);
|
|
579
|
+
var shield = polled(20000, readShield);
|
|
580
|
+
var ads = polled(30000, async (user) => parseAds((await twitch.request("GET", "/channels/ads", own(user)))[0]));
|
|
581
|
+
var followers = polled(60000, async (user) => {
|
|
582
|
+
const { total } = await twitch.call("GET", "/channels/followers", { ...own(user), first: "1" });
|
|
583
|
+
return Number(total) || 0;
|
|
584
|
+
});
|
|
585
|
+
async function change(method, path, body) {
|
|
586
|
+
return twitch.request(method, path, mod(await twitch.user()), body);
|
|
587
|
+
}
|
|
588
|
+
var listeners = new Set;
|
|
589
|
+
var events = new ManagedConnection({
|
|
590
|
+
open: (lost, signal) => connectEvents(twitch, (event) => {
|
|
591
|
+
if (event.type.startsWith("stream."))
|
|
592
|
+
stream.refresh();
|
|
593
|
+
if (event.type === "channel.follow")
|
|
594
|
+
followers.refresh();
|
|
595
|
+
for (const listener of [...listeners])
|
|
596
|
+
listener(event);
|
|
597
|
+
}, lost, signal, (...args) => plugin.context?.log(...args)),
|
|
598
|
+
onStatus: (state, error) => {
|
|
599
|
+
if (state === "connected")
|
|
600
|
+
plugin.context?.status("Signed in to Twitch", "ok");
|
|
601
|
+
else if (error)
|
|
602
|
+
plugin.context?.status(error.message, twitch.signedIn ? "error" : "info");
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
function onEvent(listener) {
|
|
606
|
+
listeners.add(listener);
|
|
607
|
+
const release = events.hold();
|
|
608
|
+
return () => {
|
|
609
|
+
listeners.delete(listener);
|
|
610
|
+
release();
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
var signingIn;
|
|
614
|
+
function startOver() {
|
|
615
|
+
events.restart();
|
|
616
|
+
for (const refresh of [...refreshers])
|
|
617
|
+
refresh();
|
|
618
|
+
}
|
|
619
|
+
var signIn = plugin.command({
|
|
620
|
+
id: "sign-in",
|
|
621
|
+
name: "Sign in",
|
|
622
|
+
description: "Opens Twitch in your browser to allow access.",
|
|
623
|
+
run: async (context) => {
|
|
624
|
+
signingIn?.cancel();
|
|
625
|
+
const started = await twitch.signIn();
|
|
626
|
+
signingIn = started;
|
|
627
|
+
context.status(`Allow access in the browser (code ${started.code})`, "info");
|
|
628
|
+
started.done.then(() => {
|
|
629
|
+
context.status("Signed in to Twitch", "ok");
|
|
630
|
+
startOver();
|
|
631
|
+
}, (error) => {
|
|
632
|
+
if (signingIn === started)
|
|
633
|
+
context.status(error.message, "error");
|
|
634
|
+
});
|
|
635
|
+
return started.url;
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
var signOut = plugin.command({
|
|
639
|
+
id: "sign-out",
|
|
640
|
+
name: "Sign out",
|
|
641
|
+
run: async (context) => {
|
|
642
|
+
signingIn?.cancel();
|
|
643
|
+
signingIn = undefined;
|
|
644
|
+
await twitch.signOut();
|
|
645
|
+
startOver();
|
|
646
|
+
context.status("Signed out", "info");
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
var isSignedIn = () => twitch.signedIn;
|
|
650
|
+
function stopAll() {
|
|
651
|
+
signingIn?.cancel();
|
|
652
|
+
signingIn = undefined;
|
|
653
|
+
events.close();
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
// src/state.ts
|
|
657
|
+
import { following } from "@sdeck/plugin";
|
|
658
|
+
var ticking = (shared, pick) => (set) => {
|
|
659
|
+
const update = () => set(shared.last === undefined ? undefined : pick(shared.last));
|
|
660
|
+
const stop = shared.listen(update);
|
|
661
|
+
update();
|
|
662
|
+
const timer = setInterval(update, 1000);
|
|
663
|
+
return () => {
|
|
664
|
+
stop();
|
|
665
|
+
clearInterval(timer);
|
|
666
|
+
};
|
|
667
|
+
};
|
|
668
|
+
var { boolean, number, string } = plugin.state;
|
|
669
|
+
var chatMode = (id, name, mode) => boolean(id, name, { start: following(chat.shared, (c) => c[mode]) });
|
|
670
|
+
var live = boolean("live", "Live", { start: following(stream.shared, (s) => s.live) });
|
|
671
|
+
var viewers = number("viewers", "Viewers", {
|
|
672
|
+
start: following(stream.shared, (s) => s.viewers)
|
|
673
|
+
});
|
|
674
|
+
var uptime = string("uptime", "Time live", {
|
|
675
|
+
description: "Such as 1:02:05; empty while offline.",
|
|
676
|
+
start: ticking(stream.shared, (s) => s.live ? formatDuration((Date.now() - s.startedAt) / 1000) : "")
|
|
677
|
+
});
|
|
678
|
+
var title = string("title", "Stream title", {
|
|
679
|
+
start: following(channel.shared, (c) => c.title)
|
|
680
|
+
});
|
|
681
|
+
var category = string("category", "Category", {
|
|
682
|
+
start: following(channel.shared, (c) => c.category)
|
|
683
|
+
});
|
|
684
|
+
var followerCount = number("followers", "Followers", {
|
|
685
|
+
start: following(followers.shared, (n) => n)
|
|
686
|
+
});
|
|
687
|
+
var nextAd = string("nextAd", "Time to the next ad", {
|
|
688
|
+
description: "Such as 4:30; empty when none is planned.",
|
|
689
|
+
start: ticking(ads.shared, (a) => a.nextAt > Date.now() ? formatDuration((a.nextAt - Date.now()) / 1000) : "")
|
|
690
|
+
});
|
|
691
|
+
var snoozes = number("snoozes", "Ad snoozes left", {
|
|
692
|
+
start: following(ads.shared, (a) => a.snoozes)
|
|
693
|
+
});
|
|
694
|
+
var shieldOn = boolean("shield", "Shield Mode", {
|
|
695
|
+
start: following(shield.shared, (on) => on)
|
|
696
|
+
});
|
|
697
|
+
var chatModes = {
|
|
698
|
+
emote_mode: chatMode("emoteOnly", "Chat: emotes only", "emote_mode"),
|
|
699
|
+
subscriber_mode: chatMode("subscribersOnly", "Chat: subscribers only", "subscriber_mode"),
|
|
700
|
+
follower_mode: chatMode("followersOnly", "Chat: followers only", "follower_mode"),
|
|
701
|
+
slow_mode: chatMode("slowMode", "Chat: slow mode", "slow_mode"),
|
|
702
|
+
unique_chat_mode: chatMode("uniqueChat", "Chat: no repeated messages", "unique_chat_mode")
|
|
703
|
+
};
|
|
704
|
+
var state = [
|
|
705
|
+
live,
|
|
706
|
+
viewers,
|
|
707
|
+
uptime,
|
|
708
|
+
title,
|
|
709
|
+
category,
|
|
710
|
+
followerCount,
|
|
711
|
+
nextAd,
|
|
712
|
+
snoozes,
|
|
713
|
+
shieldOn,
|
|
714
|
+
...Object.values(chatModes)
|
|
715
|
+
];
|
|
716
|
+
|
|
717
|
+
// src/actions.ts
|
|
718
|
+
var category2 = "Twitch";
|
|
719
|
+
var slots = ["key", "sensor"];
|
|
720
|
+
var BACKGROUND = "#18181b";
|
|
721
|
+
var PURPLE = "#9146ff";
|
|
722
|
+
var LILAC = "#bf94ff";
|
|
723
|
+
var RED = "#eb0400";
|
|
724
|
+
var MUTED = "#adadb8";
|
|
725
|
+
var key = (icon, caption = "") => iconLook({ icon, caption, background: BACKGROUND, color: LILAC });
|
|
726
|
+
var operation = setting2.select("When pressed", {
|
|
727
|
+
toggle: "Turn on or off",
|
|
728
|
+
on: "Turn on",
|
|
729
|
+
off: "Turn off"
|
|
730
|
+
});
|
|
731
|
+
async function flip(name, wanted, before, set, undo = () => set(before)) {
|
|
732
|
+
const on = wanted === "toggle" ? !before : wanted === "on";
|
|
733
|
+
const outcome = { result: on, label: `${name} ${on ? "on" : "off"}` };
|
|
734
|
+
if (on === before)
|
|
735
|
+
return outcome;
|
|
736
|
+
await set(on);
|
|
737
|
+
return { ...outcome, undo };
|
|
738
|
+
}
|
|
739
|
+
var MODE_ICONS = {
|
|
740
|
+
emote_mode: "lucide/smile",
|
|
741
|
+
subscriber_mode: "lucide/star",
|
|
742
|
+
follower_mode: "lucide/heart",
|
|
743
|
+
slow_mode: "lucide/snail",
|
|
744
|
+
unique_chat_mode: "lucide/fingerprint"
|
|
745
|
+
};
|
|
746
|
+
var chatMode2 = plugin.action({
|
|
747
|
+
id: "chat-mode",
|
|
748
|
+
name: "Chat mode",
|
|
749
|
+
description: "Emotes, subscribers or followers only, slow mode, or no repeated messages.",
|
|
750
|
+
category: category2,
|
|
751
|
+
icon: "messages-square",
|
|
752
|
+
slots,
|
|
753
|
+
settings: {
|
|
754
|
+
mode: setting2.select("Mode", CHAT_MODES),
|
|
755
|
+
seconds: setting2.number("Seconds between messages", {
|
|
756
|
+
default: 30,
|
|
757
|
+
min: 3,
|
|
758
|
+
max: 120,
|
|
759
|
+
visibleIf: { mode: "slow_mode" }
|
|
760
|
+
}),
|
|
761
|
+
minutes: setting2.number("Minutes of following first", {
|
|
762
|
+
default: 0,
|
|
763
|
+
min: 0,
|
|
764
|
+
max: 129600,
|
|
765
|
+
visibleIf: { mode: "follower_mode" }
|
|
766
|
+
}),
|
|
767
|
+
operation
|
|
768
|
+
},
|
|
769
|
+
run: async ({ settings }) => {
|
|
770
|
+
const { mode } = settings;
|
|
771
|
+
const before = await readChat(await twitch.user());
|
|
772
|
+
const set = async (on, slowSeconds, followMinutes) => {
|
|
773
|
+
const body = chatModeBody(mode, on, { slowSeconds, followMinutes });
|
|
774
|
+
await change("PATCH", "/chat/settings", body);
|
|
775
|
+
chat.refresh();
|
|
776
|
+
};
|
|
777
|
+
return flip(CHAT_MODES[mode], settings.operation, before[mode], (on) => set(on, settings.seconds, settings.minutes), () => set(before[mode], before.slowSeconds, before.followMinutes));
|
|
778
|
+
},
|
|
779
|
+
look: (s) => switchLook({
|
|
780
|
+
icon: MODE_ICONS[s.mode],
|
|
781
|
+
on: chatModes[s.mode].key,
|
|
782
|
+
color: PURPLE,
|
|
783
|
+
background: BACKGROUND
|
|
784
|
+
})
|
|
785
|
+
});
|
|
786
|
+
var shieldMode = plugin.action({
|
|
787
|
+
id: "shield",
|
|
788
|
+
name: "Shield Mode",
|
|
789
|
+
description: "Twitch's panic switch against harassment, with the limits you set up there.",
|
|
790
|
+
category: category2,
|
|
791
|
+
icon: "shield",
|
|
792
|
+
slots,
|
|
793
|
+
settings: { operation },
|
|
794
|
+
run: async ({ settings }) => {
|
|
795
|
+
const before = await readShield(await twitch.user());
|
|
796
|
+
return flip("Shield Mode", settings.operation, before, async (on) => {
|
|
797
|
+
await change("PUT", "/moderation/shield_mode", { is_active: on });
|
|
798
|
+
shield.refresh();
|
|
799
|
+
});
|
|
800
|
+
},
|
|
801
|
+
look: () => switchLook({
|
|
802
|
+
icon: "lucide/shield",
|
|
803
|
+
on: shieldOn.key,
|
|
804
|
+
color: RED,
|
|
805
|
+
background: BACKGROUND
|
|
806
|
+
})
|
|
807
|
+
});
|
|
808
|
+
var atMost = (length) => (text) => [...text].length > length ? `cannot be longer than ${length} letters` : undefined;
|
|
809
|
+
var chatText = setting2.text("Message", {
|
|
810
|
+
multiline: true,
|
|
811
|
+
required: true,
|
|
812
|
+
validate: atMost(500)
|
|
813
|
+
});
|
|
814
|
+
var caption = setting2.text("Caption", { appearance: true });
|
|
815
|
+
var message = plugin.action({
|
|
816
|
+
id: "message",
|
|
817
|
+
name: "Send a chat message",
|
|
818
|
+
category: category2,
|
|
819
|
+
icon: "message-square",
|
|
820
|
+
slots,
|
|
821
|
+
settings: { text: chatText, caption },
|
|
822
|
+
run: async ({ settings }) => {
|
|
823
|
+
const { id } = await twitch.user();
|
|
824
|
+
const [sent] = await twitch.request("POST", "/chat/messages", {}, { broadcaster_id: id, sender_id: id, message: settings.text.trim() });
|
|
825
|
+
if (!sent?.is_sent)
|
|
826
|
+
throw new Error(sent?.drop_reason?.message ?? "Twitch did not send it");
|
|
827
|
+
return { label: "Chat message" };
|
|
828
|
+
},
|
|
829
|
+
look: (s) => key("lucide/message-square", s.caption)
|
|
830
|
+
});
|
|
831
|
+
var announcement = plugin.action({
|
|
832
|
+
id: "announcement",
|
|
833
|
+
name: "Announce in chat",
|
|
834
|
+
description: "A highlighted message.",
|
|
835
|
+
category: category2,
|
|
836
|
+
icon: "megaphone",
|
|
837
|
+
slots,
|
|
838
|
+
settings: {
|
|
839
|
+
text: chatText,
|
|
840
|
+
color: setting2.select("Colour", {
|
|
841
|
+
primary: "The channel's",
|
|
842
|
+
blue: "Blue",
|
|
843
|
+
green: "Green",
|
|
844
|
+
orange: "Orange",
|
|
845
|
+
purple: "Purple"
|
|
846
|
+
}),
|
|
847
|
+
caption
|
|
848
|
+
},
|
|
849
|
+
run: async ({ settings }) => {
|
|
850
|
+
const body = { message: settings.text.trim(), color: settings.color };
|
|
851
|
+
await change("POST", "/chat/announcements", body);
|
|
852
|
+
return { label: "Announcement" };
|
|
853
|
+
},
|
|
854
|
+
look: (s) => key("lucide/megaphone", s.caption)
|
|
855
|
+
});
|
|
856
|
+
var clearChat = plugin.action({
|
|
857
|
+
id: "clear-chat",
|
|
858
|
+
name: "Clear chat",
|
|
859
|
+
description: "Removes every message. This cannot be undone.",
|
|
860
|
+
category: category2,
|
|
861
|
+
icon: "eraser",
|
|
862
|
+
slots,
|
|
863
|
+
run: async () => {
|
|
864
|
+
await change("DELETE", "/moderation/chat");
|
|
865
|
+
return { label: "Clear chat" };
|
|
866
|
+
},
|
|
867
|
+
look: () => key("lucide/eraser")
|
|
868
|
+
});
|
|
869
|
+
async function whileLive(request) {
|
|
870
|
+
try {
|
|
871
|
+
return await request;
|
|
872
|
+
} catch (error) {
|
|
873
|
+
if (error instanceof TwitchError && error.status === 404) {
|
|
874
|
+
throw new Error("this only works while you are live");
|
|
875
|
+
}
|
|
876
|
+
throw error;
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
var clip = plugin.action({
|
|
880
|
+
id: "clip",
|
|
881
|
+
name: "Create a clip",
|
|
882
|
+
description: "Of the last moments of your stream. Its result is the link to edit it.",
|
|
883
|
+
category: category2,
|
|
884
|
+
icon: "clapperboard",
|
|
885
|
+
slots,
|
|
886
|
+
run: async () => {
|
|
887
|
+
const { id } = await twitch.user();
|
|
888
|
+
const [made] = await whileLive(twitch.request("POST", "/clips", { broadcaster_id: id }));
|
|
889
|
+
return { result: made?.edit_url, label: "Clip" };
|
|
890
|
+
},
|
|
891
|
+
look: () => key("lucide/clapperboard")
|
|
892
|
+
});
|
|
893
|
+
var marker = plugin.action({
|
|
894
|
+
id: "marker",
|
|
895
|
+
name: "Add a stream marker",
|
|
896
|
+
description: "Marks this moment, for editing the recording later.",
|
|
897
|
+
category: category2,
|
|
898
|
+
icon: "bookmark",
|
|
899
|
+
slots,
|
|
900
|
+
settings: {
|
|
901
|
+
text: setting2.text("Description", { placeholder: "Optional", validate: atMost(140) })
|
|
902
|
+
},
|
|
903
|
+
run: async ({ settings }) => {
|
|
904
|
+
const { id } = await twitch.user();
|
|
905
|
+
const description = settings.text.trim();
|
|
906
|
+
const body = { user_id: id, ...description && { description } };
|
|
907
|
+
const [made] = await whileLive(twitch.request("POST", "/streams/markers", {}, body));
|
|
908
|
+
return { result: made?.position_seconds, label: "Stream marker" };
|
|
909
|
+
},
|
|
910
|
+
look: () => key("lucide/bookmark")
|
|
911
|
+
});
|
|
912
|
+
var ad = plugin.action({
|
|
913
|
+
id: "ad",
|
|
914
|
+
name: "Play an ad",
|
|
915
|
+
category: category2,
|
|
916
|
+
icon: "badge-dollar-sign",
|
|
917
|
+
slots,
|
|
918
|
+
settings: {
|
|
919
|
+
length: setting2.select("Length", [
|
|
920
|
+
{ value: "30", label: "30 seconds" },
|
|
921
|
+
{ value: "60", label: "1 minute" },
|
|
922
|
+
{ value: "90", label: "1\xBD minutes" },
|
|
923
|
+
{ value: "120", label: "2 minutes" },
|
|
924
|
+
{ value: "150", label: "2\xBD minutes" },
|
|
925
|
+
{ value: "180", label: "3 minutes" }
|
|
926
|
+
])
|
|
927
|
+
},
|
|
928
|
+
run: async ({ settings }) => {
|
|
929
|
+
const { id } = await twitch.user();
|
|
930
|
+
const body = { broadcaster_id: id, length: Number(settings.length) };
|
|
931
|
+
await twitch.request("POST", "/channels/commercial", {}, body);
|
|
932
|
+
ads.refresh();
|
|
933
|
+
return { label: `Ad, ${settings.length} s` };
|
|
934
|
+
},
|
|
935
|
+
look: (s) => key("lucide/badge-dollar-sign", `${s.length} s`)
|
|
936
|
+
});
|
|
937
|
+
var snoozeAd = plugin.action({
|
|
938
|
+
id: "snooze-ad",
|
|
939
|
+
name: "Snooze the next ad",
|
|
940
|
+
description: "Puts it off by 5 minutes. Shows how long it is until the next one.",
|
|
941
|
+
category: category2,
|
|
942
|
+
icon: "alarm-clock",
|
|
943
|
+
slots,
|
|
944
|
+
run: async () => {
|
|
945
|
+
const { id } = await twitch.user();
|
|
946
|
+
await twitch.request("POST", "/channels/ads/schedule/snooze", { broadcaster_id: id });
|
|
947
|
+
ads.refresh();
|
|
948
|
+
return { label: "Snooze ad" };
|
|
949
|
+
},
|
|
950
|
+
look: () => key("lucide/alarm-clock", nextAd.template)
|
|
951
|
+
});
|
|
952
|
+
async function categoryId(name) {
|
|
953
|
+
const found = await twitch.request("GET", "/search/categories", {
|
|
954
|
+
query: name,
|
|
955
|
+
first: "50"
|
|
956
|
+
});
|
|
957
|
+
const exact = found.find((c) => c.name.toLowerCase() === name.toLowerCase());
|
|
958
|
+
if (exact)
|
|
959
|
+
return exact.id;
|
|
960
|
+
const near = found.slice(0, 3).map((c) => c.name);
|
|
961
|
+
const hint = near.length > 0 ? ` (did you mean ${near.join(", ")}?)` : "";
|
|
962
|
+
throw new Error(`Twitch has no category "${name}"${hint}`);
|
|
963
|
+
}
|
|
964
|
+
var update = plugin.action({
|
|
965
|
+
id: "update",
|
|
966
|
+
name: "Change title or category",
|
|
967
|
+
category: category2,
|
|
968
|
+
icon: "pencil",
|
|
969
|
+
slots,
|
|
970
|
+
settings: {
|
|
971
|
+
title: setting2.text("Title", {
|
|
972
|
+
placeholder: "Leave it as it is",
|
|
973
|
+
validate: atMost(140)
|
|
974
|
+
}),
|
|
975
|
+
category: setting2.text("Category", {
|
|
976
|
+
placeholder: "Leave it as it is",
|
|
977
|
+
description: "As Twitch names it, such as Just Chatting."
|
|
978
|
+
}),
|
|
979
|
+
caption
|
|
980
|
+
},
|
|
981
|
+
run: async ({ settings }) => {
|
|
982
|
+
const title = settings.title.trim();
|
|
983
|
+
const name = settings.category.trim();
|
|
984
|
+
if (!title && !name)
|
|
985
|
+
throw new Error("enter a title or a category");
|
|
986
|
+
const gameId = name && await categoryId(name);
|
|
987
|
+
const user = await twitch.user();
|
|
988
|
+
const before = await readChannel(user);
|
|
989
|
+
const set = async (to) => {
|
|
990
|
+
const body = { ...title && { title: to.title }, ...name && { game_id: to.categoryId } };
|
|
991
|
+
await twitch.request("PATCH", "/channels", { broadcaster_id: user.id }, body);
|
|
992
|
+
channel.refresh();
|
|
993
|
+
};
|
|
994
|
+
await set({ title, categoryId: gameId });
|
|
995
|
+
return { label: name || title, undo: () => set(before) };
|
|
996
|
+
},
|
|
997
|
+
look: (s) => key("lucide/pencil", s.caption || s.category)
|
|
998
|
+
});
|
|
999
|
+
async function find(input) {
|
|
1000
|
+
const login = channelLogin(input);
|
|
1001
|
+
if (!login)
|
|
1002
|
+
throw new Error(`"${input}" is not a channel's name`);
|
|
1003
|
+
const [user] = await twitch.request("GET", "/users", { login });
|
|
1004
|
+
if (!user)
|
|
1005
|
+
throw new Error(`there is no channel "${login}"`);
|
|
1006
|
+
return { id: user.id, login: user.login, name: user.display_name };
|
|
1007
|
+
}
|
|
1008
|
+
var otherChannel = (description) => setting2.text("Channel", {
|
|
1009
|
+
required: true,
|
|
1010
|
+
placeholder: "Name or link",
|
|
1011
|
+
description,
|
|
1012
|
+
validate: (text) => channelLogin(text) ? undefined : "should be a channel's name or link"
|
|
1013
|
+
});
|
|
1014
|
+
var shoutout = plugin.action({
|
|
1015
|
+
id: "shoutout",
|
|
1016
|
+
name: "Shout out a channel",
|
|
1017
|
+
category: category2,
|
|
1018
|
+
icon: "volume-2",
|
|
1019
|
+
slots,
|
|
1020
|
+
settings: { channel: otherChannel("In a workflow that a raid starts: {{event.login}}.") },
|
|
1021
|
+
run: async ({ settings }) => {
|
|
1022
|
+
const me = await twitch.user();
|
|
1023
|
+
const to = await find(settings.channel);
|
|
1024
|
+
await twitch.request("POST", "/chat/shoutouts", {
|
|
1025
|
+
from_broadcaster_id: me.id,
|
|
1026
|
+
to_broadcaster_id: to.id,
|
|
1027
|
+
moderator_id: me.id
|
|
1028
|
+
});
|
|
1029
|
+
return { label: `Shout out ${to.name}` };
|
|
1030
|
+
},
|
|
1031
|
+
look: (s) => key("lucide/volume-2", s.channel)
|
|
1032
|
+
});
|
|
1033
|
+
var raid = plugin.action({
|
|
1034
|
+
id: "raid",
|
|
1035
|
+
name: "Raid a channel",
|
|
1036
|
+
description: "Starts the countdown. Undoing it before it ends calls the raid off.",
|
|
1037
|
+
category: category2,
|
|
1038
|
+
icon: "swords",
|
|
1039
|
+
slots,
|
|
1040
|
+
settings: { channel: otherChannel("Where your viewers go.") },
|
|
1041
|
+
run: async ({ settings }) => {
|
|
1042
|
+
const me = await twitch.user();
|
|
1043
|
+
const to = await find(settings.channel);
|
|
1044
|
+
await twitch.request("POST", "/raids", {
|
|
1045
|
+
from_broadcaster_id: me.id,
|
|
1046
|
+
to_broadcaster_id: to.id
|
|
1047
|
+
});
|
|
1048
|
+
return {
|
|
1049
|
+
label: `Raid ${to.name}`,
|
|
1050
|
+
undo: () => twitch.request("DELETE", "/raids", { broadcaster_id: me.id })
|
|
1051
|
+
};
|
|
1052
|
+
},
|
|
1053
|
+
look: (s) => key("lucide/swords", s.channel)
|
|
1054
|
+
});
|
|
1055
|
+
var grouped = (path) => `${path} == null ? "" : fmt(${path}, 0, ",")`;
|
|
1056
|
+
var viewers2 = `{{${viewers.key} >= 100000 ? fmt(${viewers.key} / 1000, 1) + "K" : (${grouped(viewers.key)})}}`;
|
|
1057
|
+
function statusWindow() {
|
|
1058
|
+
const on = live.key;
|
|
1059
|
+
const off = `!${on}`;
|
|
1060
|
+
const pill = { x: 4, y: 14, width: 19, height: 36 };
|
|
1061
|
+
const followers = followerCount.key;
|
|
1062
|
+
return {
|
|
1063
|
+
background: surface(BACKGROUND),
|
|
1064
|
+
layers: [
|
|
1065
|
+
rectLayer("badge", { frame: pill, fill: RED, radius: 6, visible: on }),
|
|
1066
|
+
textLayer("live", "LIVE", { size: 13, frame: pill, visible: on }),
|
|
1067
|
+
iconLayer("eye", "lucide/eye", {
|
|
1068
|
+
color: LILAC,
|
|
1069
|
+
frame: { x: 27, y: 15, width: 9, height: 34 },
|
|
1070
|
+
visible: on
|
|
1071
|
+
}),
|
|
1072
|
+
textLayer("viewers", viewers2, {
|
|
1073
|
+
size: 21,
|
|
1074
|
+
align: "left",
|
|
1075
|
+
frame: { x: 37.5, y: 10, width: 32, height: 44 },
|
|
1076
|
+
visible: on
|
|
1077
|
+
}),
|
|
1078
|
+
textLayer("uptime", uptime.template, {
|
|
1079
|
+
size: 15,
|
|
1080
|
+
color: MUTED,
|
|
1081
|
+
align: "right",
|
|
1082
|
+
frame: { x: 69, y: 10, width: 27, height: 44 },
|
|
1083
|
+
visible: on
|
|
1084
|
+
}),
|
|
1085
|
+
textLayer("category", category.template, {
|
|
1086
|
+
size: 13,
|
|
1087
|
+
color: MUTED,
|
|
1088
|
+
align: "left",
|
|
1089
|
+
frame: { x: 4, y: 58, width: 92, height: 32 },
|
|
1090
|
+
visible: on
|
|
1091
|
+
}),
|
|
1092
|
+
iconLayer("logo", "si/SiTwitch", {
|
|
1093
|
+
color: LILAC,
|
|
1094
|
+
frame: { x: 21, y: 20, width: 15, height: 60 },
|
|
1095
|
+
visible: off
|
|
1096
|
+
}),
|
|
1097
|
+
textLayer("offline", "Offline", {
|
|
1098
|
+
size: 20,
|
|
1099
|
+
align: "left",
|
|
1100
|
+
frame: { x: 41, y: 12, width: 55, height: 44 },
|
|
1101
|
+
visible: `${off} && ${followers} != null`
|
|
1102
|
+
}),
|
|
1103
|
+
textLayer("offline-alone", "Offline", {
|
|
1104
|
+
size: 20,
|
|
1105
|
+
align: "left",
|
|
1106
|
+
frame: { x: 41, y: 20, width: 55, height: 60 },
|
|
1107
|
+
visible: `${off} && ${followers} == null`
|
|
1108
|
+
}),
|
|
1109
|
+
textLayer("followers", `{{${grouped(followers)}}} followers`, {
|
|
1110
|
+
size: 13,
|
|
1111
|
+
color: MUTED,
|
|
1112
|
+
align: "left",
|
|
1113
|
+
frame: { x: 41, y: 54, width: 55, height: 32 },
|
|
1114
|
+
visible: `${off} && ${followers} != null`
|
|
1115
|
+
})
|
|
1116
|
+
]
|
|
1117
|
+
};
|
|
1118
|
+
}
|
|
1119
|
+
var status = plugin.action({
|
|
1120
|
+
id: "status",
|
|
1121
|
+
name: "Stream status",
|
|
1122
|
+
description: "Whether you are live, with viewers and the time since it started.",
|
|
1123
|
+
category: category2,
|
|
1124
|
+
icon: "radio",
|
|
1125
|
+
slots: ["key", "window"],
|
|
1126
|
+
display: true,
|
|
1127
|
+
look: (_, slot) => {
|
|
1128
|
+
if (slot === "window")
|
|
1129
|
+
return statusWindow();
|
|
1130
|
+
const on = live.key;
|
|
1131
|
+
const off = `!${on}`;
|
|
1132
|
+
const pill = { x: 22, y: 13, width: 56, height: 22 };
|
|
1133
|
+
return {
|
|
1134
|
+
background: surface(BACKGROUND),
|
|
1135
|
+
layers: [
|
|
1136
|
+
rectLayer("badge", { frame: pill, fill: RED, radius: 6, visible: on }),
|
|
1137
|
+
textLayer("live", "LIVE", { size: 13, frame: pill, visible: on }),
|
|
1138
|
+
textLayer("viewers", viewers2, {
|
|
1139
|
+
size: 25,
|
|
1140
|
+
frame: { x: 0, y: 38, width: 100, height: 30 },
|
|
1141
|
+
visible: on
|
|
1142
|
+
}),
|
|
1143
|
+
textLayer("uptime", uptime.template, {
|
|
1144
|
+
size: 13,
|
|
1145
|
+
color: MUTED,
|
|
1146
|
+
frame: { x: 0, y: 69, width: 100, height: 20 },
|
|
1147
|
+
visible: on
|
|
1148
|
+
}),
|
|
1149
|
+
iconLayer("logo", "si/SiTwitch", { color: MUTED, size: iconSize(true), visible: off }),
|
|
1150
|
+
captionLayer("offline", "Offline", { color: MUTED, visible: off })
|
|
1151
|
+
]
|
|
1152
|
+
};
|
|
1153
|
+
}
|
|
1154
|
+
});
|
|
1155
|
+
var followers2 = plugin.action({
|
|
1156
|
+
id: "followers",
|
|
1157
|
+
name: "Followers",
|
|
1158
|
+
category: category2,
|
|
1159
|
+
icon: "users",
|
|
1160
|
+
slots: ["key", "window"],
|
|
1161
|
+
display: true,
|
|
1162
|
+
look: (_, slot) => {
|
|
1163
|
+
const count = textLayer("label", `{{${grouped(followerCount.key)}}}`, {
|
|
1164
|
+
size: slot === "window" ? 26 : 18,
|
|
1165
|
+
row: slot === "window" ? "line" : ""
|
|
1166
|
+
});
|
|
1167
|
+
if (slot !== "window") {
|
|
1168
|
+
const icon = iconLayer("icon", "lucide/users", { color: LILAC, size: 44 });
|
|
1169
|
+
return { background: surface(BACKGROUND), layers: [icon, count] };
|
|
1170
|
+
}
|
|
1171
|
+
return {
|
|
1172
|
+
background: surface(BACKGROUND),
|
|
1173
|
+
layers: [
|
|
1174
|
+
iconLayer("icon", "lucide/users", { color: LILAC, size: 50, row: "line" }),
|
|
1175
|
+
count,
|
|
1176
|
+
textLayer("unit", "followers", { color: MUTED, size: 14, row: "line" })
|
|
1177
|
+
]
|
|
1178
|
+
};
|
|
1179
|
+
}
|
|
1180
|
+
});
|
|
1181
|
+
var actions = [
|
|
1182
|
+
status,
|
|
1183
|
+
clip,
|
|
1184
|
+
marker,
|
|
1185
|
+
chatMode2,
|
|
1186
|
+
shieldMode,
|
|
1187
|
+
message,
|
|
1188
|
+
announcement,
|
|
1189
|
+
clearChat,
|
|
1190
|
+
ad,
|
|
1191
|
+
snoozeAd,
|
|
1192
|
+
update,
|
|
1193
|
+
shoutout,
|
|
1194
|
+
raid,
|
|
1195
|
+
followers2
|
|
1196
|
+
];
|
|
1197
|
+
var rewards = plugin.source("rewards", async () => {
|
|
1198
|
+
try {
|
|
1199
|
+
const { id } = await twitch.user();
|
|
1200
|
+
const found = await twitch.request("GET", "/channel_points/custom_rewards", {
|
|
1201
|
+
broadcaster_id: id
|
|
1202
|
+
});
|
|
1203
|
+
return found.map(({ title }) => ({ value: title, label: title }));
|
|
1204
|
+
} catch (error) {
|
|
1205
|
+
if (error instanceof SignedOut || error instanceof TwitchError)
|
|
1206
|
+
return [];
|
|
1207
|
+
throw error;
|
|
1208
|
+
}
|
|
1209
|
+
});
|
|
1210
|
+
|
|
1211
|
+
// src/triggers.ts
|
|
1212
|
+
import { setting as setting3 } from "@sdeck/plugin";
|
|
1213
|
+
var on = (type, then) => onEvent((event) => {
|
|
1214
|
+
if (event.type === type)
|
|
1215
|
+
then(EVENTS[type](event.data));
|
|
1216
|
+
});
|
|
1217
|
+
var online = plugin.trigger({
|
|
1218
|
+
id: "online",
|
|
1219
|
+
name: "When you go live",
|
|
1220
|
+
subscribe: ({ fire }) => on("stream.online", fire)
|
|
1221
|
+
});
|
|
1222
|
+
var offline = plugin.trigger({
|
|
1223
|
+
id: "offline",
|
|
1224
|
+
name: "When your stream ends",
|
|
1225
|
+
subscribe: ({ fire }) => on("stream.offline", fire)
|
|
1226
|
+
});
|
|
1227
|
+
var follow = plugin.trigger({
|
|
1228
|
+
id: "follow",
|
|
1229
|
+
name: "When someone follows",
|
|
1230
|
+
description: "Gives event.user and event.login.",
|
|
1231
|
+
subscribe: ({ fire }) => on("channel.follow", fire)
|
|
1232
|
+
});
|
|
1233
|
+
var subscribe = plugin.trigger({
|
|
1234
|
+
id: "subscribe",
|
|
1235
|
+
name: "When someone subscribes",
|
|
1236
|
+
description: "Gives event.user, tier (1 to 3), months, message and gift.",
|
|
1237
|
+
settings: { gifts: setting3.boolean("Also for each gifted one", { default: true }) },
|
|
1238
|
+
subscribe: ({ settings, fire }) => {
|
|
1239
|
+
const stops = [
|
|
1240
|
+
on("channel.subscribe", (event) => {
|
|
1241
|
+
if (settings.gifts || !event.gift)
|
|
1242
|
+
fire(event);
|
|
1243
|
+
}),
|
|
1244
|
+
on("channel.subscription.message", fire)
|
|
1245
|
+
];
|
|
1246
|
+
return () => {
|
|
1247
|
+
for (const stop of stops)
|
|
1248
|
+
stop();
|
|
1249
|
+
};
|
|
1250
|
+
}
|
|
1251
|
+
});
|
|
1252
|
+
var gift = plugin.trigger({
|
|
1253
|
+
id: "gift",
|
|
1254
|
+
name: "When someone gives subscriptions",
|
|
1255
|
+
description: "Gives event.user, total and tier.",
|
|
1256
|
+
settings: { min: setting3.number("At least this many", { default: 1, min: 1 }) },
|
|
1257
|
+
subscribe: ({ settings, fire }) => on("channel.subscription.gift", (event) => {
|
|
1258
|
+
if (event.total >= settings.min)
|
|
1259
|
+
fire(event);
|
|
1260
|
+
})
|
|
1261
|
+
});
|
|
1262
|
+
var cheer = plugin.trigger({
|
|
1263
|
+
id: "cheer",
|
|
1264
|
+
name: "When someone cheers",
|
|
1265
|
+
description: "Gives event.user, bits and message.",
|
|
1266
|
+
settings: { min: setting3.number("At least this many bits", { default: 1, min: 1 }) },
|
|
1267
|
+
subscribe: ({ settings, fire }) => on("channel.cheer", (event) => {
|
|
1268
|
+
if (event.bits >= settings.min)
|
|
1269
|
+
fire(event);
|
|
1270
|
+
})
|
|
1271
|
+
});
|
|
1272
|
+
var raided = plugin.trigger({
|
|
1273
|
+
id: "raid",
|
|
1274
|
+
name: "When someone raids you",
|
|
1275
|
+
description: "Gives event.user, login and viewers.",
|
|
1276
|
+
settings: { min: setting3.number("At least this many viewers", { default: 1, min: 1 }) },
|
|
1277
|
+
subscribe: ({ settings, fire }) => on("channel.raid", (event) => {
|
|
1278
|
+
if (event.viewers >= settings.min)
|
|
1279
|
+
fire(event);
|
|
1280
|
+
})
|
|
1281
|
+
});
|
|
1282
|
+
var redeem = plugin.trigger({
|
|
1283
|
+
id: "redeem",
|
|
1284
|
+
name: "When a reward is redeemed",
|
|
1285
|
+
description: "Channel points. Gives event.user, reward, cost and input.",
|
|
1286
|
+
settings: {
|
|
1287
|
+
reward: setting3.text("Reward", { source: rewards.id, placeholder: "Any reward" })
|
|
1288
|
+
},
|
|
1289
|
+
subscribe: ({ settings, fire }) => {
|
|
1290
|
+
const wanted = settings.reward.trim().toLowerCase();
|
|
1291
|
+
return on("channel.channel_points_custom_reward_redemption.add", (event) => {
|
|
1292
|
+
if (!wanted || event.reward.toLowerCase() === wanted)
|
|
1293
|
+
fire(event);
|
|
1294
|
+
});
|
|
1295
|
+
}
|
|
1296
|
+
});
|
|
1297
|
+
var adBreak = plugin.trigger({
|
|
1298
|
+
id: "ad",
|
|
1299
|
+
name: "When an ad starts",
|
|
1300
|
+
description: "Gives event.seconds and automatic.",
|
|
1301
|
+
subscribe: ({ fire }) => on("channel.ad_break.begin", fire)
|
|
1302
|
+
});
|
|
1303
|
+
var command = plugin.trigger({
|
|
1304
|
+
id: "command",
|
|
1305
|
+
name: "When someone writes a chat command",
|
|
1306
|
+
description: "Gives event.user, message, and text: what follows the command.",
|
|
1307
|
+
settings: {
|
|
1308
|
+
command: setting3.text("Command", { default: "!deck", required: true }),
|
|
1309
|
+
who: setting3.select("Who can use it", {
|
|
1310
|
+
everyone: "Everyone",
|
|
1311
|
+
subscriber: "Subscribers and above",
|
|
1312
|
+
vip: "VIPs and above",
|
|
1313
|
+
moderator: "Moderators and you",
|
|
1314
|
+
broadcaster: "Only you"
|
|
1315
|
+
}, { default: "moderator" })
|
|
1316
|
+
},
|
|
1317
|
+
subscribe: ({ settings, fire }) => on("channel.chat.message", (event) => {
|
|
1318
|
+
const text = commandWords(event.message, settings.command);
|
|
1319
|
+
if (text !== undefined && hasRole(event.badges, settings.who))
|
|
1320
|
+
fire({ ...event, text });
|
|
1321
|
+
})
|
|
1322
|
+
});
|
|
1323
|
+
var triggers = [
|
|
1324
|
+
online,
|
|
1325
|
+
offline,
|
|
1326
|
+
follow,
|
|
1327
|
+
subscribe,
|
|
1328
|
+
gift,
|
|
1329
|
+
cheer,
|
|
1330
|
+
raided,
|
|
1331
|
+
redeem,
|
|
1332
|
+
adBreak,
|
|
1333
|
+
command
|
|
1334
|
+
];
|
|
1335
|
+
|
|
1336
|
+
// src/index.tsx
|
|
1337
|
+
var src_default = plugin.define({
|
|
1338
|
+
actions,
|
|
1339
|
+
state,
|
|
1340
|
+
triggers,
|
|
1341
|
+
sources: [rewards],
|
|
1342
|
+
commands: [signIn, signOut],
|
|
1343
|
+
activate: (context) => {
|
|
1344
|
+
if (!isSignedIn())
|
|
1345
|
+
context.status("Sign in to Twitch", "info");
|
|
1346
|
+
return stopAll;
|
|
1347
|
+
},
|
|
1348
|
+
onboarding: {
|
|
1349
|
+
intro: "Twitch lets apps in with a client id of your own. It takes two minutes, once.",
|
|
1350
|
+
steps: [
|
|
1351
|
+
{
|
|
1352
|
+
id: "app",
|
|
1353
|
+
title: "Register an app",
|
|
1354
|
+
body: [
|
|
1355
|
+
"Open the Twitch developer console and press **Register Your Application**. (Your account needs two-factor authentication for this.)",
|
|
1356
|
+
"Give it any name, the redirect URL `http://localhost:3000`, a category, and the client type **Public**. Then open it with **Manage** and copy its **Client ID** here. It needs no secret."
|
|
1357
|
+
].join(`
|
|
1358
|
+
|
|
1359
|
+
`),
|
|
1360
|
+
link: { label: "Twitch developer console", url: "https://dev.twitch.tv/console/apps" },
|
|
1361
|
+
settings: ["clientId"]
|
|
1362
|
+
},
|
|
1363
|
+
{
|
|
1364
|
+
id: "sign-in",
|
|
1365
|
+
title: "Sign in",
|
|
1366
|
+
body: "Allow access in the browser that opens, with the account you stream from.",
|
|
1367
|
+
command: "sign-in",
|
|
1368
|
+
done: isSignedIn
|
|
1369
|
+
}
|
|
1370
|
+
],
|
|
1371
|
+
starterPage: {
|
|
1372
|
+
name: "Twitch",
|
|
1373
|
+
keys: {
|
|
1374
|
+
0: { action: "status" },
|
|
1375
|
+
1: { action: "clip" },
|
|
1376
|
+
2: { action: "marker" },
|
|
1377
|
+
3: { action: "snooze-ad" },
|
|
1378
|
+
4: { action: "chat-mode", settings: { mode: "emote_mode" } },
|
|
1379
|
+
5: { action: "chat-mode", settings: { mode: "follower_mode" } },
|
|
1380
|
+
6: { action: "chat-mode", settings: { mode: "slow_mode" } },
|
|
1381
|
+
7: { action: "shield" }
|
|
1382
|
+
},
|
|
1383
|
+
window: { action: "status" }
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
});
|
|
1387
|
+
export {
|
|
1388
|
+
src_default as default
|
|
1389
|
+
};
|
|
1390
|
+
|
|
1391
|
+
//# debugId=F0E94A5D27E7667264756E2164756E21
|