pi-roundtable 0.47.3 → 0.48.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/CHANGELOG.md +16 -0
- package/README.md +5 -0
- package/docs/plugins.md +57 -6
- package/package.json +6 -1
- package/src/core/builtin/discord.ts +41 -0
- package/src/core/config/adapters.ts +10 -0
- package/src/core/contract/surface.ts +5 -4
- package/src/core/discord/discord-surface.ts +4 -0
- package/src/core/discord/voice/voice-clock.ts +51 -0
- package/src/core/discord/voice/voice-discord.ts +185 -0
- package/src/core/discord/voice/voice-hearing.ts +357 -0
- package/src/core/discord/voice/voice-link.ts +418 -0
- package/src/core/discord/voice/voice-service.ts +336 -0
- package/src/core/discord/voice/voice-session.ts +230 -0
- package/src/core/discord/voice/voice.ts +144 -0
- package/src/core/domain/block-support.ts +15 -9
- package/src/core/host/define-roundtable.ts +1 -0
- package/src/core/testing/fake-discord.ts +7 -2
- package/src/core/testing/fake-voice.ts +229 -0
- package/src/core/testing/plugin-stand-ins.ts +27 -0
- package/src/core/testing/test-host.ts +5 -1
- package/src/discord/index.ts +14 -0
- package/src/testing.ts +7 -19
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import type { VoiceConnection, VoiceConnectionState } from "@discordjs/voice";
|
|
2
|
+
import type { Logger } from "../../kernel/log.ts";
|
|
3
|
+
import type { VoiceClock } from "./voice-clock.ts";
|
|
4
|
+
|
|
5
|
+
// Whether a voice link hears. A connection's state says nothing of its audio: after the library
|
|
6
|
+
// resumed a dropped voice socket, the connection is ready again over the media socket it had,
|
|
7
|
+
// and Discord's audio may no longer arrive there. Nothing but a datagram shows that it does, and
|
|
8
|
+
// silence shows nothing, so the watch goes by what contradicts silence: the voice socket naming
|
|
9
|
+
// someone who speaks, and a resume that cut into a channel that was being heard.
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* How long after the voice socket says someone speaks a datagram must have come. A speaker's
|
|
13
|
+
* audio is a datagram every 20 ms from that moment, so 2 s without one is a hundred missing in
|
|
14
|
+
* a row, which no jitter or loss explains; and 2 s is all a recorder loses before the remedy.
|
|
15
|
+
*/
|
|
16
|
+
const SPEAKING_PROOF_MS = 2_000;
|
|
17
|
+
/**
|
|
18
|
+
* How long after a resume a datagram must have come, in a channel that was heard shortly before.
|
|
19
|
+
* Nothing says anyone speaks then, so the pause between two sentences has to fit in it: 5 s
|
|
20
|
+
* keeps most pauses from being taken for deafness, and bounds what is lost when it is deafness.
|
|
21
|
+
*/
|
|
22
|
+
const RESUME_PROOF_MS = 5_000;
|
|
23
|
+
/**
|
|
24
|
+
* How long before its voice socket dropped a channel counts as heard. Whoever was speaking is
|
|
25
|
+
* cut off when the network goes, and the socket follows later: the library closes it after
|
|
26
|
+
* three missed heartbeats of some 14 s each, checked at the fourth, and a socket that cannot
|
|
27
|
+
* say goodbye takes up to 30 s more to close, 85 s in all. Two minutes span that, and a channel
|
|
28
|
+
* quiet for longer is left alone.
|
|
29
|
+
*/
|
|
30
|
+
const RECENT_MS = 120_000;
|
|
31
|
+
/**
|
|
32
|
+
* How long a media path is up before a speaking state counts as someone speaking. To a
|
|
33
|
+
* connection that just came up or resumed, Discord may repeat the states it sent before; those
|
|
34
|
+
* name speakers and say nothing of now.
|
|
35
|
+
*/
|
|
36
|
+
const SETTLE_MS = 3_000;
|
|
37
|
+
/** How long a remedy has to bring audio before the next one. */
|
|
38
|
+
const VERIFY_MS = 10_000;
|
|
39
|
+
/** How many datagrams of a speaker the voice socket never named are worth a line: two seconds of audio. */
|
|
40
|
+
const UNNAMED_DATAGRAMS = 100;
|
|
41
|
+
|
|
42
|
+
export interface HearingOptions {
|
|
43
|
+
clock: VoiceClock;
|
|
44
|
+
logger: Logger;
|
|
45
|
+
/** What every line of the watch names; never audio. */
|
|
46
|
+
fields: { guildId: string; channelId: string };
|
|
47
|
+
/** Whether the user is a person in the channel, not a bot; with no user, whether any person is. */
|
|
48
|
+
person(userId?: string): boolean;
|
|
49
|
+
/** Whether a moderator deafened the bot: Discord then sends it no audio, which no remedy mends. */
|
|
50
|
+
deafened(): boolean;
|
|
51
|
+
/** Sets the media path up again without leaving the channel; false when that could not be started. */
|
|
52
|
+
rebuild(): boolean;
|
|
53
|
+
/** Leaves the channel and joins it again. */
|
|
54
|
+
rejoin(): void;
|
|
55
|
+
/** Nothing made the link hear: it is closed. */
|
|
56
|
+
giveUp(): void;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** What a link tells the watch of its connection. */
|
|
60
|
+
export interface HearingWatch {
|
|
61
|
+
/** The connection is ready over a media path it did not have before. */
|
|
62
|
+
up(): void;
|
|
63
|
+
/** The connection is ready over the media path it had: its voice socket was resumed. */
|
|
64
|
+
resumed(): void;
|
|
65
|
+
/** The connection is no longer ready. */
|
|
66
|
+
down(): void;
|
|
67
|
+
/** The voice socket says the user is speaking. */
|
|
68
|
+
speaking(userId: string): void;
|
|
69
|
+
/**
|
|
70
|
+
* An audio datagram arrived; `named` is whether the voice socket said whose it is. Only a named
|
|
71
|
+
* one is heard: the receiver hands nobody the audio of a speaker it cannot name.
|
|
72
|
+
*/
|
|
73
|
+
audio(named: boolean): void;
|
|
74
|
+
stop(): void;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Watches whether a link hears, and remedies a link that does not: first the media path is set
|
|
79
|
+
* up again, which nobody in the channel notices; a speaker still not heard 10 s later has the
|
|
80
|
+
* bot leave the channel and join it again; a speaker not heard after that closes the link.
|
|
81
|
+
* A datagram of a speaker the voice socket named ends it all. Silence alone goes no further than the first remedy, and that one
|
|
82
|
+
* only after a resume: a quiet channel is never left, and never closed.
|
|
83
|
+
*/
|
|
84
|
+
export function hearingWatch(options: HearingOptions): HearingWatch {
|
|
85
|
+
const { clock, logger, fields } = options;
|
|
86
|
+
let ready = false;
|
|
87
|
+
let stopped = false;
|
|
88
|
+
/** When the media path came up or was resumed. */
|
|
89
|
+
let since = 0;
|
|
90
|
+
/** When the media path last carried audio of a speaker the voice socket named. */
|
|
91
|
+
let lastAudioAt: number | undefined;
|
|
92
|
+
/** How long the media path had carried none when the connection last stopped being ready; undefined when it never had. */
|
|
93
|
+
let quietAtDrop: number | undefined;
|
|
94
|
+
/** Whether audio came while the connection was not ready: the media path never stopped. */
|
|
95
|
+
let heardAway = false;
|
|
96
|
+
/** The remedies used since audio was last heard: 1 the media path, 2 the rejoin. */
|
|
97
|
+
let remedies: 0 | 1 | 2 = 0;
|
|
98
|
+
/** Whether someone was said to speak and was not heard, since audio was last heard. */
|
|
99
|
+
let evidenced = false;
|
|
100
|
+
/** Datagrams of speakers the voice socket has not named, since the media path came up or was resumed. */
|
|
101
|
+
let unnamed = 0;
|
|
102
|
+
/** When someone was first said to speak while the first remedy is waited on: its 10 s are not cut short for them. */
|
|
103
|
+
let saidAt: number | undefined;
|
|
104
|
+
/** The one deadline that runs: for a datagram after a speaking state or a resume, or after a remedy. */
|
|
105
|
+
let timer: unknown;
|
|
106
|
+
let waitsFor: "speaking" | "resume" | "remedy" | undefined;
|
|
107
|
+
|
|
108
|
+
const disarm = (): void => {
|
|
109
|
+
clock.clearTimeout(timer);
|
|
110
|
+
timer = undefined;
|
|
111
|
+
waitsFor = undefined;
|
|
112
|
+
};
|
|
113
|
+
const arm = (what: NonNullable<typeof waitsFor>, ms: number): void => {
|
|
114
|
+
disarm();
|
|
115
|
+
waitsFor = what;
|
|
116
|
+
timer = clock.setTimeout(() => due(what), ms);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/** The next remedy, of a link that does not hear. */
|
|
120
|
+
const remedy = (after: "speaking" | "resume"): void => {
|
|
121
|
+
if (remedies === 0) {
|
|
122
|
+
remedies = 1;
|
|
123
|
+
if (options.rebuild()) {
|
|
124
|
+
logger.warn(
|
|
125
|
+
{ ...fields, after },
|
|
126
|
+
"voice hears nothing: its networking is set up again",
|
|
127
|
+
);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
// Without word of a speaker the channel is not left: the next one who is not heard takes it from here.
|
|
131
|
+
if (!evidenced) {
|
|
132
|
+
logger.warn(
|
|
133
|
+
{ ...fields, after },
|
|
134
|
+
"voice hears nothing, and its networking could not be set up again",
|
|
135
|
+
);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (remedies === 1) {
|
|
140
|
+
remedies = 2;
|
|
141
|
+
logger.warn(
|
|
142
|
+
fields,
|
|
143
|
+
"voice hears nothing: the channel is left and joined again",
|
|
144
|
+
);
|
|
145
|
+
options.rejoin();
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
logger.warn(fields, "voice audio not recovered");
|
|
149
|
+
options.giveUp();
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const due = (what: NonNullable<typeof waitsFor>): void => {
|
|
153
|
+
timer = undefined;
|
|
154
|
+
waitsFor = undefined;
|
|
155
|
+
// A deafened bot was sent nothing to hear: its silence shows no fault.
|
|
156
|
+
if (stopped || !ready || options.deafened()) return;
|
|
157
|
+
if (what === "speaking") {
|
|
158
|
+
evidenced = true;
|
|
159
|
+
remedy("speaking");
|
|
160
|
+
} else if (what === "resume") remedy("resume");
|
|
161
|
+
// A remedy brought no audio in its time: the next one is for a speaker, never for silence.
|
|
162
|
+
else if (remedies === 1 && (evidenced || saidAt !== undefined)) {
|
|
163
|
+
// Someone named while it was waited on has their own 2 s to be heard, like anyone.
|
|
164
|
+
const left =
|
|
165
|
+
evidenced || saidAt === undefined
|
|
166
|
+
? 0
|
|
167
|
+
: SPEAKING_PROOF_MS - (clock.now() - saidAt);
|
|
168
|
+
saidAt = undefined;
|
|
169
|
+
if (left > 0) arm("speaking", left);
|
|
170
|
+
else {
|
|
171
|
+
evidenced = true;
|
|
172
|
+
remedy("speaking");
|
|
173
|
+
}
|
|
174
|
+
} else
|
|
175
|
+
logger.warn(
|
|
176
|
+
{ ...fields, remedies },
|
|
177
|
+
"voice has heard nothing since its remedy",
|
|
178
|
+
);
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
up() {
|
|
183
|
+
ready = true;
|
|
184
|
+
since = clock.now();
|
|
185
|
+
lastAudioAt = undefined;
|
|
186
|
+
unnamed = 0;
|
|
187
|
+
saidAt = undefined;
|
|
188
|
+
if (remedies > 0) arm("remedy", VERIFY_MS);
|
|
189
|
+
},
|
|
190
|
+
resumed() {
|
|
191
|
+
ready = true;
|
|
192
|
+
since = clock.now();
|
|
193
|
+
unnamed = 0;
|
|
194
|
+
if (remedies > 0) {
|
|
195
|
+
arm("remedy", VERIFY_MS);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const heard = quietAtDrop !== undefined && quietAtDrop <= RECENT_MS;
|
|
199
|
+
if (heard && !heardAway && options.person())
|
|
200
|
+
arm("resume", RESUME_PROOF_MS);
|
|
201
|
+
},
|
|
202
|
+
down() {
|
|
203
|
+
if (ready) {
|
|
204
|
+
quietAtDrop =
|
|
205
|
+
lastAudioAt === undefined ? undefined : clock.now() - lastAudioAt;
|
|
206
|
+
heardAway = false;
|
|
207
|
+
}
|
|
208
|
+
ready = false;
|
|
209
|
+
saidAt = undefined;
|
|
210
|
+
disarm();
|
|
211
|
+
},
|
|
212
|
+
speaking(userId) {
|
|
213
|
+
if (stopped || !ready || waitsFor === "speaking") return;
|
|
214
|
+
if (clock.now() - since < SETTLE_MS || !options.person(userId)) return;
|
|
215
|
+
if (options.deafened()) return;
|
|
216
|
+
const now = clock.now();
|
|
217
|
+
// The word of a speaker can come after their audio: the voice socket resends what it lost, a
|
|
218
|
+
// datagram is never resent. Whoever was heard this shortly before is no evidence of deafness.
|
|
219
|
+
if (lastAudioAt !== undefined && now - lastAudioAt < SPEAKING_PROOF_MS)
|
|
220
|
+
return;
|
|
221
|
+
// The first remedy keeps its time: the speaker is weighed when it is over.
|
|
222
|
+
if (waitsFor === "remedy" && remedies === 1) {
|
|
223
|
+
saidAt ??= now;
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
arm("speaking", SPEAKING_PROOF_MS);
|
|
227
|
+
},
|
|
228
|
+
audio(named) {
|
|
229
|
+
// Audio nobody is handed is not hearing: it ends no deadline and no remedy.
|
|
230
|
+
if (!named) {
|
|
231
|
+
if (++unnamed === UNNAMED_DATAGRAMS)
|
|
232
|
+
logger.warn(
|
|
233
|
+
{ ...fields, datagrams: unnamed },
|
|
234
|
+
"voice audio arrives from a speaker the voice socket has not named",
|
|
235
|
+
);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
lastAudioAt = clock.now();
|
|
239
|
+
if (!ready) heardAway = true;
|
|
240
|
+
saidAt = undefined;
|
|
241
|
+
if (timer !== undefined) disarm();
|
|
242
|
+
if (remedies === 0) return;
|
|
243
|
+
logger.info({ ...fields, remedies }, "voice hears again");
|
|
244
|
+
remedies = 0;
|
|
245
|
+
evidenced = false;
|
|
246
|
+
},
|
|
247
|
+
stop() {
|
|
248
|
+
stopped = true;
|
|
249
|
+
disarm();
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** The voice opcode of someone's speaking state. */
|
|
255
|
+
const SPEAKING = 5;
|
|
256
|
+
/** The flag of a speaking state that says the microphone is sending; the others are for sound shared with a video and for priority. */
|
|
257
|
+
const MICROPHONE = 1;
|
|
258
|
+
/** The RTP payload type of Opus, as Discord sends audio. */
|
|
259
|
+
const OPUS = 120;
|
|
260
|
+
|
|
261
|
+
/** An event source of @discordjs/voice: a networking, its voice socket, its media socket. */
|
|
262
|
+
interface Source {
|
|
263
|
+
on(event: string, listener: (...given: never[]) => void): unknown;
|
|
264
|
+
off(event: string, listener: (...given: never[]) => void): unknown;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** A member of a state the library types by its status: `networking` of a connection's, `ws` and `udp` of a networking's. */
|
|
268
|
+
function partOf(state: object | undefined, key: string): Source | undefined {
|
|
269
|
+
const part: unknown = state ? Reflect.get(state, key) : undefined;
|
|
270
|
+
return typeof part === "object" && part !== null
|
|
271
|
+
? (part as Source)
|
|
272
|
+
: undefined;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** The networking a connection's state holds: a resume keeps it, a new media path is a new one. */
|
|
276
|
+
export function networkingOf(state: VoiceConnectionState): object | undefined {
|
|
277
|
+
return partOf(state, "networking");
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Whether a datagram on the media socket is audio: RTP version 2 that carries Opus, which neither a keep-alive, an IP discovery nor RTCP is. */
|
|
281
|
+
function isAudio(message: Uint8Array): boolean {
|
|
282
|
+
return (
|
|
283
|
+
message.length > 12 &&
|
|
284
|
+
(message[0] ?? 0) >> 6 === 2 &&
|
|
285
|
+
((message[1] ?? 0) & 0x7f) === OPUS
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Hands the watch what a connection's sockets carry: each speaking state of the voice socket and
|
|
291
|
+
* each audio datagram of the media socket. The library replaces the voice socket on a resume and
|
|
292
|
+
* both with a new networking, so `follow` is called with each state of the connection and the
|
|
293
|
+
* sockets are followed from there, as the library's own receiver follows them.
|
|
294
|
+
*/
|
|
295
|
+
export function hearingTaps(
|
|
296
|
+
connection: VoiceConnection,
|
|
297
|
+
watch: HearingWatch,
|
|
298
|
+
): { follow(state: VoiceConnectionState): void; stop(): void } {
|
|
299
|
+
let networking: Source | undefined;
|
|
300
|
+
let voice: Source | undefined;
|
|
301
|
+
let media: Source | undefined;
|
|
302
|
+
const packet = (given: {
|
|
303
|
+
op?: unknown;
|
|
304
|
+
d?: { user_id?: unknown; speaking?: unknown } | null;
|
|
305
|
+
}): void => {
|
|
306
|
+
if (given.op !== SPEAKING || !given.d) return;
|
|
307
|
+
const { user_id: userId, speaking } = given.d;
|
|
308
|
+
if (typeof userId !== "string" || typeof speaking !== "number") return;
|
|
309
|
+
if (speaking & MICROPHONE) watch.speaking(userId);
|
|
310
|
+
};
|
|
311
|
+
const datagram = (message: Uint8Array): void => {
|
|
312
|
+
if (!isAudio(message)) return;
|
|
313
|
+
const ssrc = new DataView(
|
|
314
|
+
message.buffer,
|
|
315
|
+
message.byteOffset,
|
|
316
|
+
message.byteLength,
|
|
317
|
+
).getUint32(8);
|
|
318
|
+
// A stand-in receiver keeps no such map: its audio is its speakers'.
|
|
319
|
+
const named = connection.receiver.ssrcMap;
|
|
320
|
+
watch.audio(!named || named.get(ssrc) !== undefined);
|
|
321
|
+
};
|
|
322
|
+
const bind = (): void => {
|
|
323
|
+
const state: unknown = networking
|
|
324
|
+
? Reflect.get(networking, "state")
|
|
325
|
+
: undefined;
|
|
326
|
+
const sockets = typeof state === "object" && state ? state : undefined;
|
|
327
|
+
const nextVoice = partOf(sockets, "ws");
|
|
328
|
+
const nextMedia = partOf(sockets, "udp");
|
|
329
|
+
if (nextVoice !== voice) {
|
|
330
|
+
voice?.off("packet", packet);
|
|
331
|
+
nextVoice?.on("packet", packet);
|
|
332
|
+
voice = nextVoice;
|
|
333
|
+
}
|
|
334
|
+
if (nextMedia !== media) {
|
|
335
|
+
media?.off("message", datagram);
|
|
336
|
+
nextMedia?.on("message", datagram);
|
|
337
|
+
media = nextMedia;
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
return {
|
|
341
|
+
follow(state) {
|
|
342
|
+
const next = partOf(state, "networking");
|
|
343
|
+
if (next !== networking) {
|
|
344
|
+
networking?.off("stateChange", bind);
|
|
345
|
+
next?.on("stateChange", bind);
|
|
346
|
+
networking = next;
|
|
347
|
+
}
|
|
348
|
+
bind();
|
|
349
|
+
},
|
|
350
|
+
stop() {
|
|
351
|
+
networking?.off("stateChange", bind);
|
|
352
|
+
voice?.off("packet", packet);
|
|
353
|
+
media?.off("message", datagram);
|
|
354
|
+
networking = voice = media = undefined;
|
|
355
|
+
},
|
|
356
|
+
};
|
|
357
|
+
}
|