/
guildVoiceChannel.ts
93 lines (79 loc) · 2.73 KB
/
guildVoiceChannel.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
import type { Client } from '../client/mod.ts'
import type {
GuildVoiceChannelPayload,
ModifyVoiceChannelOption,
ModifyVoiceChannelPayload
} from '../types/channel.ts'
import { CHANNEL } from '../types/endpoint.ts'
import { GuildChannel } from './channel.ts'
import type { Guild } from './guild.ts'
import { GuildChannelVoiceStatesManager } from '../managers/guildChannelVoiceStates.ts'
import type { User } from './user.ts'
import type { Member } from './member.ts'
import type {
VoiceChannelJoinOptions,
VoiceServerData
} from '../client/voice.ts'
import { Mixin } from '../../deps.ts'
import { TextChannel } from './textChannel.ts'
export class VoiceChannel extends Mixin(GuildChannel, TextChannel) {
bitrate!: string
userLimit!: number
voiceStates = new GuildChannelVoiceStatesManager(
this.client,
this.guild.voiceStates,
this
)
constructor(client: Client, data: GuildVoiceChannelPayload, guild: Guild) {
super(client, data, guild)
this.readFromData(data)
}
/** Join the Voice Channel */
async join(options?: VoiceChannelJoinOptions): Promise<VoiceServerData> {
return this.client.voice.join(this.id, options)
}
/** Leave the Voice Channel */
async leave(): Promise<void> {
return this.client.voice.leave(this.guild)
}
readFromData(data: GuildVoiceChannelPayload): void {
super.readFromData(data)
this.bitrate = data.bitrate ?? this.bitrate
this.userLimit = data.user_limit ?? this.userLimit
}
async edit(options?: ModifyVoiceChannelOption): Promise<VoiceChannel> {
const body: ModifyVoiceChannelPayload = {
name: options?.name,
position: options?.position,
permission_overwrites: options?.permissionOverwrites,
parent_id: options?.parentID,
bitrate: options?.bitrate,
user_limit: options?.userLimit
}
const resp = await this.client.rest.patch(CHANNEL(this.id), body)
return new VoiceChannel(this.client, resp, this.guild)
}
async setBitrate(rate: number | undefined): Promise<VoiceChannel> {
return await this.edit({ bitrate: rate })
}
async setUserLimit(limit: number | undefined): Promise<VoiceChannel> {
return await this.edit({ userLimit: limit })
}
async disconnectMember(
member: User | Member | string
): Promise<Member | undefined> {
const memberID = typeof member === 'string' ? member : member.id
const memberVoiceState = await this.voiceStates.get(memberID)
return memberVoiceState?.disconnect()
}
async disconnectAll(): Promise<Member[]> {
const members: Member[] = []
for await (const memberVoiceState of this.voiceStates) {
const member = await memberVoiceState.disconnect()
if (member !== undefined) {
members.push(member)
}
}
return members
}
}