Merge branch 'master' into indev-prism

This commit is contained in:
Amish Shah
2017-02-22 20:37:59 +00:00
48 changed files with 738 additions and 420 deletions

View File

@@ -1,7 +1,7 @@
const os = require('os');
const EventEmitter = require('events').EventEmitter;
const mergeDefault = require('../util/MergeDefault');
const Constants = require('../util/Constants');
const Util = require('../util/Util');
const RESTManager = require('./rest/RESTManager');
const ClientDataManager = require('./ClientDataManager');
const ClientManager = require('./ClientManager');
@@ -33,7 +33,7 @@ class Client extends EventEmitter {
* The options the client was instantiated with
* @type {ClientOptions}
*/
this.options = mergeDefault(Constants.DefaultOptions, options);
this.options = Util.mergeDefault(Constants.DefaultOptions, options);
this._validateOptions();
/**
@@ -44,42 +44,42 @@ class Client extends EventEmitter {
this.rest = new RESTManager(this);
/**
* The data manager of the Client
* The data manager of the client
* @type {ClientDataManager}
* @private
*/
this.dataManager = new ClientDataManager(this);
/**
* The manager of the Client
* The manager of the client
* @type {ClientManager}
* @private
*/
this.manager = new ClientManager(this);
/**
* The WebSocket Manager of the Client
* The WebSocket manager of the client
* @type {WebSocketManager}
* @private
*/
this.ws = new WebSocketManager(this);
/**
* The Data Resolver of the Client
* The data resolver of the client
* @type {ClientDataResolver}
* @private
*/
this.resolver = new ClientDataResolver(this);
/**
* The Action Manager of the Client
* The action manager of the client
* @type {ActionsManager}
* @private
*/
this.actions = new ActionsManager(this);
/**
* The Voice Manager of the Client (`null` in browsers)
* The voice manager of the client (`null` in browsers)
* @type {?ClientVoiceManager}
* @private
*/
@@ -155,8 +155,25 @@ class Client extends EventEmitter {
*/
this.pings = [];
/**
* Timestamp of the latest ping's start time
* @type {number}
* @private
*/
this._pingTimestamp = 0;
/**
* Timeouts set by {@link Client#setTimeout} that are still active
* @type {Set<Timeout>}
* @private
*/
this._timeouts = new Set();
/**
* Intervals set by {@link Client#setInterval} that are still active
* @type {Set<Timeout>}
* @private
*/
this._intervals = new Set();
if (this.options.messageSweepInterval > 0) {
@@ -258,7 +275,7 @@ class Client extends EventEmitter {
}
/**
* Logs out, terminates the connection to Discord, and destroys the client
* Logs out, terminates the connection to Discord, and destroys the client.
* @returns {Promise}
*/
destroy() {
@@ -361,12 +378,11 @@ class Client extends EventEmitter {
/**
* Obtains the OAuth Application of the bot from Discord.
* <warn>This is only available when using a bot account.</warn>
* @param {Snowflake} [id='@me'] ID of application to fetch
* @returns {Promise<ClientOAuth2Application>}
*/
fetchApplication() {
if (!this.user.bot) throw new Error(Constants.Errors.NO_BOT_ACCOUNT);
return this.rest.methods.getMyApplication();
fetchApplication(id = '@me') {
return this.rest.methods.getApplication(id);
}
/**
@@ -408,7 +424,7 @@ class Client extends EventEmitter {
}
/**
* Clears a timeout
* Clears a timeout.
* @param {Timeout} timeout Timeout to cancel
*/
clearTimeout(timeout) {
@@ -430,7 +446,7 @@ class Client extends EventEmitter {
}
/**
* Clears an interval
* Clears an interval.
* @param {Timeout} interval Interval to cancel
*/
clearInterval(interval) {
@@ -464,7 +480,8 @@ class Client extends EventEmitter {
}
/**
* Calls `eval(script)` with the client as `this`.
* Calls {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval} on a script
* with the client as `this`.
* @param {string} script Script to eval
* @returns {*}
* @private
@@ -474,7 +491,7 @@ class Client extends EventEmitter {
}
/**
* Validates client options
* Validates the client options.
* @param {ClientOptions} [options=this.options] Options to validate
* @private
*/

View File

@@ -1,5 +1,5 @@
const Constants = require('../util/Constants');
const cloneObject = require('../util/CloneObject');
const Util = require('../util/Util');
const Guild = require('../structures/Guild');
const User = require('../structures/User');
const DMChannel = require('../structures/DMChannel');
@@ -50,15 +50,15 @@ class ClientDataManager {
let channel;
if (data.type === Constants.ChannelTypes.DM) {
channel = new DMChannel(this.client, data);
} else if (data.type === Constants.ChannelTypes.groupDM) {
} else if (data.type === Constants.ChannelTypes.GROUP_DM) {
channel = new GroupDMChannel(this.client, data);
} else {
guild = guild || this.client.guilds.get(data.guild_id);
if (guild) {
if (data.type === Constants.ChannelTypes.text) {
if (data.type === Constants.ChannelTypes.TEXT) {
channel = new TextChannel(guild, data);
guild.channels.set(channel.id, channel);
} else if (data.type === Constants.ChannelTypes.voice) {
} else if (data.type === Constants.ChannelTypes.VOICE) {
channel = new VoiceChannel(guild, data);
guild.channels.set(channel.id, channel);
}
@@ -110,7 +110,7 @@ class ClientDataManager {
}
updateGuild(currentGuild, newData) {
const oldGuild = cloneObject(currentGuild);
const oldGuild = Util.cloneObject(currentGuild);
currentGuild.setup(newData);
if (this.pastReady) this.client.emit(Constants.Events.GUILD_UPDATE, oldGuild, currentGuild);
}
@@ -120,7 +120,7 @@ class ClientDataManager {
}
updateEmoji(currentEmoji, newData) {
const oldEmoji = cloneObject(currentEmoji);
const oldEmoji = Util.cloneObject(currentEmoji);
currentEmoji.setup(newData);
this.client.emit(Constants.Events.GUILD_EMOJI_UPDATE, oldEmoji, currentEmoji);
return currentEmoji;

View File

@@ -3,7 +3,7 @@ const fs = require('fs');
const request = require('superagent');
const Constants = require('../util/Constants');
const convertArrayBuffer = require('../util/ConvertArrayBuffer');
const convertToBuffer = require('../util/Util').convertToBuffer;
const User = require('../structures/User');
const Message = require('../structures/Message');
const Guild = require('../structures/Guild');
@@ -217,6 +217,20 @@ class ClientDataResolver {
return bitfield;
}
hasPermission(bitfield, name, explicit = false) {
const permission = this.resolvePermission(name);
if (!explicit && (bitfield & Constants.PermissionFlags.ADMINISTRATOR) > 0) return true;
return (bitfield & permission) > 0;
}
serializePermissions(bitfield) {
const serializedPermissions = {};
for (const name in Constants.PermissionFlags) {
serializedPermissions[name] = this.hasPermission(bitfield, name);
}
return serializedPermissions;
}
/**
* Data that can be resolved to give a string. This can be:
* * A string
@@ -268,7 +282,7 @@ class ClientDataResolver {
*/
resolveBuffer(resource) {
if (resource instanceof Buffer) return Promise.resolve(resource);
if (this.client.browser && resource instanceof ArrayBuffer) return Promise.resolve(convertArrayBuffer(resource));
if (this.client.browser && resource instanceof ArrayBuffer) return Promise.resolve(convertToBuffer(resource));
if (typeof resource === 'string') {
return new Promise((resolve, reject) => {
@@ -277,18 +291,19 @@ class ClientDataResolver {
if (this.client.browser) req.responseType('arraybuffer');
req.end((err, res) => {
if (err) return reject(err);
if (this.client.browser) return resolve(convertArrayBuffer(res.xhr.response));
if (this.client.browser) return resolve(convertToBuffer(res.xhr.response));
if (!(res.body instanceof Buffer)) return reject(new TypeError('The response body isn\'t a Buffer.'));
return resolve(res.body);
});
} else {
const file = path.resolve(resource);
fs.stat(file, (err, stats) => {
if (err) reject(err);
if (!stats || !stats.isFile()) throw new Error(`The file could not be found: ${file}`);
if (err) return reject(err);
if (!stats || !stats.isFile()) return reject(new Error(`The file could not be found: ${file}`));
fs.readFile(file, (err2, data) => {
if (err2) reject(err2); else resolve(data);
});
return null;
});
}
});
@@ -343,6 +358,7 @@ class ClientDataResolver {
* 'DARK_GREY',
* 'LIGHT_GREY',
* 'DARK_NAVY',
* 'RANDOM',
* ]
* ```
* or something like
@@ -360,6 +376,7 @@ class ClientDataResolver {
*/
static resolveColor(color) {
if (typeof color === 'string') {
if (color === 'RANDOM') return Math.floor(Math.random() * (0xFFFFFF + 1));
color = Constants.Colors[color] || parseInt(color.replace('#', ''), 16);
} else if (color instanceof Array) {
color = (color[0] << 16) + (color[1] << 8) + color[2];

View File

@@ -1,8 +1,8 @@
const Webhook = require('../structures/Webhook');
const RESTManager = require('./rest/RESTManager');
const ClientDataResolver = require('./ClientDataResolver');
const mergeDefault = require('../util/MergeDefault');
const Constants = require('../util/Constants');
const Util = require('../util/Util');
/**
* The Webhook Client
@@ -10,13 +10,13 @@ const Constants = require('../util/Constants');
*/
class WebhookClient extends Webhook {
/**
* @param {string} id The id of the webhook.
* @param {string} token the token of the webhook.
* @param {string} id ID of the webhook
* @param {string} token Token of the webhook
* @param {ClientOptions} [options] Options for the client
* @example
* // create a new webhook and send a message
* let hook = new Discord.WebhookClient('1234', 'abcdef')
* hook.sendMessage('This will send a message').catch(console.error)
* const hook = new Discord.WebhookClient('1234', 'abcdef');
* hook.sendMessage('This will send a message').catch(console.error);
*/
constructor(id, token, options) {
super(null, id, token);
@@ -25,7 +25,7 @@ class WebhookClient extends Webhook {
* The options the client was instantiated with
* @type {ClientOptions}
*/
this.options = mergeDefault(Constants.DefaultOptions, options);
this.options = Util.mergeDefault(Constants.DefaultOptions, options);
/**
* The REST manager of the client
@@ -35,11 +35,83 @@ class WebhookClient extends Webhook {
this.rest = new RESTManager(this);
/**
* The Data Resolver of the Client
* The data resolver of the client
* @type {ClientDataResolver}
* @private
*/
this.resolver = new ClientDataResolver(this);
/**
* Timeouts set by {@link WebhookClient#setTimeout} that are still active
* @type {Set<Timeout>}
* @private
*/
this._timeouts = new Set();
/**
* Intervals set by {@link WebhookClient#setInterval} that are still active
* @type {Set<Timeout>}
* @private
*/
this._intervals = new Set();
}
/**
* Sets a timeout that will be automatically cancelled if the client is destroyed.
* @param {Function} fn Function to execute
* @param {number} delay Time to wait before executing (in milliseconds)
* @param {...*} args Arguments for the function
* @returns {Timeout}
*/
setTimeout(fn, delay, ...args) {
const timeout = setTimeout(() => {
fn();
this._timeouts.delete(timeout);
}, delay, ...args);
this._timeouts.add(timeout);
return timeout;
}
/**
* Clears a timeout.
* @param {Timeout} timeout Timeout to cancel
*/
clearTimeout(timeout) {
clearTimeout(timeout);
this._timeouts.delete(timeout);
}
/**
* Sets an interval that will be automatically cancelled if the client is destroyed.
* @param {Function} fn Function to execute
* @param {number} delay Time to wait before executing (in milliseconds)
* @param {...*} args Arguments for the function
* @returns {Timeout}
*/
setInterval(fn, delay, ...args) {
const interval = setInterval(fn, delay, ...args);
this._intervals.add(interval);
return interval;
}
/**
* Clears an interval.
* @param {Timeout} interval Interval to cancel
*/
clearInterval(interval) {
clearInterval(interval);
this._intervals.delete(interval);
}
/**
* Destroys the client.
*/
destroy() {
for (const t of this._timeouts) clearTimeout(t);
for (const i of this._intervals) clearInterval(i);
this._timeouts.clear();
this._intervals.clear();
}
}

View File

@@ -1,6 +1,6 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
const cloneObject = require('../../util/CloneObject');
const Util = require('../../util/Util');
class ChannelUpdateAction extends Action {
handle(data) {
@@ -8,7 +8,7 @@ class ChannelUpdateAction extends Action {
const channel = client.channels.get(data.id);
if (channel) {
const oldChannel = cloneObject(channel);
const oldChannel = Util.cloneObject(channel);
channel.setup(data);
client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel);
return {

View File

@@ -1,6 +1,6 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
const cloneObject = require('../../util/CloneObject');
const Util = require('../../util/Util');
class GuildRoleUpdateAction extends Action {
handle(data) {
@@ -13,7 +13,7 @@ class GuildRoleUpdateAction extends Action {
const role = guild.roles.get(roleData.id);
if (role) {
oldRole = cloneObject(role);
oldRole = Util.cloneObject(role);
role.setup(data.role);
client.emit(Constants.Events.GUILD_ROLE_UPDATE, oldRole, role);
}

View File

@@ -1,6 +1,6 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
const cloneObject = require('../../util/CloneObject');
const Util = require('../../util/Util');
class GuildUpdateAction extends Action {
handle(data) {
@@ -8,7 +8,7 @@ class GuildUpdateAction extends Action {
const guild = client.guilds.get(data.id);
if (guild) {
const oldGuild = cloneObject(guild);
const oldGuild = Util.cloneObject(guild);
guild.setup(data);
client.emit(Constants.Events.GUILD_UPDATE, oldGuild, guild);
return {

View File

@@ -1,6 +1,6 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
const cloneObject = require('../../util/CloneObject');
const Util = require('../../util/Util');
class MessageUpdateAction extends Action {
handle(data) {
@@ -10,7 +10,7 @@ class MessageUpdateAction extends Action {
if (channel) {
const message = channel.messages.get(data.id);
if (message) {
const oldMessage = cloneObject(message);
const oldMessage = Util.cloneObject(message);
message.patch(data);
message._edits.unshift(oldMessage);
client.emit(Constants.Events.MESSAGE_UPDATE, oldMessage, message);

View File

@@ -1,6 +1,6 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
const cloneObject = require('../../util/CloneObject');
const Util = require('../../util/Util');
class UserUpdateAction extends Action {
handle(data) {
@@ -14,7 +14,7 @@ class UserUpdateAction extends Action {
};
}
const oldUser = cloneObject(client.user);
const oldUser = Util.cloneObject(client.user);
client.user.patch(data);
client.emit(Constants.Events.USER_UPDATE, oldUser, client.user);
return {

View File

@@ -1,11 +1,9 @@
const querystring = require('querystring');
const long = require('long');
const Constants = require('../../util/Constants');
const Collection = require('../../util/Collection');
const splitMessage = require('../../util/SplitMessage');
const parseEmoji = require('../../util/ParseEmoji');
const escapeMarkdown = require('../../util/EscapeMarkdown');
const transformSearchOptions = require('../../util/TransformSearchOptions');
const Snowflake = require('../../util/Snowflake');
const Util = require('../../util/Util');
const User = require('../../structures/User');
const GuildMember = require('../../structures/GuildMember');
@@ -14,7 +12,7 @@ const Role = require('../../structures/Role');
const Invite = require('../../structures/Invite');
const Webhook = require('../../structures/Webhook');
const UserProfile = require('../../structures/UserProfile');
const ClientOAuth2Application = require('../../structures/ClientOAuth2Application');
const OAuth2Application = require('../../structures/OAuth2Application');
const Channel = require('../../structures/Channel');
const Guild = require('../../structures/Guild');
const VoiceRegion = require('../../structures/VoiceRegion');
@@ -66,7 +64,7 @@ class RESTMethods {
// Wrap everything in a code block
if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) {
content = escapeMarkdown(this.client.resolver.resolveString(content), true);
content = Util.escapeMarkdown(this.client.resolver.resolveString(content), true);
content = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n${content}\n\`\`\``;
if (split) {
split.prepend = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n`;
@@ -88,7 +86,7 @@ class RESTMethods {
}
// Split the content
if (split) content = splitMessage(content, split);
if (split) content = Util.splitMessage(content, split);
} else if (reply && !(channel instanceof User || channel instanceof GuildMember) && channel.type !== 'dm') {
const id = this.client.resolver.resolveUserID(reply);
content = `<@${reply instanceof GuildMember && reply.nickname ? '!' : ''}${id}>`;
@@ -125,7 +123,7 @@ class RESTMethods {
// Wrap everything in a code block
if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) {
content = escapeMarkdown(this.client.resolver.resolveString(content), true);
content = Util.escapeMarkdown(this.client.resolver.resolveString(content), true);
content = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n${content}\n\`\`\``;
}
@@ -168,9 +166,46 @@ class RESTMethods {
}
search(target, options) {
options = transformSearchOptions(options, this.client);
for (const key in options) if (options[key] === undefined) delete options[key];
if (options.before) {
if (!(options.before instanceof Date)) options.before = new Date(options.before);
options.maxID = long.fromNumber(options.before.getTime() - 14200704e5).shiftLeft(22).toString();
}
if (options.after) {
if (!(options.after instanceof Date)) options.after = new Date(options.after);
options.minID = long.fromNumber(options.after.getTime() - 14200704e5).shiftLeft(22).toString();
}
if (options.during) {
if (!(options.during instanceof Date)) options.during = new Date(options.during);
const t = options.during.getTime() - 14200704e5;
options.minID = long.fromNumber(t).shiftLeft(22).toString();
options.maxID = long.fromNumber(t + 86400000).shiftLeft(22).toString();
}
if (options.channel) options.channel = this.client.resolver.resolveChannelID(options.channel);
if (options.author) options.author = this.client.resolver.resolveUserID(options.author);
if (options.mentions) options.mentions = this.client.resolver.resolveUserID(options.options.mentions);
options = {
content: options.content,
max_id: options.maxID,
min_id: options.minID,
has: options.has,
channel_id: options.channel,
author_id: options.author,
author_type: options.authorType,
context_size: options.contextSize,
sort_by: options.sortBy,
sort_order: options.sortOrder,
limit: options.limit,
offset: options.offset,
mentions: options.mentions,
mentions_everyone: options.mentionsEveryone,
link_hostname: options.linkHostname,
embed_provider: options.embedProvider,
embed_type: options.embedType,
attachment_filename: options.attachmentFilename,
attachment_extension: options.attachmentExtension,
};
for (const key in options) if (options[key] === undefined) delete options[key];
const queryString = (querystring.stringify(options).match(/[^=&?]+=[^=&?]+/g) || []).join('&');
let type;
@@ -377,6 +412,18 @@ class RESTMethods {
return this.rest.makeRequest('get', Constants.Endpoints.channelMessage(channel.id, messageID), true);
}
putGuildMember(guild, user, options) {
options.access_token = options.accessToken;
if (options.roles) {
const roles = options.roles;
if (roles instanceof Collection || (roles instanceof Array && roles[0] instanceof Role)) {
options.roles = roles.map(role => role.id);
}
}
return this.rest.makeRequest('put', Constants.Endpoints.guildMember(guild.id, user.id), true, options)
.then(data => this.client.actions.GuildMemberGet.handle(guild, data).member);
}
getGuildMember(guild, user, cache) {
return this.rest.makeRequest('get', Constants.Endpoints.guildMember(guild.id, user.id), true).then(data => {
if (cache) {
@@ -406,22 +453,42 @@ class RESTMethods {
}
addMemberRole(member, role) {
return this.rest.makeRequest('put', Constants.Endpoints.guildMemberRole(member.guild.id, member.id, role.id), true)
.then(() => {
if (!member._roles.includes(role.id)) member._roles.push(role.id);
return member;
});
return new Promise(resolve => {
const listener = (oldMember, newMember) => {
if (!oldMember._roles.includes(role.id) && newMember._roles.includes(role.id)) {
this.client.removeListener('guildMemberUpdate', listener);
resolve(newMember);
}
};
this.client.on('guildMemberUpdate', listener);
this.client.setTimeout(() => this.client.removeListener('guildMemberUpdate', listener), 10e3);
this.rest.makeRequest(
'put',
Constants.Endpoints.guildMemberRole(member.guild.id, member.id, role.id),
true
);
});
}
removeMemberRole(member, role) {
return this.rest.makeRequest(
'delete',
Constants.Endpoints.guildMemberRole(member.guild.id, member.id, role.id),
true
).then(() => {
const index = member._roles.indexOf(role.id);
if (index >= 0) member._roles.splice(index, 1);
return member;
return new Promise(resolve => {
const listener = (oldMember, newMember) => {
if (oldMember._roles.includes(role.id) && !newMember._roles.includes(role.id)) {
this.client.removeListener('guildMemberUpdate', listener);
resolve(newMember);
}
};
this.client.on('guildMemberUpdate', listener);
this.client.setTimeout(() => this.client.removeListener('guildMemberUpdate', listener), 10e3);
this.rest.makeRequest(
'delete',
Constants.Endpoints.guildMemberRole(member.guild.id, member.id, role.id),
true
);
});
}
@@ -700,7 +767,7 @@ class RESTMethods {
this.client.actions.MessageReactionAdd.handle({
user_id: this.client.user.id,
message_id: message.id,
emoji: parseEmoji(emoji),
emoji: Util.parseEmoji(emoji),
channel_id: message.channel.id,
}).reaction
);
@@ -715,7 +782,7 @@ class RESTMethods {
this.client.actions.MessageReactionRemove.handle({
user_id: user,
message_id: message.id,
emoji: parseEmoji(emoji),
emoji: Util.parseEmoji(emoji),
channel_id: message.channel.id,
}).reaction
);
@@ -732,12 +799,20 @@ class RESTMethods {
);
}
getMyApplication() {
return this.rest.makeRequest('get', Constants.Endpoints.myApplication, true).then(app =>
new ClientOAuth2Application(this.client, app)
getApplication(id) {
return this.rest.makeRequest('get', Constants.Endpoints.oauth2Application(id), true).then(app =>
new OAuth2Application(this.client, app)
);
}
resetApplication(id) {
return this.rest.makeRequest(
'post',
`${Constants.Endpoints.oauth2Application(id)}/reset`,
true
).then(app => new OAuth2Application(this.client, app));
}
setNote(user, note) {
return this.rest.makeRequest('put', Constants.Endpoints.note(user.id), true, { note }).then(() => user);
}

View File

@@ -3,8 +3,15 @@ const RequestHandler = require('./RequestHandler');
class BurstRequestHandler extends RequestHandler {
constructor(restManager, endpoint) {
super(restManager, endpoint);
this.requestRemaining = 1;
this.first = true;
this.client = restManager.client;
this.limit = Infinity;
this.resetTime = null;
this.remaining = 1;
this.timeDifference = 0;
this.resetTimeout = null;
}
push(request) {
@@ -12,58 +19,45 @@ class BurstRequestHandler extends RequestHandler {
this.handle();
}
handleNext(time) {
if (this.waiting) return;
this.waiting = true;
this.restManager.client.setTimeout(() => {
this.requestRemaining = this.requestLimit;
this.waiting = false;
this.handle();
}, time);
}
execute(item) {
if (!item) return;
item.request.gen().end((err, res) => {
if (res && res.headers) {
this.requestLimit = res.headers['x-ratelimit-limit'];
this.requestResetTime = Number(res.headers['x-ratelimit-reset']) * 1000;
this.requestRemaining = Number(res.headers['x-ratelimit-remaining']);
this.limit = Number(res.headers['x-ratelimit-limit']);
this.resetTime = Number(res.headers['x-ratelimit-reset']) * 1000;
this.remaining = Number(res.headers['x-ratelimit-remaining']);
this.timeDifference = Date.now() - new Date(res.headers.date).getTime();
this.handleNext(
this.requestResetTime - Date.now() + this.timeDifference + this.restManager.client.options.restTimeOffset
);
}
if (err) {
if (err.status === 429) {
this.requestRemaining = 0;
this.queue.unshift(item);
this.restManager.client.setTimeout(() => {
if (res.headers['x-ratelimit-global']) this.globalLimit = true;
if (this.resetTimeout) return;
this.resetTimeout = this.client.setTimeout(() => {
this.remaining = this.limit;
this.globalLimit = false;
this.handle();
}, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset);
if (res.headers['x-ratelimit-global']) this.globalLimit = true;
this.resetTimeout = null;
}, Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
} else {
item.reject(err);
this.handle();
}
} else {
this.globalLimit = false;
const data = res && res.body ? res.body : {};
item.resolve(data);
if (this.first) {
this.first = false;
this.handle();
}
this.handle();
}
});
}
handle() {
super.handle();
if (this.requestRemaining < 1 || this.queue.length === 0 || this.globalLimit) return;
while (this.queue.length > 0 && this.requestRemaining > 0) {
this.execute(this.queue.shift());
this.requestRemaining--;
}
if (this.remaining <= 0 || this.queue.length === 0 || this.globalLimit) return;
this.execute(this.queue.shift());
this.remaining--;
this.handle();
}
}

View File

@@ -15,12 +15,6 @@ class SequentialRequestHandler extends RequestHandler {
constructor(restManager, endpoint) {
super(restManager, endpoint);
/**
* Whether this rate limiter is waiting for a response from a request
* @type {boolean}
*/
this.waiting = false;
/**
* The endpoint that this handler is handling
* @type {string}
@@ -49,27 +43,24 @@ class SequentialRequestHandler extends RequestHandler {
return new Promise(resolve => {
item.request.gen().end((err, res) => {
if (res && res.headers) {
this.requestLimit = res.headers['x-ratelimit-limit'];
this.requestLimit = Number(res.headers['x-ratelimit-limit']);
this.requestResetTime = Number(res.headers['x-ratelimit-reset']) * 1000;
this.requestRemaining = Number(res.headers['x-ratelimit-remaining']);
this.timeDifference = Date.now() - new Date(res.headers.date).getTime();
}
if (err) {
if (err.status === 429) {
this.queue.unshift(item);
this.restManager.client.setTimeout(() => {
this.waiting = false;
this.globalLimit = false;
resolve();
}, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset);
if (res.headers['x-ratelimit-global']) this.globalLimit = true;
} else {
this.queue.shift();
this.waiting = false;
item.reject(err);
resolve(err);
}
} else {
this.queue.shift();
this.globalLimit = false;
const data = res && res.body ? res.body : {};
item.resolve(data);
@@ -82,7 +73,6 @@ class SequentialRequestHandler extends RequestHandler {
this.requestResetTime - Date.now() + this.timeDifference + this.restManager.client.options.restTimeOffset
);
} else {
this.waiting = false;
resolve(data);
}
}
@@ -92,12 +82,8 @@ class SequentialRequestHandler extends RequestHandler {
handle() {
super.handle();
if (this.waiting || this.queue.length === 0 || this.globalLimit) return;
this.waiting = true;
const item = this.queue[0];
this.execute(item).then(() => this.handle());
if (this.remaining === 0 || this.queue.length === 0 || this.globalLimit) return;
this.execute(this.queue.shift()).then(() => this.handle());
}
}

View File

@@ -1,4 +1,6 @@
const Collection = require('../../util/Collection');
const Constants = require('../../util/Constants');
const Util = require('../../util/Util');
const VoiceConnection = require('./VoiceConnection');
/**
@@ -34,6 +36,18 @@ class ClientVoiceManager {
connection.channel = this.client.channels.get(channel_id);
connection.setSessionID(session_id);
}
options = Util.mergeDefault({
guild_id: channel.guild.id,
channel_id: channel.id,
self_mute: false,
self_deaf: false,
}, options);
this.client.ws.send({
op: Constants.OPCodes.VOICE_STATE_UPDATE,
d: options,
});
}
/**

View File

@@ -1,7 +1,7 @@
const browser = require('os').platform() === 'browser';
const EventEmitter = require('events').EventEmitter;
const Constants = require('../../util/Constants');
const convertArrayBuffer = require('../../util/ConvertArrayBuffer');
const convertToBuffer = require('../../util/Util').convertToBuffer;
const pako = require('pako');
const zlib = require('zlib');
const PacketManager = require('./packets/WebSocketPacketManager');
@@ -279,7 +279,7 @@ class WebSocketManager extends EventEmitter {
*/
parseEventData(data) {
if (erlpack) {
if (data instanceof ArrayBuffer) data = convertArrayBuffer(data);
if (data instanceof ArrayBuffer) data = convertToBuffer(data);
return erlpack.unpack(data);
} else {
if (data instanceof ArrayBuffer) data = pako.inflate(data, { to: 'string' });

View File

@@ -1,6 +1,6 @@
const AbstractHandler = require('./AbstractHandler');
const Constants = require('../../../../util/Constants');
const cloneObject = require('../../../../util/CloneObject');
const Util = require('../../../../util/Util');
class PresenceUpdateHandler extends AbstractHandler {
handle(packet) {
@@ -18,7 +18,7 @@ class PresenceUpdateHandler extends AbstractHandler {
}
}
const oldUser = cloneObject(user);
const oldUser = Util.cloneObject(user);
user.patch(data.user);
if (!user.equals(oldUser)) {
client.emit(Constants.Events.USER_UPDATE, oldUser, user);
@@ -40,9 +40,9 @@ class PresenceUpdateHandler extends AbstractHandler {
guild._setPresence(user.id, data);
return;
}
const oldMember = cloneObject(member);
const oldMember = Util.cloneObject(member);
if (member.presence) {
oldMember.frozenPresence = cloneObject(member.presence);
oldMember.frozenPresence = Util.cloneObject(member.presence);
}
guild._setPresence(user.id, data);
client.emit(Constants.Events.PRESENCE_UPDATE, oldMember, member);

View File

@@ -1,7 +1,7 @@
const AbstractHandler = require('./AbstractHandler');
const Constants = require('../../../../util/Constants');
const cloneObject = require('../../../../util/CloneObject');
const Util = require('../../../../util/Util');
class VoiceStateUpdateHandler extends AbstractHandler {
handle(packet) {
@@ -12,7 +12,7 @@ class VoiceStateUpdateHandler extends AbstractHandler {
if (guild) {
const member = guild.members.get(data.user_id);
if (member) {
const oldVoiceChannelMember = cloneObject(member);
const oldVoiceChannelMember = Util.cloneObject(member);
if (member.voiceChannel && member.voiceChannel.id !== data.channel_id) {
member.voiceChannel.members.delete(oldVoiceChannelMember.id);
}