UNPKG

detritus-client

Version:

A Typescript NodeJS library to interact with Discord's API, both Rest and Gateway.

165 lines (164 loc) 5.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VoiceState = void 0; const baseset_1 = require("../collections/baseset"); const constants_1 = require("../constants"); const basestructure_1 = require("./basestructure"); const member_1 = require("./member"); const keysVoiceState = new baseset_1.BaseSet([ constants_1.DiscordKeys.CHANNEL_ID, constants_1.DiscordKeys.DEAF, constants_1.DiscordKeys.GUILD_ID, constants_1.DiscordKeys.MEMBER, constants_1.DiscordKeys.MUTE, constants_1.DiscordKeys.REQUEST_TO_SPEAK_TIMESTAMP, constants_1.DiscordKeys.SELF_DEAF, constants_1.DiscordKeys.SELF_MUTE, constants_1.DiscordKeys.SELF_STREAM, constants_1.DiscordKeys.SELF_VIDEO, constants_1.DiscordKeys.SESSION_ID, constants_1.DiscordKeys.SUPPRESS, constants_1.DiscordKeys.USER_ID, ]); const keysMergeVoiceState = new baseset_1.BaseSet([ constants_1.DiscordKeys.GUILD_ID, ]); const keysSkipDifferenceVoiceState = new baseset_1.BaseSet([ constants_1.DiscordKeys.MEMBER, ]); /** * Voice State Structure * @category Structure */ class VoiceState extends basestructure_1.BaseStructure { constructor(client, data, isClone) { super(client, undefined, isClone); this._keys = keysVoiceState; this._keysMerge = keysMergeVoiceState; this._keysSkipDifference = keysSkipDifferenceVoiceState; this._isSpeaking = false; this.deaf = false; this.member = null; this.mute = false; this.requestToSpeakTimestampUnix = 0; this.selfDeaf = false; this.selfMute = false; this.selfStream = false; this.selfVideo = false; this.sessionId = ''; this.suppress = false; this.userId = ''; Object.defineProperties(this, { _isSpeaking: { enumerable: false, writable: true }, }); this.merge(data); } get channel() { if (this.channelId && this.client.channels.has(this.channelId)) { return this.client.channels.get(this.channelId); } return null; } get guild() { if (this.guildId) { return this.client.guilds.get(this.guildId) || null; } return null; } get isAudience() { return this.suppress || !!this.requestToSpeakTimestampUnix; } get isSpeaker() { return !this.suppress && !this.requestToSpeakTimestampUnix; } get isSpeaking() { return this._isSpeaking; } get requestToSpeakTimestamp() { if (this.requestToSpeakTimestampUnix) { return new Date(this.requestToSpeakTimestampUnix); } return null; } get serverId() { return this.guildId || this.channelId || ''; } get streamKey() { if (this.guildId) { return `guild:${this.guildId}:${this.channelId}:${this.userId}`; } return ''; } async fetchStreamPreview() { if (!this.guildId) { throw new Error('Stream Previews are unable in a DM call.'); } if (!this.selfStream) { throw new Error('User is not streaming'); } return this.client.rest.fetchStreamPreview(this.streamKey); } async edit(options) { if (!this.guildId) { throw new Error('Cannot edit a user in a DM call.'); } return this.client.rest.editGuildMember(this.guildId, this.userId, options); } async editState(options) { if (!this.guildId) { throw new Error('Cannot edit a voice state in a DM call.'); } const userId = (this.userId === this.client.userId) ? '@me' : this.userId; return this.client.rest.editGuildVoiceState(this.guildId, userId, options); } joinVoice(options) { return this.client.voiceConnect(this.guildId, this.channelId, options); } move(channelId) { return this.edit({ channelId }); } setDeaf(deaf) { return this.edit({ deaf }); } setMute(mute) { return this.edit({ mute }); } mergeValue(key, value) { if (value !== undefined) { switch (key) { case constants_1.DiscordKeys.MEMBER: { if (value) { const guildId = this.guildId; value.guild_id = guildId; let member; if (this.isClone) { member = new member_1.Member(this.client, value, this.isClone); } else { if (this.client.members.has(guildId, value.user.id)) { member = this.client.members.get(guildId, value.user.id); member.merge(value); } else { member = new member_1.Member(this.client, value); this.client.members.insert(member); } } value = member; } } ; break; case constants_1.DiscordKeys.REQUEST_TO_SPEAK_TIMESTAMP: { this.requestToSpeakTimestampUnix = (value) ? (new Date(value).getTime()) : 0; } ; return; } return super.mergeValue(key, value); } } } exports.VoiceState = VoiceState;