This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
/
DataStore.ts
62 lines (55 loc) · 1.93 KB
/
DataStore.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
import { GatewayDispatchEvents, GatewayOPCodes, GatewayVoiceServerUpdateDispatch, GatewayVoiceStateUpdateDispatch } from 'discord-api-types/v8/gateway';
import { Client, Constants, Guild } from 'discord.js';
import { VoiceConnection } from './VoiceConnection';
// Clients
const clients: Set<Client> = new Set();
export function trackClient(client: Client) {
if (clients.has(client)) {
return;
}
clients.add(client);
client.on(Constants.Events.RAW, payload => {
if (payload.t === GatewayDispatchEvents.VoiceServerUpdate) {
const packet: GatewayVoiceServerUpdateDispatch = payload;
getVoiceConnection(packet.d.guild_id)?.addServerPacket(packet);
} else if (payload.t === GatewayDispatchEvents.VoiceStateUpdate) {
const packet: GatewayVoiceStateUpdateDispatch = payload;
if (packet.d.guild_id && packet.d.session_id && packet.d.user_id === client.user?.id) {
getVoiceConnection(packet.d.guild_id)?.addStatePacket(packet);
}
}
});
}
export interface JoinConfig {
guild: Guild;
channelId: string|null;
selfDeaf: boolean;
selfMute: boolean;
}
/**
* Sends a voice state update to the main websocket shard of a guild, to indicate joining/leaving/moving across
* voice channels
* @param voiceChannel The voice channel to move to
*/
export function signalJoinVoiceChannel(config: JoinConfig) {
return config.guild.shard.send({
op: GatewayOPCodes.VoiceStateUpdate,
d: {
guild_id: config.guild.id,
channel_id: config.channelId,
self_deaf: config.selfDeaf,
self_mute: config.selfMute
}
});
}
// Voice Connections
const voiceConnections: Map<string, VoiceConnection> = new Map();
export function getVoiceConnection(guildId: string) {
return voiceConnections.get(guildId);
}
export function untrackVoiceConnection(guildId: string) {
return voiceConnections.delete(guildId);
}
export function trackVoiceConnection(guildId: string, voiceConnection: VoiceConnection) {
return voiceConnections.set(guildId, voiceConnection);
}