pi-roundtable 0.48.0 → 0.49.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/CHANGELOG.md +15 -0
- package/README.md +1 -0
- package/docs/plugins.md +6 -5
- package/package.json +1 -1
- package/src/core/builtin/discord.ts +1 -0
- package/src/core/config/adapters.ts +1 -0
- package/src/core/contract/channel-context.ts +16 -0
- package/src/core/contract/surface.ts +5 -4
- package/src/core/discord/channel-context-settings.ts +105 -0
- package/src/core/discord/channel-context.ts +66 -102
- package/src/core/discord/context-reading.ts +60 -0
- package/src/core/discord/discord-surface.ts +13 -16
- package/src/core/discord/voice/voice-discord.ts +15 -0
- package/src/core/discord/voice/voice-hearing.ts +357 -0
- package/src/core/discord/voice/voice-link.ts +109 -13
- package/src/core/discord/voice/voice-service.ts +31 -3
- package/src/core/discord/voice/voice.ts +1 -1
- package/src/core/domain/block-support.ts +15 -9
- package/src/core/testing/fake-voice.ts +2 -0
|
@@ -36,15 +36,13 @@ import type { MemberJoinedEvent } from "../plugin-api/event-types.ts";
|
|
|
36
36
|
import { chunksFor } from "../presentation/reply-chunks.ts";
|
|
37
37
|
import { threadsOfHost } from "../threads/host-threads.ts";
|
|
38
38
|
import { DiscordAgentChannels, DiscordDashboard } from "./agent-discord.ts";
|
|
39
|
-
import {
|
|
40
|
-
type ContextSourceMessage,
|
|
41
|
-
contextSourceOf,
|
|
42
|
-
} from "./channel-context.ts";
|
|
39
|
+
import type { ContextSourceMessage } from "./channel-context.ts";
|
|
43
40
|
import { discordChannelExecutor } from "./channel-executor.ts";
|
|
44
41
|
import { channelInfoOf } from "./channel-info.ts";
|
|
45
42
|
import type { ChannelExecutor } from "./channel-operations.ts";
|
|
46
43
|
import type { ComposedCommands } from "./compose-commands.ts";
|
|
47
44
|
import type { ChannelInfo, DiscordConnection } from "./connection.ts";
|
|
45
|
+
import { readMessagesBefore, readThreadStarter } from "./context-reading.ts";
|
|
48
46
|
import { DiscordThreadHost } from "./dispatch-thread-host.ts";
|
|
49
47
|
import { DISCORD_FORMATTING, pickFormatting } from "./formatting.ts";
|
|
50
48
|
import { DEFAULT_FRESH_MARKER, postFreshMarker } from "./fresh-marker.ts";
|
|
@@ -202,22 +200,21 @@ export class DiscordSurface
|
|
|
202
200
|
* direct message or a channel that holds no messages. Throws while the connection is not ready
|
|
203
201
|
* or when Discord refuses, such as for a channel the bot cannot read.
|
|
204
202
|
*/
|
|
205
|
-
|
|
203
|
+
messagesBefore(
|
|
206
204
|
channelId: string,
|
|
207
205
|
before: string,
|
|
208
206
|
limit: number,
|
|
209
207
|
): Promise<ContextSourceMessage[] | undefined> {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
);
|
|
208
|
+
return readMessagesBefore(this.#client, channelId, before, limit);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* A thread's opening message, as channel context reads it; undefined for a channel that is not
|
|
213
|
+
* a thread and for a thread without one. Throws while the connection is not ready or when
|
|
214
|
+
* Discord refuses.
|
|
215
|
+
*/
|
|
216
|
+
threadStarter(channelId: string): Promise<ContextSourceMessage | undefined> {
|
|
217
|
+
return readThreadStarter(this.#client, channelId);
|
|
221
218
|
}
|
|
222
219
|
|
|
223
220
|
/** Reports deleted server channels by id; register before start. */
|
|
@@ -150,6 +150,18 @@ export function discordVoiceGateway(
|
|
|
150
150
|
}
|
|
151
151
|
});
|
|
152
152
|
},
|
|
153
|
+
onReady(listener) {
|
|
154
|
+
const ready = () => {
|
|
155
|
+
try {
|
|
156
|
+
listener();
|
|
157
|
+
} catch (error) {
|
|
158
|
+
logger.error({ err: error }, "voice gateway handler failed");
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
// Ready once the shard has its guilds again, so their voice states are the new ones.
|
|
162
|
+
client.on(Events.ShardReady, ready);
|
|
163
|
+
client.on(Events.ShardResume, ready);
|
|
164
|
+
},
|
|
153
165
|
async prepare() {
|
|
154
166
|
await loaded();
|
|
155
167
|
},
|
|
@@ -164,6 +176,9 @@ export function discordVoiceGateway(
|
|
|
164
176
|
decoder,
|
|
165
177
|
logger,
|
|
166
178
|
...(options.clock ? { clock: options.clock } : {}),
|
|
179
|
+
deafened: () =>
|
|
180
|
+
guild.voiceStates.cache.get(client.user?.id ?? "")?.serverDeaf ===
|
|
181
|
+
true,
|
|
167
182
|
});
|
|
168
183
|
},
|
|
169
184
|
};
|
|
@@ -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
|
+
}
|
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
} from "@discordjs/voice";
|
|
6
6
|
import type { Logger } from "../../kernel/log.ts";
|
|
7
7
|
import { systemClock, type VoiceClock } from "./voice-clock.ts";
|
|
8
|
+
import { hearingTaps, hearingWatch, networkingOf } from "./voice-hearing.ts";
|
|
8
9
|
import type {
|
|
9
10
|
VoiceLink,
|
|
10
11
|
VoiceLinkHandlers,
|
|
@@ -99,6 +100,11 @@ export interface VoiceLinkOptions {
|
|
|
99
100
|
logger: Logger;
|
|
100
101
|
/** What the rejoin attempts wait by. Default: the host's clock. */
|
|
101
102
|
clock?: VoiceClock;
|
|
103
|
+
/**
|
|
104
|
+
* Whether a moderator deafened the bot in the guild. Discord then sends it no audio, so a
|
|
105
|
+
* speaker it does not hear is no fault of the connection. Default: never.
|
|
106
|
+
*/
|
|
107
|
+
deafened?: () => boolean;
|
|
102
108
|
}
|
|
103
109
|
|
|
104
110
|
/**
|
|
@@ -152,8 +158,12 @@ function linkOver(
|
|
|
152
158
|
let closed = false;
|
|
153
159
|
/** The timer of the next rejoin, while the last one was refused. */
|
|
154
160
|
let rejoin: unknown;
|
|
155
|
-
/** Whether
|
|
156
|
-
let
|
|
161
|
+
/** Whether the connection is being asked for something right now, which it reports as a state change before it answers. */
|
|
162
|
+
let asking = false;
|
|
163
|
+
/** Whether the link itself left the channel to join it again, and the gateway has yet to report that leave. */
|
|
164
|
+
let leaving = false;
|
|
165
|
+
/** The networking the connection was last ready over: ready over it again is a resume, over another a new media path. */
|
|
166
|
+
let path: object | undefined;
|
|
157
167
|
|
|
158
168
|
const statusOf = (state: VoiceConnectionState): VoiceLinkStatus => {
|
|
159
169
|
if (state.status === Status.Ready) return "ready";
|
|
@@ -167,7 +177,7 @@ function linkOver(
|
|
|
167
177
|
*/
|
|
168
178
|
const recover = (state: VoiceConnectionState): void => {
|
|
169
179
|
// `rejoin()` reports its own outcome as a state change before it answers: that one is not a drop to recover from.
|
|
170
|
-
if (
|
|
180
|
+
if (asking) return;
|
|
171
181
|
clock.clearTimeout(rejoin);
|
|
172
182
|
rejoin = undefined;
|
|
173
183
|
if (closed || state.status !== Status.Disconnected) return;
|
|
@@ -176,17 +186,22 @@ function linkOver(
|
|
|
176
186
|
state.closeCode === DISCONNECTED;
|
|
177
187
|
if (refused) return;
|
|
178
188
|
let carried: boolean;
|
|
179
|
-
|
|
189
|
+
asking = true;
|
|
180
190
|
try {
|
|
181
|
-
|
|
191
|
+
// The link's own channel, which the connection no longer names once the link left it to join again.
|
|
192
|
+
carried = connection.rejoin({
|
|
193
|
+
channelId,
|
|
194
|
+
selfDeaf: false,
|
|
195
|
+
selfMute: true,
|
|
196
|
+
});
|
|
182
197
|
} finally {
|
|
183
|
-
|
|
198
|
+
asking = false;
|
|
184
199
|
}
|
|
185
200
|
if (carried) return;
|
|
186
201
|
rejoin = clock.setTimeout(() => retry(), REJOIN_MS);
|
|
187
202
|
};
|
|
188
203
|
|
|
189
|
-
/** A
|
|
204
|
+
/** A rejoin that throws, after the join itself, is a refused one: warned, no audio, asked for again. */
|
|
190
205
|
const retry = (): void => {
|
|
191
206
|
try {
|
|
192
207
|
recover(connection.state);
|
|
@@ -279,9 +294,74 @@ function linkOver(
|
|
|
279
294
|
});
|
|
280
295
|
};
|
|
281
296
|
|
|
297
|
+
/** What the connection is asked for a link that does not hear; a failure is warned and never thrown into the watch's timer. */
|
|
298
|
+
const attempt = (what: string, run: () => void): void => {
|
|
299
|
+
asking = true;
|
|
300
|
+
try {
|
|
301
|
+
run();
|
|
302
|
+
} catch (error) {
|
|
303
|
+
logger.warn(
|
|
304
|
+
{
|
|
305
|
+
guildId,
|
|
306
|
+
channelId,
|
|
307
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
308
|
+
},
|
|
309
|
+
`voice ${what} failed`,
|
|
310
|
+
);
|
|
311
|
+
} finally {
|
|
312
|
+
asking = false;
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
const watch = hearingWatch({
|
|
316
|
+
clock,
|
|
317
|
+
logger,
|
|
318
|
+
fields: { guildId, channelId },
|
|
319
|
+
person: (userId) => handlers?.person(userId) ?? false,
|
|
320
|
+
deafened: () => options.deafened?.() ?? false,
|
|
321
|
+
rebuild: () => {
|
|
322
|
+
const before = networkingOf(connection.state);
|
|
323
|
+
attempt("networking", () => connection.configureNetworking());
|
|
324
|
+
return networkingOf(connection.state) !== before;
|
|
325
|
+
},
|
|
326
|
+
rejoin: () => {
|
|
327
|
+
leaving = true;
|
|
328
|
+
// Leaves the channel; `retry` then finds the connection disconnected and asks for the channel again.
|
|
329
|
+
attempt("leave", () => connection.disconnect());
|
|
330
|
+
retry();
|
|
331
|
+
},
|
|
332
|
+
giveUp: () => {
|
|
333
|
+
attempt("leave", close);
|
|
334
|
+
// A connection that could not be destroyed reports nothing: the session ends all the same.
|
|
335
|
+
if (connection.state.status !== Status.Destroyed)
|
|
336
|
+
attempt("leave", () => handlers?.status("closed"));
|
|
337
|
+
},
|
|
338
|
+
});
|
|
339
|
+
const taps = hearingTaps(connection, watch);
|
|
340
|
+
/** The watch is told when the connection can hear: over a new media path, or the one it had. */
|
|
341
|
+
const heardOver = (
|
|
342
|
+
before: VoiceConnectionState,
|
|
343
|
+
state: VoiceConnectionState,
|
|
344
|
+
): void => {
|
|
345
|
+
if (state.status !== Status.Ready) {
|
|
346
|
+
if (before.status === Status.Ready) watch.down();
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
if (before.status === Status.Ready) return;
|
|
350
|
+
leaving = false;
|
|
351
|
+
const networking = networkingOf(state);
|
|
352
|
+
if (networking !== undefined && networking === path) {
|
|
353
|
+
watch.resumed();
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
path = networking;
|
|
357
|
+
watch.up();
|
|
358
|
+
};
|
|
359
|
+
|
|
282
360
|
connection.receiver.speaking.on("start", hear);
|
|
283
|
-
connection.on("stateChange", (
|
|
284
|
-
|
|
361
|
+
connection.on("stateChange", (before, state) => {
|
|
362
|
+
taps.follow(state);
|
|
363
|
+
heardOver(before, state);
|
|
364
|
+
retry();
|
|
285
365
|
if (state.status === Status.Destroyed) release();
|
|
286
366
|
handlers?.status(statusOf(state));
|
|
287
367
|
});
|
|
@@ -293,6 +373,8 @@ function linkOver(
|
|
|
293
373
|
if (closed) return;
|
|
294
374
|
closed = true;
|
|
295
375
|
clock.clearTimeout(rejoin);
|
|
376
|
+
watch.stop();
|
|
377
|
+
taps.stop();
|
|
296
378
|
connection.receiver.speaking.off("start", hear);
|
|
297
379
|
for (const stream of connection.receiver.subscriptions.values())
|
|
298
380
|
stream.destroy();
|
|
@@ -306,6 +388,19 @@ function linkOver(
|
|
|
306
388
|
);
|
|
307
389
|
};
|
|
308
390
|
|
|
391
|
+
const close = (): void => {
|
|
392
|
+
release();
|
|
393
|
+
// Tells Discord the bot left the channel; reports `closed` through the state change.
|
|
394
|
+
if (connection.state.status !== Status.Destroyed) connection.destroy();
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
taps.follow(connection.state);
|
|
398
|
+
// A connection the library already had ready is heard over the networking it has.
|
|
399
|
+
if (connection.state.status === Status.Ready) {
|
|
400
|
+
path = networkingOf(connection.state);
|
|
401
|
+
watch.up();
|
|
402
|
+
}
|
|
403
|
+
// The join's own first request is not caught: a connection whose setup throws is not joined.
|
|
309
404
|
recover(connection.state);
|
|
310
405
|
return {
|
|
311
406
|
status: () => (closed ? "closed" : statusOf(connection.state)),
|
|
@@ -313,10 +408,11 @@ function linkOver(
|
|
|
313
408
|
handlers = given;
|
|
314
409
|
},
|
|
315
410
|
forget,
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
411
|
+
leftItself: () => {
|
|
412
|
+
if (!leaving || closed) return false;
|
|
413
|
+
leaving = false;
|
|
414
|
+
return true;
|
|
320
415
|
},
|
|
416
|
+
close,
|
|
321
417
|
};
|
|
322
418
|
}
|