-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
DisTubeVoice.ts
275 lines (274 loc) · 8.95 KB
/
DisTubeVoice.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { Constants } from "discord.js";
import { TypedEmitter } from "tiny-typed-emitter";
import { DisTubeError, isSupportedVoiceChannel } from "..";
import {
AudioPlayerStatus,
VoiceConnectionDisconnectReason,
VoiceConnectionStatus,
createAudioPlayer,
createAudioResource,
entersState,
joinVoiceChannel,
} from "@discordjs/voice";
import type { Snowflake, VoiceBasedChannel, VoiceState } from "discord.js";
import type { DisTubeStream, DisTubeVoiceEvents, DisTubeVoiceManager } from "..";
import type { AudioPlayer, AudioResource, VoiceConnection } from "@discordjs/voice";
/**
* Create a voice connection to the voice channel
*/
export class DisTubeVoice extends TypedEmitter<DisTubeVoiceEvents> {
readonly id: Snowflake;
readonly voices: DisTubeVoiceManager;
readonly audioPlayer: AudioPlayer;
connection!: VoiceConnection;
audioResource?: AudioResource;
emittedError!: boolean;
isDisconnected = false;
stream?: DisTubeStream;
#channel!: VoiceBasedChannel;
#volume = 100;
constructor(voiceManager: DisTubeVoiceManager, channel: VoiceBasedChannel) {
super();
/**
* The voice manager that instantiated this connection
*/
this.voices = voiceManager;
this.id = channel.guildId;
this.channel = channel;
this.voices.add(this.id, this);
this.audioPlayer = createAudioPlayer()
.on(AudioPlayerStatus.Idle, oldState => {
if (oldState.status !== AudioPlayerStatus.Idle) {
delete this.audioResource;
this.emit("finish");
}
})
.on(AudioPlayerStatus.Playing, () => this.#br())
.on("error", (error: NodeJS.ErrnoException) => {
if (this.emittedError) return;
this.emittedError = true;
this.emit("error", error);
});
this.connection
.on(VoiceConnectionStatus.Disconnected, (_, newState) => {
if (newState.reason === VoiceConnectionDisconnectReason.Manual) {
// User disconnect
this.leave();
} else if (newState.reason === VoiceConnectionDisconnectReason.WebSocketClose && newState.closeCode === 4014) {
// Move to other channel
entersState(this.connection, VoiceConnectionStatus.Connecting, 5e3).catch(() => {
if (
![VoiceConnectionStatus.Ready, VoiceConnectionStatus.Connecting].includes(this.connection.state.status)
) {
this.leave();
}
});
} else if (this.connection.rejoinAttempts < 5) {
// Try to rejoin
setTimeout(
() => {
this.connection.rejoin();
},
(this.connection.rejoinAttempts + 1) * 5e3,
).unref();
} else if (this.connection.state.status !== VoiceConnectionStatus.Destroyed) {
// Leave after 5 attempts
this.leave(new DisTubeError("VOICE_RECONNECT_FAILED"));
}
})
.on(VoiceConnectionStatus.Destroyed, () => {
this.leave();
})
.on("error", () => undefined);
this.connection.subscribe(this.audioPlayer);
/**
* Get or set the volume percentage
*/
}
#br() {
if (this.audioResource?.encoder?.encoder) this.audioResource.encoder.setBitrate(this.channel.bitrate);
}
/**
* The voice channel id the bot is in
*/
get channelId() {
return this.connection?.joinConfig?.channelId ?? undefined;
}
get channel() {
if (!this.channelId) return this.#channel;
if (this.#channel?.id === this.channelId) return this.#channel;
const channel = this.voices.client.channels.cache.get(this.channelId);
if (!channel) return this.#channel;
for (const type of Constants.VoiceBasedChannelTypes) {
if (channel.type === type) {
this.#channel = channel;
return channel;
}
}
return this.#channel;
}
set channel(channel: VoiceBasedChannel) {
if (!isSupportedVoiceChannel(channel)) {
throw new DisTubeError("INVALID_TYPE", "BaseGuildVoiceChannel", channel, "DisTubeVoice#channel");
}
if (channel.guildId !== this.id) throw new DisTubeError("VOICE_DIFFERENT_GUILD");
if (channel.client.user?.id !== this.voices.client.user?.id) throw new DisTubeError("VOICE_DIFFERENT_CLIENT");
if (channel.id === this.channelId) return;
if (!channel.joinable) {
if (channel.full) throw new DisTubeError("VOICE_FULL");
else throw new DisTubeError("VOICE_MISSING_PERMS");
}
this.connection = this.#join(channel);
this.#channel = channel;
this.#br();
}
#join(channel: VoiceBasedChannel) {
return joinVoiceChannel({
channelId: channel.id,
guildId: this.id,
adapterCreator: channel.guild.voiceAdapterCreator,
group: channel.client.user?.id,
});
}
/**
* Join a voice channel with this connection
*
* @param channel - A voice channel
*/
async join(channel?: VoiceBasedChannel): Promise<DisTubeVoice> {
const TIMEOUT = 30e3;
if (channel) this.channel = channel;
try {
await entersState(this.connection, VoiceConnectionStatus.Ready, TIMEOUT);
} catch {
if (this.connection.state.status === VoiceConnectionStatus.Ready) return this;
if (this.connection.state.status !== VoiceConnectionStatus.Destroyed) this.connection.destroy();
this.voices.remove(this.id);
throw new DisTubeError("VOICE_CONNECT_FAILED", TIMEOUT / 1e3);
}
return this;
}
/**
* Leave the voice channel of this connection
*
* @param error - Optional, an error to emit with 'error' event.
*/
leave(error?: Error) {
this.stop(true);
if (!this.isDisconnected) {
this.emit("disconnect", error);
this.isDisconnected = true;
}
if (this.connection.state.status !== VoiceConnectionStatus.Destroyed) this.connection.destroy();
this.voices.remove(this.id);
}
/**
* Stop the playing stream
*
* @param force - If true, will force the {@link DisTubeVoice#audioPlayer} to enter the Idle state even
* if the {@link DisTubeVoice#audioResource} has silence padding frames.
*/
stop(force = false) {
this.audioPlayer.stop(force);
this.stream?.kill?.();
}
/**
* Play a {@link DisTubeStream}
*
* @param dtStream - DisTubeStream
*/
play(dtStream: DisTubeStream) {
this.emittedError = false;
dtStream.on("error", (error: NodeJS.ErrnoException) => {
if (this.emittedError || error.code === "ERR_STREAM_PREMATURE_CLOSE") return;
this.emittedError = true;
this.emit("error", error);
});
this.audioResource = createAudioResource(dtStream.stream, {
inputType: dtStream.type,
inlineVolume: true,
});
this.volume = this.#volume;
if (this.audioPlayer.state.status !== AudioPlayerStatus.Paused) this.audioPlayer.play(this.audioResource);
this.stream?.kill?.();
this.stream = dtStream;
}
set volume(volume: number) {
if (typeof volume !== "number" || isNaN(volume)) {
throw new DisTubeError("INVALID_TYPE", "number", volume, "volume");
}
if (volume < 0) {
throw new DisTubeError("NUMBER_COMPARE", "Volume", "bigger or equal to", 0);
}
this.#volume = volume;
this.audioResource?.volume?.setVolume(Math.pow(this.#volume / 100, 0.5 / Math.log10(2)));
}
get volume() {
return this.#volume;
}
/**
* Playback duration of the audio resource in seconds
*/
get playbackDuration() {
return (this.audioResource?.playbackDuration ?? 0) / 1000;
}
pause() {
this.audioPlayer.pause();
}
unpause() {
const state = this.audioPlayer.state;
if (state.status !== AudioPlayerStatus.Paused) return;
if (this.audioResource && state.resource !== this.audioResource) this.audioPlayer.play(this.audioResource);
else this.audioPlayer.unpause();
}
/**
* Whether the bot is self-deafened
*/
get selfDeaf(): boolean {
return this.connection.joinConfig.selfDeaf;
}
/**
* Whether the bot is self-muted
*/
get selfMute(): boolean {
return this.connection.joinConfig.selfMute;
}
/**
* Self-deafens/undeafens the bot.
*
* @param selfDeaf - Whether or not the bot should be self-deafened
*
* @returns true if the voice state was successfully updated, otherwise false
*/
setSelfDeaf(selfDeaf: boolean): boolean {
if (typeof selfDeaf !== "boolean") {
throw new DisTubeError("INVALID_TYPE", "boolean", selfDeaf, "selfDeaf");
}
return this.connection.rejoin({
...this.connection.joinConfig,
selfDeaf,
});
}
/**
* Self-mutes/unmutes the bot.
*
* @param selfMute - Whether or not the bot should be self-muted
*
* @returns true if the voice state was successfully updated, otherwise false
*/
setSelfMute(selfMute: boolean): boolean {
if (typeof selfMute !== "boolean") {
throw new DisTubeError("INVALID_TYPE", "boolean", selfMute, "selfMute");
}
return this.connection.rejoin({
...this.connection.joinConfig,
selfMute,
});
}
/**
* The voice state of this connection
*/
get voiceState(): VoiceState | undefined {
return this.channel?.guild?.members?.me?.voice;
}
}