-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
DisTubeVoiceManager.ts
60 lines (59 loc) · 1.67 KB
/
DisTubeVoiceManager.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
import { DisTubeVoice } from "../DisTubeVoice";
import { resolveGuildId } from "../..";
import { GuildIdManager } from ".";
import { VoiceConnectionStatus, getVoiceConnection } from "@discordjs/voice";
import type { GuildIdResolvable } from "../..";
import type { VoiceBasedChannel } from "discord.js";
/**
* Manages voice connections for {@link DisTube}
*/
export class DisTubeVoiceManager extends GuildIdManager<DisTubeVoice> {
/**
* Get a {@link DisTubeVoice}.
*
* @param guild - The queue resolvable to resolve
*/
/**
* Collection of {@link DisTubeVoice}.
*/
/**
* Create a {@link DisTubeVoice}
*
* @param channel - A voice channel to join
*/
create(channel: VoiceBasedChannel): DisTubeVoice {
const existing = this.get(channel.guildId);
if (existing) {
existing.channel = channel;
return existing;
}
return new DisTubeVoice(this, channel);
}
/**
* Join a voice channel
*
* @param channel - A voice channel to join
*/
join(channel: VoiceBasedChannel): Promise<DisTubeVoice> {
const existing = this.get(channel.guildId);
if (existing) return existing.join(channel);
return this.create(channel).join();
}
/**
* Leave the connected voice channel in a guild
*
* @param guild - Queue Resolvable
*/
leave(guild: GuildIdResolvable) {
const voice = this.get(guild);
if (voice) {
voice.leave();
} else {
const connection =
getVoiceConnection(resolveGuildId(guild), this.client.user?.id) ?? getVoiceConnection(resolveGuildId(guild));
if (connection && connection.state.status !== VoiceConnectionStatus.Destroyed) {
connection.destroy();
}
}
}
}