chore: consistency/prettier (#3852)

* chore: consistency/prettier

* chore: rebase

* chore: rebase

* chore: include typings

* fix: include typings file in prettier lint-staged
This commit is contained in:
Crawl
2020-02-29 14:35:57 +01:00
committed by GitHub
parent 6650d50a56
commit c065156a88
127 changed files with 5198 additions and 4513 deletions

View File

@@ -3,8 +3,8 @@
require('setimmediate');
const EventEmitter = require('events');
const RESTManager = require('../rest/RESTManager');
const Util = require('../util/Util');
const { DefaultOptions } = require('../util/Constants');
const Util = require('../util/Util');
/**
* The base class for all clients.

View File

@@ -1,24 +1,24 @@
'use strict';
const BaseClient = require('./BaseClient');
const Permissions = require('../util/Permissions');
const ActionsManager = require('./actions/ActionsManager');
const ClientVoiceManager = require('./voice/ClientVoiceManager');
const WebSocketManager = require('./websocket/WebSocketManager');
const ActionsManager = require('./actions/ActionsManager');
const Collection = require('../util/Collection');
const { Error, TypeError, RangeError } = require('../errors');
const ChannelManager = require('../managers/ChannelManager');
const GuildEmojiManager = require('../managers/GuildEmojiManager');
const GuildManager = require('../managers/GuildManager');
const UserManager = require('../managers/UserManager');
const ShardClientUtil = require('../sharding/ShardClientUtil');
const ClientApplication = require('../structures/ClientApplication');
const Invite = require('../structures/Invite');
const VoiceRegion = require('../structures/VoiceRegion');
const Webhook = require('../structures/Webhook');
const Invite = require('../structures/Invite');
const ClientApplication = require('../structures/ClientApplication');
const ShardClientUtil = require('../sharding/ShardClientUtil');
const UserManager = require('../managers/UserManager');
const ChannelManager = require('../managers/ChannelManager');
const GuildManager = require('../managers/GuildManager');
const GuildEmojiManager = require('../managers/GuildEmojiManager');
const Collection = require('../util/Collection');
const { Events, browser, DefaultOptions } = require('../util/Constants');
const DataResolver = require('../util/DataResolver');
const Permissions = require('../util/Permissions');
const Structures = require('../util/Structures');
const { Error, TypeError, RangeError } = require('../errors');
/**
* The main hub for interacting with the Discord API, and the starting point for any bot.
@@ -63,9 +63,11 @@ class Client extends BaseClient {
if (typeofShards === 'number') this.options.shards = [this.options.shards];
if (Array.isArray(this.options.shards)) {
this.options.shards = [...new Set(
this.options.shards.filter(item => !isNaN(item) && item >= 0 && item < Infinity && item === (item | 0)),
)];
this.options.shards = [
...new Set(
this.options.shards.filter(item => !isNaN(item) && item >= 0 && item < Infinity && item === (item | 0)),
),
];
}
this._validateOptions();
@@ -93,9 +95,10 @@ class Client extends BaseClient {
* Shard helpers for the client (only if the process was spawned from a {@link ShardingManager})
* @type {?ShardClientUtil}
*/
this.shard = !browser && process.env.SHARDING_MANAGER ?
ShardClientUtil.singleton(this, process.env.SHARDING_MANAGER_MODE) :
null;
this.shard =
!browser && process.env.SHARDING_MANAGER
? ShardClientUtil.singleton(this, process.env.SHARDING_MANAGER_MODE)
: null;
/**
* All of the {@link User} objects that have been cached at any point, mapped by their IDs
@@ -198,8 +201,12 @@ class Client extends BaseClient {
async login(token = this.token) {
if (!token || typeof token !== 'string') throw new Error('TOKEN_INVALID');
this.token = token = token.replace(/^(Bot|Bearer)\s*/i, '');
this.emit(Events.DEBUG,
`Provided token: ${token.split('.').map((val, i) => i > 1 ? val.replace(/./g, '*') : val).join('.')}`,
this.emit(
Events.DEBUG,
`Provided token: ${token
.split('.')
.map((val, i) => (i > 1 ? val.replace(/./g, '*') : val))
.join('.')}`,
);
if (this.options.presence) {
@@ -238,7 +245,9 @@ class Client extends BaseClient {
*/
fetchInvite(invite) {
const code = DataResolver.resolveInviteCode(invite);
return this.api.invites(code).get({ query: { with_counts: true } })
return this.api
.invites(code)
.get({ query: { with_counts: true } })
.then(data => new Invite(this, data));
}
@@ -253,7 +262,10 @@ class Client extends BaseClient {
* .catch(console.error);
*/
fetchWebhook(id, token) {
return this.api.webhooks(id, token).get().then(data => new Webhook(this, data));
return this.api
.webhooks(id, token)
.get()
.then(data => new Webhook(this, data));
}
/**
@@ -289,7 +301,7 @@ class Client extends BaseClient {
throw new TypeError('INVALID_TYPE', 'lifetime', 'number');
}
if (lifetime <= 0) {
this.emit(Events.DEBUG, 'Didn\'t sweep messages - lifetime is unlimited');
this.emit(Events.DEBUG, "Didn't sweep messages - lifetime is unlimited");
return -1;
}
@@ -307,8 +319,10 @@ class Client extends BaseClient {
);
}
this.emit(Events.DEBUG,
`Swept ${messages} messages older than ${lifetime} seconds in ${channels} text-based channels`);
this.emit(
Events.DEBUG,
`Swept ${messages} messages older than ${lifetime} seconds in ${channels} text-based channels`,
);
return messages;
}
@@ -317,7 +331,9 @@ class Client extends BaseClient {
* @returns {Promise<ClientApplication>}
*/
fetchApplication() {
return this.api.oauth2.applications('@me').get()
return this.api.oauth2
.applications('@me')
.get()
.then(app => new ClientApplication(this, app));
}
@@ -364,14 +380,12 @@ class Client extends BaseClient {
* @param {ClientOptions} [options=this.options] Options to validate
* @private
*/
_validateOptions(options = this.options) { // eslint-disable-line complexity
_validateOptions(options = this.options) {
if (typeof options.shardCount !== 'number' || isNaN(options.shardCount) || options.shardCount < 1) {
throw new TypeError('CLIENT_INVALID_OPTION', 'shardCount', 'a number greater than or equal to 1');
}
if (options.shards &&
!(options.shards === 'auto' || Array.isArray(options.shards))
) {
throw new TypeError('CLIENT_INVALID_OPTION', 'shards', '\'auto\', a number or array of numbers');
if (options.shards && !(options.shards === 'auto' || Array.isArray(options.shards))) {
throw new TypeError('CLIENT_INVALID_OPTION', 'shards', "'auto', a number or array of numbers");
}
if (options.shards && !options.shards.length) throw new RangeError('CLIENT_INVALID_PROVIDED_SHARDS');
if (typeof options.messageCacheMaxSize !== 'number' || isNaN(options.messageCacheMaxSize)) {

View File

@@ -1,7 +1,7 @@
'use strict';
const Webhook = require('../structures/Webhook');
const BaseClient = require('./BaseClient');
const Webhook = require('../structures/Webhook');
/**
* The webhook client.

View File

@@ -33,38 +33,65 @@ class GenericAction {
getChannel(data) {
const id = data.channel_id || data.id;
return data.channel || this.getPayload({
id,
guild_id: data.guild_id,
recipients: [data.author || { id: data.user_id }],
}, this.client.channels, id, PartialTypes.CHANNEL);
return (
data.channel ||
this.getPayload(
{
id,
guild_id: data.guild_id,
recipients: [data.author || { id: data.user_id }],
},
this.client.channels,
id,
PartialTypes.CHANNEL,
)
);
}
getMessage(data, channel, cache) {
const id = data.message_id || data.id;
return data.message || this.getPayload({
id,
channel_id: channel.id,
guild_id: data.guild_id || (channel.guild ? channel.guild.id : null),
}, channel.messages, id, PartialTypes.MESSAGE, cache);
return (
data.message ||
this.getPayload(
{
id,
channel_id: channel.id,
guild_id: data.guild_id || (channel.guild ? channel.guild.id : null),
},
channel.messages,
id,
PartialTypes.MESSAGE,
cache,
)
);
}
getReaction(data, message, user) {
const id = data.emoji.id || decodeURIComponent(data.emoji.name);
return this.getPayload({
emoji: data.emoji,
count: message.partial ? null : 0,
me: user ? user.id === this.client.user.id : false,
}, message.reactions, id, PartialTypes.REACTION);
return this.getPayload(
{
emoji: data.emoji,
count: message.partial ? null : 0,
me: user ? user.id === this.client.user.id : false,
},
message.reactions,
id,
PartialTypes.REACTION,
);
}
getMember(data, guild) {
const id = data.user.id;
return this.getPayload({
user: {
id,
return this.getPayload(
{
user: {
id,
},
},
}, guild.members, id, PartialTypes.GUILD_MEMBER);
guild.members,
id,
PartialTypes.GUILD_MEMBER,
);
}
getUser(data) {

View File

@@ -1,8 +1,8 @@
'use strict';
const Action = require('./Action');
const { Events } = require('../../util/Constants');
const DMChannel = require('../../structures/DMChannel');
const { Events } = require('../../util/Constants');
class ChannelDeleteAction extends Action {
constructor(client) {

View File

@@ -17,4 +17,3 @@ class GuildIntegrationsUpdate extends Action {
}
module.exports = GuildIntegrationsUpdate;

View File

@@ -27,5 +27,4 @@ class GuildMemberRemoveAction extends Action {
}
}
module.exports = GuildMemberRemoveAction;

View File

@@ -22,5 +22,4 @@ class GuildRoleCreate extends Action {
}
}
module.exports = GuildRoleCreate;

View File

@@ -27,5 +27,4 @@ class GuildRoleDeleteAction extends Action {
}
}
module.exports = GuildRoleDeleteAction;

View File

@@ -36,5 +36,4 @@ class GuildRoleUpdateAction extends Action {
}
}
module.exports = GuildRoleUpdateAction;

View File

@@ -30,5 +30,4 @@ class GuildUpdateAction extends Action {
}
}
module.exports = GuildUpdateAction;

View File

@@ -36,5 +36,4 @@ class MessageCreateAction extends Action {
}
}
module.exports = MessageCreateAction;

View File

@@ -26,5 +26,4 @@ class MessageDeleteAction extends Action {
}
}
module.exports = MessageDeleteAction;

View File

@@ -13,10 +13,14 @@ class MessageDeleteBulkAction extends Action {
const ids = data.ids;
const messages = new Collection();
for (const id of ids) {
const message = this.getMessage({
id,
guild_id: data.guild_id,
}, channel, false);
const message = this.getMessage(
{
id,
guild_id: data.guild_id,
},
channel,
false,
);
if (message) {
message.deleted = true;
messages.set(message.id, message);
@@ -36,5 +40,4 @@ class MessageDeleteBulkAction extends Action {
}
}
module.exports = MessageDeleteBulkAction;

View File

@@ -41,5 +41,4 @@ class MessageReactionRemove extends Action {
}
}
module.exports = MessageReactionRemove;

View File

@@ -1,8 +1,8 @@
'use strict';
const Action = require('./Action');
const { Events } = require('../../util/Constants');
const VoiceState = require('../../structures/VoiceState');
const { Events } = require('../../util/Constants');
class VoiceStateUpdate extends Action {
handle(data) {
@@ -10,9 +10,9 @@ class VoiceStateUpdate extends Action {
const guild = client.guilds.cache.get(data.guild_id);
if (guild) {
// Update the state
const oldState = guild.voiceStates.cache.has(data.user_id) ?
guild.voiceStates.cache.get(data.user_id)._clone() :
new VoiceState(guild, { user_id: data.user_id });
const oldState = guild.voiceStates.cache.has(data.user_id)
? guild.voiceStates.cache.get(data.user_id)._clone()
: new VoiceState(guild, { user_id: data.user_id });
const newState = guild.voiceStates.add(data);
@@ -41,5 +41,4 @@ class VoiceStateUpdate extends Action {
}
}
module.exports = VoiceStateUpdate;

View File

@@ -16,5 +16,4 @@ class WebhooksUpdate extends Action {
}
}
module.exports = WebhooksUpdate;

View File

@@ -1,9 +1,9 @@
'use strict';
const Collection = require('../../util/Collection');
const VoiceConnection = require('./VoiceConnection');
const VoiceBroadcast = require('./VoiceBroadcast');
const VoiceConnection = require('./VoiceConnection');
const { Error } = require('../../errors');
const Collection = require('../../util/Collection');
/**
* Manages voice connections for the client
@@ -83,7 +83,8 @@ class ClientVoiceManager {
} else {
connection = new VoiceConnection(this, channel);
connection.on('debug', msg =>
this.client.emit('debug', `[VOICE (${channel.guild.id}:${connection.status})]: ${msg}`));
this.client.emit('debug', `[VOICE (${channel.guild.id}:${connection.status})]: ${msg}`),
);
connection.authenticate();
this.connections.set(channel.guild.id, connection);
}

View File

@@ -2,8 +2,8 @@
const EventEmitter = require('events');
const BroadcastAudioPlayer = require('./player/BroadcastAudioPlayer');
const { Events } = require('../../util/Constants');
const PlayInterface = require('./util/PlayInterface');
const { Events } = require('../../util/Constants');
/**
* A voice broadcast can be played across multiple voice connections for improved shared-stream efficiency.
@@ -59,8 +59,9 @@ class VoiceBroadcast extends EventEmitter {
* broadcast.play('http://www.sample-videos.com/audio/mp3/wave.mp3');
* @returns {BroadcastDispatcher}
*/
play() { return null; }
play() {
return null;
}
/**
* Ends the broadcast, unsubscribing all subscribed channels and deleting the broadcast

View File

@@ -1,16 +1,16 @@
'use strict';
const VoiceWebSocket = require('./networking/VoiceWebSocket');
const EventEmitter = require('events');
const VoiceUDP = require('./networking/VoiceUDPClient');
const Util = require('../../util/Util');
const { OPCodes, VoiceOPCodes, VoiceStatus, Events } = require('../../util/Constants');
const VoiceWebSocket = require('./networking/VoiceWebSocket');
const AudioPlayer = require('./player/AudioPlayer');
const VoiceReceiver = require('./receiver/Receiver');
const EventEmitter = require('events');
const { Error } = require('../../errors');
const PlayInterface = require('./util/PlayInterface');
const Speaking = require('../../util/Speaking');
const Silence = require('./util/Silence');
const { Error } = require('../../errors');
const { OPCodes, VoiceOPCodes, VoiceStatus, Events } = require('../../util/Constants');
const Speaking = require('../../util/Speaking');
const Util = require('../../util/Util');
// Workaround for Discord now requiring silence to be sent before being able to receive audio
class SingleSilence extends Silence {
@@ -20,11 +20,7 @@ class SingleSilence extends Silence {
}
}
const SUPPORTED_MODES = [
'xsalsa20_poly1305_lite',
'xsalsa20_poly1305_suffix',
'xsalsa20_poly1305',
];
const SUPPORTED_MODES = ['xsalsa20_poly1305_lite', 'xsalsa20_poly1305_suffix', 'xsalsa20_poly1305'];
/**
* Represents a connection to a guild's voice server.
@@ -154,16 +150,18 @@ class VoiceConnection extends EventEmitter {
if (this.speaking.equals(value)) return;
if (this.status !== VoiceStatus.CONNECTED) return;
this.speaking = new Speaking(value).freeze();
this.sockets.ws.sendPacket({
op: VoiceOPCodes.SPEAKING,
d: {
speaking: this.speaking.bitfield,
delay: 0,
ssrc: this.authentication.ssrc,
},
}).catch(e => {
this.emit('debug', e);
});
this.sockets.ws
.sendPacket({
op: VoiceOPCodes.SPEAKING,
d: {
speaking: this.speaking.bitfield,
delay: 0,
ssrc: this.authentication.ssrc,
},
})
.catch(e => {
this.emit('debug', e);
});
}
/**
@@ -181,19 +179,25 @@ class VoiceConnection extends EventEmitter {
* @private
*/
sendVoiceStateUpdate(options = {}) {
options = Util.mergeDefault({
guild_id: this.channel.guild.id,
channel_id: this.channel.id,
self_mute: this.voice ? this.voice.selfMute : false,
self_deaf: this.voice ? this.voice.selfDeaf : false,
}, options);
options = Util.mergeDefault(
{
guild_id: this.channel.guild.id,
channel_id: this.channel.id,
self_mute: this.voice ? this.voice.selfMute : false,
self_deaf: this.voice ? this.voice.selfDeaf : false,
},
options,
);
this.emit('debug', `Sending voice state update: ${JSON.stringify(options)}`);
return this.channel.guild.shard.send({
op: OPCodes.VOICE_STATE_UPDATE,
d: options,
}, true);
return this.channel.guild.shard.send(
{
op: OPCodes.VOICE_STATE_UPDATE,
d: options,
},
true,
);
}
/**
@@ -318,8 +322,7 @@ class VoiceConnection extends EventEmitter {
*/
authenticate() {
this.sendVoiceStateUpdate();
this.connectTimeout = this.client.setTimeout(
() => this.authenticateFailed('VOICE_CONNECTION_TIMEOUT'), 15000);
this.connectTimeout = this.client.setTimeout(() => this.authenticateFailed('VOICE_CONNECTION_TIMEOUT'), 15000);
}
/**
@@ -371,7 +374,6 @@ class VoiceConnection extends EventEmitter {
this.emit('disconnect');
}
/**
* Cleans up after disconnect.
* @private
@@ -513,7 +515,7 @@ class VoiceConnection extends EventEmitter {
}
}
play() { } // eslint-disable-line no-empty-function
play() {} // eslint-disable-line no-empty-function
}
PlayInterface.applyToClass(VoiceConnection);

View File

@@ -1,16 +1,15 @@
'use strict';
const VolumeInterface = require('../util/VolumeInterface');
const { Writable } = require('stream');
const secretbox = require('../util/Secretbox');
const Silence = require('../util/Silence');
const VolumeInterface = require('../util/VolumeInterface');
const FRAME_LENGTH = 20;
const CHANNELS = 2;
const TIMESTAMP_INC = (48000 / 100) * CHANNELS;
const MAX_NONCE_SIZE = (2 ** 32) - 1;
const MAX_NONCE_SIZE = 2 ** 32 - 1;
const nonce = Buffer.alloc(24);
/**
@@ -31,10 +30,7 @@ const nonce = Buffer.alloc(24);
* @extends {WritableStream}
*/
class StreamDispatcher extends Writable {
constructor(
player,
{ seek = 0, volume = 1, fec, plp, bitrate = 96, highWaterMark = 12 } = {},
streams) {
constructor(player, { seek = 0, volume = 1, fec, plp, bitrate = 96, highWaterMark = 12 } = {}, streams) {
const streamOptions = { seek, volume, fec, plp, bitrate, highWaterMark };
super(streamOptions);
/**
@@ -146,7 +142,9 @@ class StreamDispatcher extends Writable {
* @type {boolean}
* @readonly
*/
get paused() { return Boolean(this.pausedSince); }
get paused() {
return Boolean(this.pausedSince);
}
/**
* Total time that this dispatcher has been paused in milliseconds
@@ -233,7 +231,7 @@ class StreamDispatcher extends Writable {
done();
};
if (!this.streams.broadcast) {
const next = FRAME_LENGTH + (this.count * FRAME_LENGTH) - (Date.now() - this.startTime - this._pausedTime);
const next = FRAME_LENGTH + this.count * FRAME_LENGTH - (Date.now() - this.startTime - this._pausedTime);
setTimeout(() => {
if ((!this.pausedSince || this._silence) && this._writeCallback) this._writeCallback();
}, next);
@@ -261,10 +259,7 @@ class StreamDispatcher extends Writable {
this._nonce++;
if (this._nonce > MAX_NONCE_SIZE) this._nonce = 0;
this._nonceBuffer.writeUInt32BE(this._nonce, 0);
return [
secretbox.methods.close(buffer, this._nonceBuffer, secret_key),
this._nonceBuffer.slice(0, 4),
];
return [secretbox.methods.close(buffer, this._nonceBuffer, secret_key), this._nonceBuffer.slice(0, 4)];
} else if (mode === 'xsalsa20_poly1305_suffix') {
const random = secretbox.methods.random(24);
return [secretbox.methods.close(buffer, random, secret_key), random];
@@ -297,11 +292,10 @@ class StreamDispatcher extends Writable {
this.emit('debug', 'Failed to send a packet - no UDP socket');
return;
}
this.player.voiceConnection.sockets.udp.send(packet)
.catch(e => {
this._setSpeaking(0);
this.emit('debug', `Failed to send a packet - ${e}`);
});
this.player.voiceConnection.sockets.udp.send(packet).catch(e => {
this._setSpeaking(0);
this.emit('debug', `Failed to send a packet - ${e}`);
});
}
_setSpeaking(value) {
@@ -316,14 +310,18 @@ class StreamDispatcher extends Writable {
this.emit('speaking', value);
}
get volumeEditable() { return Boolean(this.streams.volume); }
get volumeEditable() {
return Boolean(this.streams.volume);
}
/**
* Whether or not the Opus bitrate of this stream is editable
* @type {boolean}
* @readonly
*/
get bitrateEditable() { return this.streams.opus && this.streams.opus.setBitrate; }
get bitrateEditable() {
return this.streams.opus && this.streams.opus.setBitrate;
}
// Volume
get volume() {

View File

@@ -1,9 +1,9 @@
'use strict';
const udp = require('dgram');
const { VoiceOPCodes } = require('../../../util/Constants');
const EventEmitter = require('events');
const { Error } = require('../../../errors');
const { VoiceOPCodes } = require('../../../util/Constants');
/**
* Represents a UDP client for a Voice Connection.
@@ -90,7 +90,7 @@ class VoiceConnectionUDPClient extends EventEmitter {
async createUDPSocket(address) {
this.discordAddress = address;
const socket = this.socket = udp.createSocket('udp4');
const socket = (this.socket = udp.createSocket('udp4'));
socket.on('error', e => {
this.emit('debug', `[UDP] Error: ${e}`);
this.emit('error', e);

View File

@@ -1,9 +1,9 @@
'use strict';
const { OPCodes, VoiceOPCodes } = require('../../../util/Constants');
const EventEmitter = require('events');
const { Error } = require('../../../errors');
const WebSocket = require('../../../WebSocket');
const { Error } = require('../../../errors');
const { OPCodes, VoiceOPCodes } = require('../../../util/Constants');
/**
* Represents a Voice Connection's WebSocket.
@@ -92,7 +92,8 @@ class VoiceWebSocket extends EventEmitter {
return new Promise((resolve, reject) => {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) throw new Error('WS_NOT_OPEN', data);
this.ws.send(data, null, error => {
if (error) reject(error); else resolve(data);
if (error) reject(error);
else resolve(data);
});
});
}

View File

@@ -1,17 +1,11 @@
'use strict';
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const { Readable: ReadableStream } = require('stream');
const prism = require('prism-media');
const StreamDispatcher = require('../dispatcher/StreamDispatcher');
const FFMPEG_ARGUMENTS = [
'-analyzeduration', '0',
'-loglevel', '0',
'-f', 's16le',
'-ar', '48000',
'-ac', '2',
];
const FFMPEG_ARGUMENTS = ['-analyzeduration', '0', '-loglevel', '0', '-f', 's16le', '-ar', '48000', '-ac', '2'];
/**
* An Audio Player for a Voice Connection.
@@ -61,7 +55,7 @@ class BasePlayer extends EventEmitter {
playPCMStream(stream, options, streams = {}) {
this.destroyDispatcher();
const opus = streams.opus = new prism.opus.Encoder({ channels: 2, rate: 48000, frameSize: 960 });
const opus = (streams.opus = new prism.opus.Encoder({ channels: 2, rate: 48000, frameSize: 960 }));
if (options && options.volume === false) {
stream.pipe(opus);
return this.playOpusStream(opus, options, streams);
@@ -90,7 +84,7 @@ class BasePlayer extends EventEmitter {
createDispatcher(options, streams, broadcast) {
this.destroyDispatcher();
const dispatcher = this.dispatcher = new StreamDispatcher(this, options, streams, broadcast);
const dispatcher = (this.dispatcher = new StreamDispatcher(this, options, streams, broadcast));
return dispatcher;
}
}

View File

@@ -1,7 +1,7 @@
'use strict';
const BroadcastDispatcher = require('../dispatcher/BroadcastDispatcher');
const BasePlayer = require('./BasePlayer');
const BroadcastDispatcher = require('../dispatcher/BroadcastDispatcher');
/**
* An Audio Player for a Voice Connection.
@@ -20,7 +20,7 @@ class AudioPlayer extends BasePlayer {
createDispatcher(options, streams) {
this.destroyDispatcher();
const dispatcher = this.dispatcher = new BroadcastDispatcher(this, options, streams);
const dispatcher = (this.dispatcher = new BroadcastDispatcher(this, options, streams));
return dispatcher;
}
}

View File

@@ -1,13 +1,15 @@
'use strict';
const secretbox = require('../util/Secretbox');
const EventEmitter = require('events');
const secretbox = require('../util/Secretbox');
// The delay between packets when a user is considered to have stopped speaking
// https://github.com/discordjs/discord.js/issues/3524#issuecomment-540373200
const DISCORD_SPEAKING_DELAY = 250;
class Readable extends require('stream').Readable { _read() {} } // eslint-disable-line no-empty-function
class Readable extends require('stream').Readable {
_read() {} // eslint-disable-line no-empty-function
}
class PacketHandler extends EventEmitter {
constructor(receiver) {
@@ -59,7 +61,7 @@ class PacketHandler extends EventEmitter {
packet = Buffer.from(packet);
// Strip RTP Header Extensions (one-byte only)
if (packet[0] === 0xBE && packet[1] === 0xDE && packet.length > 4) {
if (packet[0] === 0xbe && packet[1] === 0xde && packet.length > 4) {
const headerExtensionLength = packet.readUInt16BE(2);
let offset = 4;
for (let i = 0; i < headerExtensionLength; i++) {

View File

@@ -85,12 +85,12 @@ class PlayInterface {
static applyToClass(structure) {
for (const prop of ['play']) {
Object.defineProperty(structure.prototype, prop,
Object.getOwnPropertyDescriptor(PlayInterface.prototype, prop));
Object.defineProperty(structure.prototype, prop, Object.getOwnPropertyDescriptor(PlayInterface.prototype, prop));
}
}
}
module.exports = PlayInterface;
// eslint-disable-next-line import/order
const Broadcast = require('../VoiceBroadcast');

View File

@@ -2,7 +2,7 @@
const { Readable } = require('stream');
const SILENCE_FRAME = Buffer.from([0xF8, 0xFF, 0xFE]);
const SILENCE_FRAME = Buffer.from([0xf8, 0xff, 0xfe]);
class Silence extends Readable {
_read() {

View File

@@ -94,19 +94,10 @@ class VolumeInterface extends EventEmitter {
}
}
const props = [
'volumeDecibels',
'volumeLogarithmic',
'setVolumeDecibels',
'setVolumeLogarithmic',
];
const props = ['volumeDecibels', 'volumeLogarithmic', 'setVolumeDecibels', 'setVolumeLogarithmic'];
exports.applyToClass = function applyToClass(structure) {
for (const prop of props) {
Object.defineProperty(
structure.prototype,
prop,
Object.getOwnPropertyDescriptor(VolumeInterface.prototype, prop),
);
Object.defineProperty(structure.prototype, prop, Object.getOwnPropertyDescriptor(VolumeInterface.prototype, prop));
}
};

View File

@@ -1,12 +1,12 @@
'use strict';
const EventEmitter = require('events');
const WebSocketShard = require('./WebSocketShard');
const PacketHandlers = require('./handlers');
const { Error: DJSError } = require('../../errors');
const Collection = require('../../util/Collection');
const Util = require('../../util/Util');
const WebSocketShard = require('./WebSocketShard');
const { Events, ShardEvents, Status, WSCodes, WSEvents } = require('../../util/Constants');
const PacketHandlers = require('./handlers');
const Util = require('../../util/Util');
const BeforeReadyWhitelist = [
WSEvents.READY,
@@ -18,7 +18,9 @@ const BeforeReadyWhitelist = [
WSEvents.GUILD_MEMBER_REMOVE,
];
const UNRECOVERABLE_CLOSE_CODES = Object.keys(WSCodes).slice(1).map(Number);
const UNRECOVERABLE_CLOSE_CODES = Object.keys(WSCodes)
.slice(1)
.map(Number);
const UNRESUMABLE_CLOSE_CODES = [1000, 4006, 4007];
/**

View File

@@ -237,14 +237,15 @@ class WebSocketShard extends EventEmitter {
Gateway : ${gateway}
Version : ${client.options.ws.version}
Encoding : ${WebSocket.encoding}
Compression: ${zlib ? 'zlib-stream' : 'none'}`);
Compression: ${zlib ? 'zlib-stream' : 'none'}`,
);
this.status = this.status === Status.DISCONNECTED ? Status.RECONNECTING : Status.CONNECTING;
this.setHelloTimeout();
this.connectedAt = Date.now();
const ws = this.connection = WebSocket.create(gateway, wsQuery);
const ws = (this.connection = WebSocket.create(gateway, wsQuery));
ws.onopen = this.onOpen.bind(this);
ws.onmessage = this.onMessage.bind(this);
ws.onerror = this.onError.bind(this);
@@ -271,11 +272,8 @@ class WebSocketShard extends EventEmitter {
if (data instanceof ArrayBuffer) data = new Uint8Array(data);
if (zlib) {
const l = data.length;
const flush = l >= 4 &&
data[l - 4] === 0x00 &&
data[l - 3] === 0x00 &&
data[l - 2] === 0xFF &&
data[l - 1] === 0xFF;
const flush =
l >= 4 && data[l - 4] === 0x00 && data[l - 3] === 0x00 && data[l - 2] === 0xff && data[l - 1] === 0xff;
this.inflate.push(data, flush && zlib.Z_SYNC_FLUSH);
if (!flush) return;
@@ -529,8 +527,10 @@ class WebSocketShard extends EventEmitter {
* @param {boolean} [ignoreHeartbeatAck] If we should send the heartbeat forcefully.
* @private
*/
sendHeartbeat(tag = 'HeartbeatTimer',
ignoreHeartbeatAck = [Status.WAITING_FOR_GUILDS, Status.IDENTIFYING, Status.RESUMING].includes(this.status)) {
sendHeartbeat(
tag = 'HeartbeatTimer',
ignoreHeartbeatAck = [Status.WAITING_FOR_GUILDS, Status.IDENTIFYING, Status.RESUMING].includes(this.status),
) {
if (ignoreHeartbeatAck && !this.lastHeartbeatAcked) {
this.debug(`[${tag}] Didn't process heartbeat ack yet but we are still connected. Sending one now.`);
} else if (!this.lastHeartbeatAcked) {
@@ -538,7 +538,8 @@ class WebSocketShard extends EventEmitter {
`[${tag}] Didn't receive a heartbeat ack last time, assuming zombie connection. Destroying and reconnecting.
Status : ${STATUS_KEYS[this.status]}
Sequence : ${this.sequence}
Connection State: ${this.connection ? CONNECTION_STATE[this.connection.readyState] : 'No Connection??'}`);
Connection State: ${this.connection ? CONNECTION_STATE[this.connection.readyState] : 'No Connection??'}`,
);
this.destroy({ closeCode: 4009, reset: true });
return;
@@ -741,10 +742,7 @@ class WebSocketShard extends EventEmitter {
* @private
*/
_cleanupConnection() {
this.connection.onopen =
this.connection.onclose =
this.connection.onerror =
this.connection.onmessage = null;
this.connection.onopen = this.connection.onclose = this.connection.onerror = this.connection.onmessage = null;
}
/**

View File

@@ -14,4 +14,3 @@ module.exports = (client, packet) => {
client.emit(Events.CHANNEL_UPDATE, old, updated);
}
};

View File

@@ -10,9 +10,9 @@ module.exports = async (client, { d: data }, shard) => {
guild._patch(data);
// If the client was ready before and we had unavailable guilds, fetch them
if (client.ws.status === Status.READY && client.options.fetchAllMembers) {
await guild.members.fetch().catch(err =>
client.emit(Events.DEBUG, `Failed to fetch all members: ${err}\n${err.stack}`),
);
await guild.members
.fetch()
.catch(err => client.emit(Events.DEBUG, `Failed to fetch all members: ${err}\n${err.stack}`));
}
}
} else {
@@ -26,9 +26,9 @@ module.exports = async (client, { d: data }, shard) => {
* @param {Guild} guild The created guild
*/
if (client.options.fetchAllMembers) {
await guild.members.fetch().catch(err =>
client.emit(Events.DEBUG, `Failed to fetch all members: ${err}\n${err.stack}`),
);
await guild.members
.fetch()
.catch(err => client.emit(Events.DEBUG, `Failed to fetch all members: ${err}\n${err.stack}`));
}
client.emit(Events.GUILD_CREATE, guild);
}

View File

@@ -1,7 +1,7 @@
'use strict';
const { Events } = require('../../../util/Constants');
const Collection = require('../../../util/Collection');
const { Events } = require('../../../util/Constants');
module.exports = (client, { d: data }) => {
const guild = client.guilds.cache.get(data.guild_id);

View File

@@ -8,11 +8,11 @@ module.exports = (client, { d: data }, shard) => {
guild.memberCount++;
const member = guild.members.add(data);
if (shard.status === Status.READY) {
/**
* Emitted whenever a user joins a guild.
* @event Client#guildMemberAdd
* @param {GuildMember} member The member that has joined a guild
*/
/**
* Emitted whenever a user joins a guild.
* @event Client#guildMemberAdd
* @param {GuildMember} member The member that has joined a guild
*/
client.emit(Events.GUILD_MEMBER_ADD, member);
}
}

View File

@@ -7,12 +7,12 @@ module.exports = (client, { d: data }) => {
const user = client.users.cache.get(data.user_id);
if (channel && user) {
/**
* Emitted whenever a user starts typing in a channel.
* @event Client#typingStart
* @param {Channel} channel The channel the user started typing in
* @param {User} user The user that started typing
*/
/**
* Emitted whenever a user starts typing in a channel.
* @event Client#typingStart
* @param {Channel} channel The channel the user started typing in
* @param {User} user The user that started typing
*/
client.emit(Events.TYPING_START, channel, user);
}
};