detritus-client
Version:
A Typescript NodeJS library to interact with Discord's API, both Rest and Gateway.
118 lines (117 loc) • 4.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VoiceCall = void 0;
const basecollection_1 = require("../collections/basecollection");
const baseset_1 = require("../collections/baseset");
const constants_1 = require("../constants");
const basestructure_1 = require("./basestructure");
const voicestate_1 = require("./voicestate");
const keysVoiceCall = new baseset_1.BaseSet([
constants_1.DiscordKeys.CHANNEL_ID,
constants_1.DiscordKeys.MESSAGE_ID,
constants_1.DiscordKeys.REGION,
constants_1.DiscordKeys.RINGING,
constants_1.DiscordKeys.UNAVAILABLE,
constants_1.DiscordKeys.VOICE_STATES,
]);
const keysMergeVoiceCall = new baseset_1.BaseSet([
constants_1.DiscordKeys.VOICE_STATES,
]);
/**
* VoiceCall Structure
* a DM Channel's call
* (non-bots only)
* @category Structure
*/
class VoiceCall extends basestructure_1.BaseStructure {
constructor(client, data, isClone) {
super(client, undefined, isClone);
this._keys = keysVoiceCall;
this._keysMerge = keysMergeVoiceCall;
this.ringing = new basecollection_1.BaseCollection();
this.channelId = '';
this.messageId = '';
this.region = '';
this.unavailable = false;
this.merge(data);
}
get amBeingRinged() {
if (this.client.user) {
return this.isRinging(this.client.user.id);
}
return false;
}
get channel() {
return this.client.channels.get(this.channelId) || null;
}
get joined() {
return this.client.voiceConnections.has(this.channelId);
}
get voiceConnection() {
return this.client.voiceConnections.get(this.channelId) || null;
}
get voiceStates() {
if (this.client.voiceStates.has(this.channelId)) {
return this.client.voiceStates.get(this.channelId);
}
return new basecollection_1.BaseCollection();
}
isRinging(userId) {
return this.ringing.has(userId);
}
kill() {
if (this.joined) {
const connection = this.client.voiceConnections.get(this.channelId);
connection.kill();
}
}
join(options) {
return this.client.voiceConnect(undefined, this.channelId, options);
}
startRinging(recipients) {
return this.client.rest.startChannelCallRinging(this.channelId, { recipients });
}
stopRinging(recipients) {
return this.client.rest.stopChannelCallRinging(this.channelId, { recipients });
}
mergeValue(key, value) {
if (value !== undefined) {
switch (key) {
case constants_1.DiscordKeys.RINGING:
{
this.ringing.clear();
for (let userId of value) {
if (this.client.users.has(userId)) {
this.ringing.set(userId, this.client.users.get(userId));
}
else {
this.ringing.set(userId, null);
}
}
}
;
return;
case constants_1.DiscordKeys.VOICE_STATES:
{
// cannot clone the voice states since it's not stored on the object
if (this.client.voiceStates.enabled && !this.isClone) {
const cache = this.client.voiceStates.insertCache(this.channelId);
cache.clear();
for (let raw of value) {
if (this.client.voiceStates.has(this.channelId, raw.user_id)) {
this.client.voiceStates.get(this.channelId, raw.user_id).merge(raw);
}
else {
this.client.voiceStates.insert(new voicestate_1.VoiceState(this.client, raw));
}
}
}
}
;
return;
}
return super.mergeValue(key, value);
}
}
}
exports.VoiceCall = VoiceCall;