@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
package/src/state.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/** The channel's live values: `{{youtube.viewers}}` and so on. */
|
|
2
|
+
import { following } from "@sdeck/plugin";
|
|
3
|
+
import { formatDuration } from "./model.ts";
|
|
4
|
+
import { channel, health, plugin, stream, subscribers, video } from "./plugin.ts";
|
|
5
|
+
|
|
6
|
+
const { boolean, number, string } = plugin.state;
|
|
7
|
+
|
|
8
|
+
export const live = boolean("live", "Live", { start: following(stream.shared, (s) => s.live) });
|
|
9
|
+
export const viewers = number("viewers", "Viewers", {
|
|
10
|
+
start: following(stream.shared, (s) => s.viewers),
|
|
11
|
+
});
|
|
12
|
+
export const uptime = string("uptime", "Time live", {
|
|
13
|
+
description: "Such as 1:02:05; empty while offline.",
|
|
14
|
+
start: (set) => {
|
|
15
|
+
const update = () => {
|
|
16
|
+
const now = stream.shared.last;
|
|
17
|
+
set(now && (now.live ? formatDuration((Date.now() - now.startedAt) / 1000) : ""));
|
|
18
|
+
};
|
|
19
|
+
const stop = stream.shared.listen(update);
|
|
20
|
+
update();
|
|
21
|
+
const timer = setInterval(update, 1000);
|
|
22
|
+
return () => {
|
|
23
|
+
stop();
|
|
24
|
+
clearInterval(timer);
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
export const title = string("title", "Stream title", {
|
|
29
|
+
description: "Empty while offline.",
|
|
30
|
+
start: following(stream.shared, (s) => s.title),
|
|
31
|
+
});
|
|
32
|
+
export const likes = number("likes", "Likes of the stream", {
|
|
33
|
+
start: following(stream.shared, (s) => s.likes),
|
|
34
|
+
});
|
|
35
|
+
export const views = number("views", "Views of the stream", {
|
|
36
|
+
description: "Everyone who watched so far.",
|
|
37
|
+
start: following(stream.shared, (s) => s.views),
|
|
38
|
+
});
|
|
39
|
+
export const url = string("url", "Link to the stream", {
|
|
40
|
+
description: "Empty while offline.",
|
|
41
|
+
start: following(stream.shared, (s) => (s.live ? `https://youtu.be/${s.id}` : "")),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
export const healthName = string("health", "Stream health", {
|
|
45
|
+
description: "Good, OK, Bad or No data; Offline when YouTube receives no video.",
|
|
46
|
+
start: following(health.shared, (h) => h.name),
|
|
47
|
+
});
|
|
48
|
+
export const healthProblem = string("healthProblem", "Stream problem", {
|
|
49
|
+
description: "The worst one YouTube sees, or empty.",
|
|
50
|
+
start: following(health.shared, (h) => h.problem),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
export const subscriberCount = number("subscribers", "Subscribers", {
|
|
54
|
+
description: "YouTube rounds it down to three digits.",
|
|
55
|
+
start: following(channel.shared, (c) => c.subscribers),
|
|
56
|
+
});
|
|
57
|
+
export const channelViews = number("channelViews", "Views of the channel", {
|
|
58
|
+
start: following(channel.shared, (c) => c.views),
|
|
59
|
+
});
|
|
60
|
+
export const channelVideos = number("videos", "Public videos", {
|
|
61
|
+
start: following(channel.shared, (c) => c.videos),
|
|
62
|
+
});
|
|
63
|
+
export const latestSubscriber = string("latestSubscriber", "Newest subscriber", {
|
|
64
|
+
description: "Of those who show their subscriptions to everyone.",
|
|
65
|
+
start: following(subscribers.shared, (s) => s[0]?.user ?? ""),
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
export const videoTitle = string("videoTitle", "Newest video: title", {
|
|
69
|
+
start: following(video.shared, (v) => v.title),
|
|
70
|
+
});
|
|
71
|
+
export const videoViews = number("videoViews", "Newest video: views", {
|
|
72
|
+
start: following(video.shared, (v) => v.views),
|
|
73
|
+
});
|
|
74
|
+
export const videoLikes = number("videoLikes", "Newest video: likes", {
|
|
75
|
+
start: following(video.shared, (v) => v.likes),
|
|
76
|
+
});
|
|
77
|
+
export const videoComments = number("videoComments", "Newest video: comments", {
|
|
78
|
+
start: following(video.shared, (v) => v.comments),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
export const state = [
|
|
82
|
+
live,
|
|
83
|
+
viewers,
|
|
84
|
+
uptime,
|
|
85
|
+
title,
|
|
86
|
+
likes,
|
|
87
|
+
views,
|
|
88
|
+
url,
|
|
89
|
+
healthName,
|
|
90
|
+
healthProblem,
|
|
91
|
+
subscriberCount,
|
|
92
|
+
channelViews,
|
|
93
|
+
channelVideos,
|
|
94
|
+
latestSubscriber,
|
|
95
|
+
videoTitle,
|
|
96
|
+
videoViews,
|
|
97
|
+
videoLikes,
|
|
98
|
+
videoComments,
|
|
99
|
+
];
|
package/src/triggers.ts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { type Shared, setting } from "@sdeck/plugin";
|
|
2
|
+
import { CHAT, type ChatData, type ChatType, commandWords, hasRole, newAtTop } from "./model.ts";
|
|
3
|
+
import { chat, comments, plugin, stream, subscribers } from "./plugin.ts";
|
|
4
|
+
|
|
5
|
+
/** Calls `then` with every chat event of one type. */
|
|
6
|
+
const on = <T extends ChatType>(type: T, then: (event: ChatData<T>) => void) =>
|
|
7
|
+
chat.listen((event) => {
|
|
8
|
+
if (event.type === type) then(CHAT[type](event.item) as ChatData<T>);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
/** Calls `then` when the stream starts, or when it ends. */
|
|
12
|
+
function onLive(wanted: boolean, then: (event: { title: string; url: string }) => void) {
|
|
13
|
+
let before = stream.shared.last;
|
|
14
|
+
return stream.shared.listen((now) => {
|
|
15
|
+
// An error is no news about the stream.
|
|
16
|
+
if (!now) return;
|
|
17
|
+
const was = before;
|
|
18
|
+
before = now;
|
|
19
|
+
if (!was || was.live === now.live || now.live !== wanted) return;
|
|
20
|
+
const { title, id } = now.live ? now : was;
|
|
21
|
+
then({ title, url: `https://youtu.be/${id}` });
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Calls `then` with what is added to a list while listening. */
|
|
26
|
+
function onNew<T extends { id: string }>(shared: Shared<T[] | undefined>, then: (item: T) => void) {
|
|
27
|
+
let known = shared.last && new Set(shared.last.map(({ id }) => id));
|
|
28
|
+
return shared.listen((items) => {
|
|
29
|
+
if (!items) return;
|
|
30
|
+
const fresh = known ? newAtTop(items, known) : [];
|
|
31
|
+
known ??= new Set();
|
|
32
|
+
for (const { id } of items) known.add(id);
|
|
33
|
+
for (const item of fresh) then(item);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const online = plugin.trigger({
|
|
38
|
+
id: "online",
|
|
39
|
+
name: "When you go live",
|
|
40
|
+
description: "Within a minute. Gives event.title and url.",
|
|
41
|
+
subscribe: ({ fire }) => onLive(true, fire),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const offline = plugin.trigger({
|
|
45
|
+
id: "offline",
|
|
46
|
+
name: "When your stream ends",
|
|
47
|
+
description: "Gives event.title and url.",
|
|
48
|
+
subscribe: ({ fire }) => onLive(false, fire),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const subscribe = plugin.trigger({
|
|
52
|
+
id: "subscribe",
|
|
53
|
+
name: "When someone subscribes",
|
|
54
|
+
description:
|
|
55
|
+
"Within a few minutes, and only for people who show their subscriptions to everyone. Gives event.user and channelId.",
|
|
56
|
+
subscribe: ({ fire }) =>
|
|
57
|
+
onNew(subscribers.shared, ({ user, channelId }) => fire({ user, channelId })),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const comment = plugin.trigger({
|
|
61
|
+
id: "comment",
|
|
62
|
+
name: "When someone comments on a video",
|
|
63
|
+
description: "Within a few minutes. Gives event.user, text and url.",
|
|
64
|
+
subscribe: ({ fire }) => onNew(comments.shared, fire),
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const superChat = plugin.trigger({
|
|
68
|
+
id: "super-chat",
|
|
69
|
+
name: "When someone sends a Super Chat",
|
|
70
|
+
description: "Gives event.user, amount, currency, amountText (such as $5.00) and message.",
|
|
71
|
+
settings: {
|
|
72
|
+
min: setting.number("At least this much", {
|
|
73
|
+
default: 0,
|
|
74
|
+
min: 0,
|
|
75
|
+
description: "In the currency it was sent in.",
|
|
76
|
+
}),
|
|
77
|
+
stickers: setting.boolean("Also for Super Stickers", { default: true }),
|
|
78
|
+
},
|
|
79
|
+
subscribe: ({ settings, fire }) => {
|
|
80
|
+
const check = (event: ChatData<"superChatEvent" | "superStickerEvent">) => {
|
|
81
|
+
if (event.amount >= settings.min && (settings.stickers || !event.sticker)) fire(event);
|
|
82
|
+
};
|
|
83
|
+
const stops = [on("superChatEvent", check), on("superStickerEvent", check)];
|
|
84
|
+
return () => {
|
|
85
|
+
for (const stop of stops) stop();
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const gift = plugin.trigger({
|
|
91
|
+
id: "gift",
|
|
92
|
+
name: "When someone sends a gift with Jewels",
|
|
93
|
+
description: "Gives event.user, gift, jewels and combo.",
|
|
94
|
+
settings: { min: setting.number("At least this many Jewels", { default: 1, min: 0 }) },
|
|
95
|
+
subscribe: ({ settings, fire }) =>
|
|
96
|
+
on("giftEvent", (event) => {
|
|
97
|
+
if (event.jewels >= settings.min) fire(event);
|
|
98
|
+
}),
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const member = plugin.trigger({
|
|
102
|
+
id: "member",
|
|
103
|
+
name: "When someone becomes a member",
|
|
104
|
+
description: "While you are live. Gives event.user, level and upgrade.",
|
|
105
|
+
settings: { upgrades: setting.boolean("Also when one changes to a higher level") },
|
|
106
|
+
subscribe: ({ settings, fire }) =>
|
|
107
|
+
on("newSponsorEvent", (event) => {
|
|
108
|
+
if (settings.upgrades || !event.upgrade) fire(event);
|
|
109
|
+
}),
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const milestone = plugin.trigger({
|
|
113
|
+
id: "milestone",
|
|
114
|
+
name: "When a member shares a milestone",
|
|
115
|
+
description: "Gives event.user, months, level and message.",
|
|
116
|
+
subscribe: ({ fire }) => on("memberMilestoneChatEvent", fire),
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const memberGift = plugin.trigger({
|
|
120
|
+
id: "member-gift",
|
|
121
|
+
name: "When someone gives memberships",
|
|
122
|
+
description: "Gives event.user, total and level.",
|
|
123
|
+
settings: { min: setting.number("At least this many", { default: 1, min: 1 }) },
|
|
124
|
+
subscribe: ({ settings, fire }) =>
|
|
125
|
+
on("membershipGiftingEvent", (event) => {
|
|
126
|
+
if (event.total >= settings.min) fire(event);
|
|
127
|
+
}),
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const pollEnded = plugin.trigger({
|
|
131
|
+
id: "poll-ended",
|
|
132
|
+
name: "When a chat poll ends",
|
|
133
|
+
description: "Gives event.question, winner (empty for a draw) and votes.",
|
|
134
|
+
subscribe: ({ fire }) =>
|
|
135
|
+
on("pollEvent", ({ closed, ...poll }) => {
|
|
136
|
+
if (closed) fire(poll);
|
|
137
|
+
}),
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const command = plugin.trigger({
|
|
141
|
+
id: "command",
|
|
142
|
+
name: "When someone writes a chat command",
|
|
143
|
+
description:
|
|
144
|
+
"Gives event.user, channelId, messageId, message, and text: what follows the command.",
|
|
145
|
+
settings: {
|
|
146
|
+
command: setting.text("Command", { default: "!deck", required: true }),
|
|
147
|
+
who: setting.select(
|
|
148
|
+
"Who can use it",
|
|
149
|
+
{
|
|
150
|
+
everyone: "Everyone",
|
|
151
|
+
member: "Members and above",
|
|
152
|
+
moderator: "Moderators and you",
|
|
153
|
+
owner: "Only you",
|
|
154
|
+
},
|
|
155
|
+
{ default: "moderator" },
|
|
156
|
+
),
|
|
157
|
+
},
|
|
158
|
+
subscribe: ({ settings, fire }) =>
|
|
159
|
+
on("textMessageEvent", (event) => {
|
|
160
|
+
const text = commandWords(event.message, settings.command);
|
|
161
|
+
if (text !== undefined && hasRole(event.role, settings.who)) fire({ ...event, text });
|
|
162
|
+
}),
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
export const triggers = [
|
|
166
|
+
online,
|
|
167
|
+
offline,
|
|
168
|
+
subscribe,
|
|
169
|
+
comment,
|
|
170
|
+
superChat,
|
|
171
|
+
gift,
|
|
172
|
+
member,
|
|
173
|
+
milestone,
|
|
174
|
+
memberGift,
|
|
175
|
+
pollEnded,
|
|
176
|
+
command,
|
|
177
|
+
];
|