@sdeck/youtube 0.0.0-stage → 1.0.1
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 +81 -2
- package/dist/actions.d.ts +4 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/api.d.ts +68 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/chat.d.ts +28 -0
- package/dist/chat.d.ts.map +1 -0
- package/dist/displays.d.ts +2 -0
- package/dist/displays.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1792 -0
- package/dist/index.js.map +19 -0
- package/dist/looks.d.ts +11 -0
- package/dist/looks.d.ts.map +1 -0
- package/dist/model.d.ts +175 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/plugin.d.ts +68 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/state.d.ts +19 -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 +67 -4
- package/src/actions.ts +471 -0
- package/src/api.ts +367 -0
- package/src/chat.ts +95 -0
- package/src/displays.ts +270 -0
- package/src/index.tsx +58 -0
- package/src/looks.ts +18 -0
- package/src/model.ts +378 -0
- package/src/plugin.ts +326 -0
- package/src/state.ts +99 -0
- package/src/triggers.ts +177 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"triggers.d.ts","sourceRoot":"","sources":["../src/triggers.ts"],"names":[],"mappings":"AAoKA,eAAO,MAAM,QAAQ,mCAYpB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,69 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdeck/youtube",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Run your YouTube channel: go live, chat, polls, ads, markers and goals, with live values and triggers.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"keywords": [
|
|
9
|
+
"sdeck-plugin"
|
|
10
|
+
],
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"src"
|
|
14
|
+
],
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"bun": "./src/index.tsx",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"sdeck": {
|
|
26
|
+
"apiVersion": 5,
|
|
27
|
+
"entry": "./dist/index.js",
|
|
28
|
+
"icon": "si/SiYoutube#ff0000",
|
|
29
|
+
"permissions": [
|
|
30
|
+
"network"
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "bun ../../scripts/build-package.ts --target=bun",
|
|
35
|
+
"clean": "rm -rf dist",
|
|
36
|
+
"test": "bun test"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"zod": "^4.6.7"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@sdeck/plugin": "^0.5.0",
|
|
43
|
+
"@sdeck/schema": "^0.5.0",
|
|
44
|
+
"react": ">=19.3"
|
|
45
|
+
},
|
|
46
|
+
"peerDependenciesMeta": {
|
|
47
|
+
"@sdeck/plugin": {
|
|
48
|
+
"optional": true
|
|
49
|
+
},
|
|
50
|
+
"@sdeck/schema": {
|
|
51
|
+
"optional": true
|
|
52
|
+
},
|
|
53
|
+
"react": {
|
|
54
|
+
"optional": true
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@sdeck/plugin": "0.5.0",
|
|
59
|
+
"@sdeck/schema": "0.5.0"
|
|
60
|
+
},
|
|
61
|
+
"repository": {
|
|
62
|
+
"type": "git",
|
|
63
|
+
"url": "git+https://github.com/ivanoliverfabra/sdeck.git",
|
|
64
|
+
"directory": "packages/plugin-youtube"
|
|
65
|
+
},
|
|
66
|
+
"publishConfig": {
|
|
67
|
+
"access": "public"
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/actions.ts
ADDED
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
import { setting } from "@sdeck/plugin";
|
|
2
|
+
import type { Json } from "./api.ts";
|
|
3
|
+
import { pause } from "./chat.ts";
|
|
4
|
+
import { category, key, slots } from "./looks.ts";
|
|
5
|
+
import {
|
|
6
|
+
addChapter,
|
|
7
|
+
type Broadcast,
|
|
8
|
+
formatDuration,
|
|
9
|
+
isActive,
|
|
10
|
+
parseBroadcast,
|
|
11
|
+
parsePoll,
|
|
12
|
+
removeChapter,
|
|
13
|
+
} from "./model.ts";
|
|
14
|
+
import { liveChat, liveStream, plugin, readEncoders, stream, youtube } from "./plugin.ts";
|
|
15
|
+
import * as values from "./state.ts";
|
|
16
|
+
|
|
17
|
+
// -- Settings -----------------------------------------------------------------------------------
|
|
18
|
+
|
|
19
|
+
const atMost = (length: number) => (text: string) =>
|
|
20
|
+
[...text].length > length ? `cannot be longer than ${length} letters` : undefined;
|
|
21
|
+
|
|
22
|
+
/** Titles and descriptions: YouTube refuses these two signs. */
|
|
23
|
+
const plain = (length: number) => (text: string) =>
|
|
24
|
+
/[<>]/.test(text) ? "cannot have < or > in it" : atMost(length)(text);
|
|
25
|
+
|
|
26
|
+
const caption = setting.text("Caption", { appearance: true });
|
|
27
|
+
|
|
28
|
+
// -- Going live ---------------------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
async function upcoming(): Promise<Broadcast[]> {
|
|
31
|
+
const found = await youtube.list("/liveBroadcasts", {
|
|
32
|
+
part: "id,snippet,status,contentDetails",
|
|
33
|
+
broadcastStatus: "upcoming",
|
|
34
|
+
broadcastType: "all",
|
|
35
|
+
maxResults: "50",
|
|
36
|
+
});
|
|
37
|
+
return found.map(parseBroadcast).sort((a, b) => a.at - b.at);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const transition = (id: string, to: "testing" | "live" | "complete") =>
|
|
41
|
+
youtube.request("POST", "/liveBroadcasts/transition", {
|
|
42
|
+
id,
|
|
43
|
+
broadcastStatus: to,
|
|
44
|
+
part: "id,status",
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
/** Waits until YouTube has done it, which takes a few seconds. Whether it did within a minute. */
|
|
48
|
+
async function reaches(id: string, status: string, signal: AbortSignal): Promise<boolean> {
|
|
49
|
+
for (let i = 0; i < 20; i++) {
|
|
50
|
+
const [now] = await youtube.list("/liveBroadcasts", { part: "id,status", id });
|
|
51
|
+
if (now && parseBroadcast(now).status === status) return true;
|
|
52
|
+
await pause(3000, signal);
|
|
53
|
+
if (signal.aborted) throw new Error("stopped");
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const goLive = plugin.action({
|
|
59
|
+
id: "go-live",
|
|
60
|
+
name: "Go live",
|
|
61
|
+
description:
|
|
62
|
+
"Starts a stream you set up in YouTube Studio, once your encoder sends video. Not needed for streams that start by themselves.",
|
|
63
|
+
category,
|
|
64
|
+
icon: "podcast",
|
|
65
|
+
slots,
|
|
66
|
+
settings: {
|
|
67
|
+
broadcast: setting.text("Stream", {
|
|
68
|
+
source: "broadcasts",
|
|
69
|
+
placeholder: "The next one",
|
|
70
|
+
description: "Its title.",
|
|
71
|
+
}),
|
|
72
|
+
},
|
|
73
|
+
run: async ({ settings, signal }) => {
|
|
74
|
+
const wanted = settings.broadcast.trim().toLowerCase();
|
|
75
|
+
const planned = (await upcoming()).filter((b) => !wanted || b.title.toLowerCase() === wanted);
|
|
76
|
+
if (planned.length === 0) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
wanted
|
|
79
|
+
? `no stream called "${settings.broadcast}" is planned`
|
|
80
|
+
: "no stream is planned: set one up in YouTube Studio first",
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
const receiving = new Set((await readEncoders()).filter(isActive).map(({ id }) => id));
|
|
84
|
+
const next = planned.find((b) => receiving.has(b.streamId));
|
|
85
|
+
if (!next) {
|
|
86
|
+
throw new Error(
|
|
87
|
+
"YouTube receives no video for it yet: start streaming in OBS or your encoder first",
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
if (next.preview && next.status !== "testing") {
|
|
91
|
+
if (next.status !== "testStarting") await transition(next.id, "testing");
|
|
92
|
+
if (!(await reaches(next.id, "testing", signal))) {
|
|
93
|
+
throw new Error("YouTube's preview did not start; try again");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
await transition(next.id, "live");
|
|
97
|
+
await reaches(next.id, "live", signal);
|
|
98
|
+
stream.refresh();
|
|
99
|
+
return { result: `https://youtu.be/${next.id}`, label: `Go live: ${next.title}` };
|
|
100
|
+
},
|
|
101
|
+
look: () => key("lucide/podcast", `{{${values.live.key} ? "Live" : "Go live"}}`),
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const endStream = plugin.action({
|
|
105
|
+
id: "end-stream",
|
|
106
|
+
name: "End the stream",
|
|
107
|
+
description: "This cannot be undone: put it on a hold or a double press.",
|
|
108
|
+
category,
|
|
109
|
+
icon: "circle-stop",
|
|
110
|
+
slots,
|
|
111
|
+
run: async () => {
|
|
112
|
+
const { id } = await liveStream();
|
|
113
|
+
await transition(id, "complete");
|
|
114
|
+
stream.refresh();
|
|
115
|
+
return { label: "End the stream" };
|
|
116
|
+
},
|
|
117
|
+
look: () => key("lucide/circle-stop", "End"),
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
/** Titles of the streams that are planned. */
|
|
121
|
+
export const broadcasts = plugin.source("broadcasts", async () => {
|
|
122
|
+
const titles = new Set((await upcoming()).map(({ title }) => title));
|
|
123
|
+
return [...titles].map((title) => ({ value: title, label: title }));
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// -- The stream's video -------------------------------------------------------------------------
|
|
127
|
+
|
|
128
|
+
/** What a change has to send along, or YouTube empties it. */
|
|
129
|
+
const KEPT = {
|
|
130
|
+
snippet: [
|
|
131
|
+
"title",
|
|
132
|
+
"description",
|
|
133
|
+
"tags",
|
|
134
|
+
"categoryId",
|
|
135
|
+
"defaultLanguage",
|
|
136
|
+
"defaultAudioLanguage",
|
|
137
|
+
],
|
|
138
|
+
status: [
|
|
139
|
+
"privacyStatus",
|
|
140
|
+
"embeddable",
|
|
141
|
+
"license",
|
|
142
|
+
"publicStatsViewable",
|
|
143
|
+
"selfDeclaredMadeForKids",
|
|
144
|
+
"containsSyntheticMedia",
|
|
145
|
+
],
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
type Texts = Record<string, string>;
|
|
149
|
+
|
|
150
|
+
let editing: Promise<unknown> = Promise.resolve();
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Changes part of a video, keeping the rest as it is now. One at a time, so that none undoes
|
|
154
|
+
* another. Resolves with how it was.
|
|
155
|
+
*/
|
|
156
|
+
function edit(id: string, part: keyof typeof KEPT, change: (now: Texts) => Texts): Promise<Texts> {
|
|
157
|
+
const next = editing
|
|
158
|
+
.catch(() => {})
|
|
159
|
+
.then(async () => {
|
|
160
|
+
const [video] = await youtube.list("/videos", { part, id });
|
|
161
|
+
if (!video) throw new Error("the video is gone");
|
|
162
|
+
const now = video[part] as Texts;
|
|
163
|
+
const same = Object.entries(now).filter(([name]) => KEPT[part].includes(name));
|
|
164
|
+
const changed = { ...Object.fromEntries(same), ...change(now) };
|
|
165
|
+
await youtube.request("PUT", "/videos", { part }, { id, [part]: changed });
|
|
166
|
+
return now;
|
|
167
|
+
});
|
|
168
|
+
editing = next;
|
|
169
|
+
return next;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const update = plugin.action({
|
|
173
|
+
id: "update",
|
|
174
|
+
name: "Change title or description",
|
|
175
|
+
description: "Of the stream that is live.",
|
|
176
|
+
category,
|
|
177
|
+
icon: "pencil",
|
|
178
|
+
slots,
|
|
179
|
+
settings: {
|
|
180
|
+
title: setting.text("Title", { placeholder: "Leave it as it is", validate: plain(100) }),
|
|
181
|
+
description: setting.text("Description", {
|
|
182
|
+
placeholder: "Leave it as it is",
|
|
183
|
+
multiline: true,
|
|
184
|
+
validate: plain(5000),
|
|
185
|
+
}),
|
|
186
|
+
caption,
|
|
187
|
+
},
|
|
188
|
+
run: async ({ settings }) => {
|
|
189
|
+
const title = settings.title.trim();
|
|
190
|
+
const description = settings.description.trim();
|
|
191
|
+
if (!title && !description) throw new Error("enter a title or a description");
|
|
192
|
+
const { id } = await liveStream();
|
|
193
|
+
const set = async (to: Texts) => {
|
|
194
|
+
const before = await edit(id, "snippet", () => ({
|
|
195
|
+
...(title && { title: to.title ?? "" }),
|
|
196
|
+
...(description && { description: to.description ?? "" }),
|
|
197
|
+
}));
|
|
198
|
+
stream.refresh();
|
|
199
|
+
return before;
|
|
200
|
+
};
|
|
201
|
+
const before = await set({ title, description });
|
|
202
|
+
return { label: title || "Description", undo: () => set(before) };
|
|
203
|
+
},
|
|
204
|
+
look: (s) => key("lucide/pencil", s.caption),
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
const PRIVACY = { public: "Public", unlisted: "Unlisted", private: "Private" } as const;
|
|
208
|
+
const PRIVACY_ICONS = { public: "globe", unlisted: "link", private: "lock" } as const;
|
|
209
|
+
|
|
210
|
+
const privacy = plugin.action({
|
|
211
|
+
id: "privacy",
|
|
212
|
+
name: "Who can watch",
|
|
213
|
+
description: "Makes the stream that is live public, unlisted or private.",
|
|
214
|
+
category,
|
|
215
|
+
icon: "globe",
|
|
216
|
+
slots,
|
|
217
|
+
settings: { to: setting.select("Make it", PRIVACY) },
|
|
218
|
+
run: async ({ settings }) => {
|
|
219
|
+
const { id } = await liveStream();
|
|
220
|
+
const set = (privacyStatus: string) => edit(id, "status", () => ({ privacyStatus }));
|
|
221
|
+
const { privacyStatus } = await set(settings.to);
|
|
222
|
+
const label = `${PRIVACY[settings.to]} stream`;
|
|
223
|
+
if (!privacyStatus || privacyStatus === settings.to) return { label };
|
|
224
|
+
return { label, undo: () => set(privacyStatus) };
|
|
225
|
+
},
|
|
226
|
+
look: (s) => key(`lucide/${PRIVACY_ICONS[s.to]}`, PRIVACY[s.to]),
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
const marker = plugin.action({
|
|
230
|
+
id: "marker",
|
|
231
|
+
name: "Mark this moment",
|
|
232
|
+
description:
|
|
233
|
+
"Adds the time to the stream's description, where YouTube makes a chapter of it. Its result is the time.",
|
|
234
|
+
category,
|
|
235
|
+
icon: "bookmark",
|
|
236
|
+
slots,
|
|
237
|
+
settings: {
|
|
238
|
+
text: setting.text("Name", { default: "Highlight", required: true, validate: plain(100) }),
|
|
239
|
+
back: setting.number("Seconds ago", {
|
|
240
|
+
default: 0,
|
|
241
|
+
min: 0,
|
|
242
|
+
max: 600,
|
|
243
|
+
description: "For what you only know was good once it is over.",
|
|
244
|
+
}),
|
|
245
|
+
},
|
|
246
|
+
run: async ({ settings }) => {
|
|
247
|
+
const { id, startedAt } = await liveStream();
|
|
248
|
+
const time = formatDuration(Math.max(1, (Date.now() - startedAt) / 1000 - settings.back));
|
|
249
|
+
const line = `${time} ${settings.text.trim().replace(/\s+/g, " ")}`;
|
|
250
|
+
await edit(id, "snippet", ({ description = "" }) => {
|
|
251
|
+
const longer = addChapter(description, line);
|
|
252
|
+
if ([...longer].length > 5000) throw new Error("the stream's description is full");
|
|
253
|
+
return { description: longer };
|
|
254
|
+
});
|
|
255
|
+
const undo = () =>
|
|
256
|
+
edit(id, "snippet", ({ description = "" }) => ({
|
|
257
|
+
description: removeChapter(description, line),
|
|
258
|
+
}));
|
|
259
|
+
return { result: time, label: `Mark ${time}`, undo };
|
|
260
|
+
},
|
|
261
|
+
look: () => key("lucide/bookmark"),
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
const ad = plugin.action({
|
|
265
|
+
id: "ad",
|
|
266
|
+
name: "Play an ad",
|
|
267
|
+
description: "Your channel and the stream have to be monetized.",
|
|
268
|
+
category,
|
|
269
|
+
icon: "badge-dollar-sign",
|
|
270
|
+
slots,
|
|
271
|
+
settings: {
|
|
272
|
+
// A list: an object would put numbers in its own order.
|
|
273
|
+
length: setting.select("Length", [
|
|
274
|
+
{ value: "30", label: "30 seconds" },
|
|
275
|
+
{ value: "60", label: "1 minute" },
|
|
276
|
+
{ value: "90", label: "1½ minutes" },
|
|
277
|
+
{ value: "120", label: "2 minutes" },
|
|
278
|
+
{ value: "180", label: "3 minutes" },
|
|
279
|
+
]),
|
|
280
|
+
},
|
|
281
|
+
run: async ({ settings }) => {
|
|
282
|
+
const { id } = await liveStream();
|
|
283
|
+
const body = { cueType: "cueTypeAd", durationSecs: Number(settings.length) };
|
|
284
|
+
await youtube.request("POST", "/liveBroadcasts/cuepoint", { id }, body);
|
|
285
|
+
return { label: `Ad, ${settings.length} s` };
|
|
286
|
+
},
|
|
287
|
+
look: (s) => key("lucide/badge-dollar-sign", `${s.length} s`),
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
// -- Chat ---------------------------------------------------------------------------------------
|
|
291
|
+
|
|
292
|
+
/** Writes to the chat. Resolves with the message's id. */
|
|
293
|
+
async function post(chatId: string, snippet: Json): Promise<string> {
|
|
294
|
+
const { id } = await youtube.request(
|
|
295
|
+
"POST",
|
|
296
|
+
"/liveChat/messages",
|
|
297
|
+
{ part: "snippet" },
|
|
298
|
+
{ snippet: { liveChatId: chatId, ...snippet } },
|
|
299
|
+
);
|
|
300
|
+
return String(id ?? "");
|
|
301
|
+
}
|
|
302
|
+
const say = (chatId: string, messageText: string) =>
|
|
303
|
+
post(chatId, { type: "textMessageEvent", textMessageDetails: { messageText } });
|
|
304
|
+
const deleteMessage = (id: string) => youtube.request("DELETE", "/liveChat/messages", { id });
|
|
305
|
+
|
|
306
|
+
const message = plugin.action({
|
|
307
|
+
id: "message",
|
|
308
|
+
name: "Send a chat message",
|
|
309
|
+
category,
|
|
310
|
+
icon: "message-square",
|
|
311
|
+
slots,
|
|
312
|
+
settings: {
|
|
313
|
+
text: setting.text("Message", { multiline: true, required: true, validate: atMost(200) }),
|
|
314
|
+
caption,
|
|
315
|
+
},
|
|
316
|
+
run: async ({ settings }) => {
|
|
317
|
+
const id = await say(await liveChat(), settings.text.trim());
|
|
318
|
+
return { label: "Chat message", ...(id && { undo: () => deleteMessage(id) }) };
|
|
319
|
+
},
|
|
320
|
+
look: (s) => key("lucide/message-square", s.caption),
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
const removeMessage = plugin.action({
|
|
324
|
+
id: "delete-message",
|
|
325
|
+
name: "Delete a chat message",
|
|
326
|
+
description: "For a workflow that a chat message starts.",
|
|
327
|
+
category,
|
|
328
|
+
icon: "message-square-x",
|
|
329
|
+
slots,
|
|
330
|
+
settings: {
|
|
331
|
+
message: setting.text("Message ID", { default: "{{event.messageId}}", required: true }),
|
|
332
|
+
},
|
|
333
|
+
run: async ({ settings }) => {
|
|
334
|
+
await deleteMessage(settings.message.trim());
|
|
335
|
+
return { label: "Delete message" };
|
|
336
|
+
},
|
|
337
|
+
look: () => key("lucide/message-square-x"),
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
const block = plugin.action({
|
|
341
|
+
id: "block",
|
|
342
|
+
name: "Time out or hide someone",
|
|
343
|
+
description:
|
|
344
|
+
"For a workflow that a chat message starts. Hiding can be undone, a timeout ends by itself.",
|
|
345
|
+
category,
|
|
346
|
+
icon: "user-x",
|
|
347
|
+
slots,
|
|
348
|
+
settings: {
|
|
349
|
+
channel: setting.text("Their channel ID", {
|
|
350
|
+
default: "{{event.channelId}}",
|
|
351
|
+
required: true,
|
|
352
|
+
}),
|
|
353
|
+
how: setting.select("What to do", {
|
|
354
|
+
temporary: "Time out",
|
|
355
|
+
permanent: "Hide from the channel",
|
|
356
|
+
}),
|
|
357
|
+
seconds: setting.number("Seconds", {
|
|
358
|
+
default: 300,
|
|
359
|
+
min: 1,
|
|
360
|
+
max: 86_400,
|
|
361
|
+
visibleIf: { how: "temporary" },
|
|
362
|
+
}),
|
|
363
|
+
},
|
|
364
|
+
run: async ({ settings }) => {
|
|
365
|
+
const { how, seconds } = settings;
|
|
366
|
+
const channelId = settings.channel.trim();
|
|
367
|
+
if (!/^UC[\w-]{22}$/.test(channelId)) {
|
|
368
|
+
throw new Error("a channel ID starts with UC and has 24 letters");
|
|
369
|
+
}
|
|
370
|
+
const snippet = {
|
|
371
|
+
liveChatId: await liveChat(),
|
|
372
|
+
type: how,
|
|
373
|
+
bannedUserDetails: { channelId },
|
|
374
|
+
...(how === "temporary" && { banDurationSeconds: seconds }),
|
|
375
|
+
};
|
|
376
|
+
const { id } = await youtube.request(
|
|
377
|
+
"POST",
|
|
378
|
+
"/liveChat/bans",
|
|
379
|
+
{ part: "snippet" },
|
|
380
|
+
{ snippet },
|
|
381
|
+
);
|
|
382
|
+
if (how === "temporary") return { label: `Timeout, ${seconds} s` };
|
|
383
|
+
const undo = () => youtube.request("DELETE", "/liveChat/bans", { id: String(id) });
|
|
384
|
+
return { label: "Hide someone", ...(id !== undefined && { undo }) };
|
|
385
|
+
},
|
|
386
|
+
look: (s) => key(s.how === "temporary" ? "lucide/timer" : "lucide/user-x"),
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
// -- Polls --------------------------------------------------------------------------------------
|
|
390
|
+
|
|
391
|
+
const answer = (label: string, required = false) =>
|
|
392
|
+
setting.text(label, { required, validate: atMost(35) });
|
|
393
|
+
|
|
394
|
+
const closePoll = async (id: string) =>
|
|
395
|
+
parsePoll(
|
|
396
|
+
await youtube.request("POST", "/liveChat/messages/transition", {
|
|
397
|
+
id,
|
|
398
|
+
status: "closed",
|
|
399
|
+
part: "id,snippet",
|
|
400
|
+
}),
|
|
401
|
+
);
|
|
402
|
+
|
|
403
|
+
const startPoll = plugin.action({
|
|
404
|
+
id: "poll",
|
|
405
|
+
name: "Start a poll",
|
|
406
|
+
description: "In the chat of your stream. Only one can run at a time.",
|
|
407
|
+
category,
|
|
408
|
+
icon: "chart-bar",
|
|
409
|
+
slots,
|
|
410
|
+
settings: {
|
|
411
|
+
question: setting.text("Question", { required: true, validate: atMost(100) }),
|
|
412
|
+
first: answer("Answer 1", true),
|
|
413
|
+
second: answer("Answer 2", true),
|
|
414
|
+
third: answer("Answer 3"),
|
|
415
|
+
fourth: answer("Answer 4"),
|
|
416
|
+
caption,
|
|
417
|
+
},
|
|
418
|
+
run: async ({ settings }) => {
|
|
419
|
+
const { first, second, third, fourth } = settings;
|
|
420
|
+
const questionText = settings.question.trim();
|
|
421
|
+
const options = [first, second, third, fourth]
|
|
422
|
+
.map((text) => ({ optionText: text.trim() }))
|
|
423
|
+
.filter(({ optionText }) => optionText);
|
|
424
|
+
const id = await post(await liveChat(), {
|
|
425
|
+
type: "pollEvent",
|
|
426
|
+
pollDetails: { metadata: { questionText, options } },
|
|
427
|
+
});
|
|
428
|
+
return { label: `Poll: ${questionText}`, ...(id && { undo: () => closePoll(id) }) };
|
|
429
|
+
},
|
|
430
|
+
look: (s) => key("lucide/chart-bar", s.caption),
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
const endPoll = plugin.action({
|
|
434
|
+
id: "end-poll",
|
|
435
|
+
name: "End the poll",
|
|
436
|
+
description: "Its result is the answer that won, or nothing for a draw.",
|
|
437
|
+
category,
|
|
438
|
+
icon: "circle-check-big",
|
|
439
|
+
slots,
|
|
440
|
+
settings: { tell: setting.boolean("Write the winner in chat") },
|
|
441
|
+
run: async ({ settings }) => {
|
|
442
|
+
const chatId = await liveChat();
|
|
443
|
+
const { activePollItem } = await youtube.request("GET", "/liveChat/messages", {
|
|
444
|
+
liveChatId: chatId,
|
|
445
|
+
part: "id,snippet",
|
|
446
|
+
maxResults: "200",
|
|
447
|
+
});
|
|
448
|
+
const { id } = parsePoll(activePollItem as Json | undefined);
|
|
449
|
+
if (!id) throw new Error("no poll is running");
|
|
450
|
+
const { winner, question, votes } = await closePoll(id);
|
|
451
|
+
if (settings.tell && winner) {
|
|
452
|
+
await say(chatId, `${question} ${winner} (${votes} ${votes === 1 ? "vote" : "votes"})`);
|
|
453
|
+
}
|
|
454
|
+
return { result: winner, label: winner ? `Poll: ${winner}` : "End the poll" };
|
|
455
|
+
},
|
|
456
|
+
look: () => key("lucide/circle-check-big", "End poll"),
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
export const controls = [
|
|
460
|
+
goLive,
|
|
461
|
+
endStream,
|
|
462
|
+
marker,
|
|
463
|
+
message,
|
|
464
|
+
startPoll,
|
|
465
|
+
endPoll,
|
|
466
|
+
ad,
|
|
467
|
+
update,
|
|
468
|
+
privacy,
|
|
469
|
+
block,
|
|
470
|
+
removeMessage,
|
|
471
|
+
];
|