mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Merge branch 'master' into indev-prism
This commit is contained in:
@@ -39,7 +39,7 @@ Using opusscript is only recommended for development environments where node-opu
|
||||
For production bots, using node-opus should be considered a necessity, especially if they're going to be running on multiple servers.
|
||||
|
||||
### Optional packages
|
||||
- [bufferutil](https://www.npmjs.com/package/bufferutil) to greatly speed up the `ws` WebSocket connection (`npm install bufferutil --save`)
|
||||
- [bufferutil](https://www.npmjs.com/package/bufferutil) to greatly speed up the WebSocket when *not* using uws (`npm install bufferutil --save`)
|
||||
- [erlpack](https://github.com/hammerandchisel/erlpack) for significantly faster WebSocket data (de)serialisation (`npm install hammerandchisel/erlpack --save`)
|
||||
- [sodium](https://www.npmjs.com/package/sodium) for faster voice packet encryption/decryption (`npm install sodium --save`)
|
||||
- [uws](https://www.npmjs.com/package/uws) for a much faster WebSocket connection (`npm install uws --save`)
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
// import the discord.js module
|
||||
const Discord = require('discord.js');
|
||||
|
||||
// create an instance of a Discord Client, and call it bot
|
||||
// create an instance of a Discord Client
|
||||
const client = new Discord.Client();
|
||||
|
||||
// the token of your bot - https://discordapp.com/developers/applications/me
|
||||
|
||||
33
docs/examples/greeting.js
Normal file
33
docs/examples/greeting.js
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
A bot that welcomes new guild members when they join
|
||||
*/
|
||||
|
||||
// import the discord.js module
|
||||
const Discord = require('discord.js');
|
||||
|
||||
// create an instance of a Discord Client
|
||||
const client = new Discord.Client();
|
||||
|
||||
// the token of your bot - https://discordapp.com/developers/applications/me
|
||||
const token = 'your bot token here';
|
||||
|
||||
// the ID of the channel in which the bot will greet new users
|
||||
const channelID = 'your channel ID here';
|
||||
|
||||
// the ready event is vital, it means that your bot will only start reacting to information
|
||||
// from Discord _after_ ready is emitted.
|
||||
client.on('ready', () => {
|
||||
console.log('I am ready!');
|
||||
});
|
||||
|
||||
// create an event listener for new guild members
|
||||
client.on('guildMemberAdd', member => {
|
||||
// get the channel by its ID
|
||||
const channel = client.channels.get(channelID);
|
||||
|
||||
// send the message, mentioning the member
|
||||
channel.sendMessage(`Welcome to the server, ${member}!`);
|
||||
});
|
||||
|
||||
// log our bot in
|
||||
client.login(token);
|
||||
@@ -43,7 +43,7 @@ Using opusscript is only recommended for development environments where node-opu
|
||||
For production bots, using node-opus should be considered a necessity, especially if they're going to be running on multiple servers.
|
||||
|
||||
### Optional packages
|
||||
- [bufferutil](https://www.npmjs.com/package/bufferutil) to greatly speed up the `ws` WebSocket connection (`npm install bufferutil --save`)
|
||||
- [bufferutil](https://www.npmjs.com/package/bufferutil) to greatly speed up the WebSocket when *not* using uws (`npm install bufferutil --save`)
|
||||
- [erlpack](https://github.com/hammerandchisel/erlpack) for significantly faster WebSocket data (de)serialisation (`npm install hammerandchisel/erlpack --save`)
|
||||
- [sodium](https://www.npmjs.com/package/sodium) for faster voice packet encryption/decryption (`npm install sodium --save`)
|
||||
- [uws](https://www.npmjs.com/package/uws) for a much faster WebSocket connection (`npm install uws --save`)
|
||||
|
||||
@@ -18,5 +18,7 @@
|
||||
path: ping.js
|
||||
- name: Avatars
|
||||
path: avatars.js
|
||||
- name: Server greeting
|
||||
path: greeting.js
|
||||
- name: Webhook
|
||||
path: webhook.js
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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(
|
||||
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
|
||||
).then(() => {
|
||||
const index = member._roles.indexOf(role.id);
|
||||
if (index >= 0) member._roles.splice(index, 1);
|
||||
return member;
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
handle() {
|
||||
super.handle();
|
||||
if (this.requestRemaining < 1 || this.queue.length === 0 || this.globalLimit) return;
|
||||
while (this.queue.length > 0 && this.requestRemaining > 0) {
|
||||
if (this.remaining <= 0 || this.queue.length === 0 || this.globalLimit) return;
|
||||
this.execute(this.queue.shift());
|
||||
this.requestRemaining--;
|
||||
}
|
||||
this.remaining--;
|
||||
this.handle();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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' });
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
23
src/index.js
23
src/index.js
@@ -1,19 +1,29 @@
|
||||
const Util = require('./util/Util');
|
||||
|
||||
module.exports = {
|
||||
// "Root" classes (starting points)
|
||||
Client: require('./client/Client'),
|
||||
WebhookClient: require('./client/WebhookClient'),
|
||||
Shard: require('./sharding/Shard'),
|
||||
ShardClientUtil: require('./sharding/ShardClientUtil'),
|
||||
ShardingManager: require('./sharding/ShardingManager'),
|
||||
WebhookClient: require('./client/WebhookClient'),
|
||||
|
||||
// Utilities
|
||||
Collection: require('./util/Collection'),
|
||||
splitMessage: require('./util/SplitMessage'),
|
||||
escapeMarkdown: require('./util/EscapeMarkdown'),
|
||||
fetchRecommendedShards: require('./util/FetchRecommendedShards'),
|
||||
Constants: require('./util/Constants'),
|
||||
Snowflake: require('./util/Snowflake'),
|
||||
SnowflakeUtil: require('./util/Snowflake'),
|
||||
Util: Util,
|
||||
util: Util,
|
||||
version: require('../package').version,
|
||||
|
||||
// Shortcuts to Util methods
|
||||
escapeMarkdown: Util.escapeMarkdown,
|
||||
fetchRecommendedShards: Util.fetchRecommendedShards,
|
||||
splitMessage: Util.splitMessage,
|
||||
|
||||
// Structures
|
||||
Channel: require('./structures/Channel'),
|
||||
ClientOAuth2Application: require('./structures/ClientOAuth2Application'),
|
||||
ClientUser: require('./structures/ClientUser'),
|
||||
DMChannel: require('./structures/DMChannel'),
|
||||
Emoji: require('./structures/Emoji'),
|
||||
@@ -41,9 +51,6 @@ module.exports = {
|
||||
User: require('./structures/User'),
|
||||
VoiceChannel: require('./structures/VoiceChannel'),
|
||||
Webhook: require('./structures/Webhook'),
|
||||
|
||||
version: require('../package').version,
|
||||
Constants: require('./util/Constants'),
|
||||
};
|
||||
|
||||
if (require('os').platform() === 'browser') window.Discord = module.exports; // eslint-disable-line no-undef
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
const childProcess = require('child_process');
|
||||
const path = require('path');
|
||||
const makeError = require('../util/MakeError');
|
||||
const makePlainError = require('../util/MakePlainError');
|
||||
const Util = require('../util/Util');
|
||||
|
||||
/**
|
||||
* Represents a Shard spawned by the ShardingManager.
|
||||
@@ -110,7 +109,7 @@ class Shard {
|
||||
if (!message || message._eval !== script) return;
|
||||
this.process.removeListener('message', listener);
|
||||
this._evals.delete(script);
|
||||
if (!message._error) resolve(message._result); else reject(makeError(message._error));
|
||||
if (!message._error) resolve(message._result); else reject(Util.makeError(message._error));
|
||||
};
|
||||
this.process.on('message', listener);
|
||||
|
||||
@@ -136,7 +135,7 @@ class Shard {
|
||||
if (message._sFetchProp) {
|
||||
this.manager.fetchClientValues(message._sFetchProp).then(
|
||||
results => this.send({ _sFetchProp: message._sFetchProp, _result: results }),
|
||||
err => this.send({ _sFetchProp: message._sFetchProp, _error: makePlainError(err) })
|
||||
err => this.send({ _sFetchProp: message._sFetchProp, _error: Util.makePlainError(err) })
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -145,7 +144,7 @@ class Shard {
|
||||
if (message._sEval) {
|
||||
this.manager.broadcastEval(message._sEval).then(
|
||||
results => this.send({ _sEval: message._sEval, _result: results }),
|
||||
err => this.send({ _sEval: message._sEval, _error: makePlainError(err) })
|
||||
err => this.send({ _sEval: message._sEval, _error: Util.makePlainError(err) })
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const makeError = require('../util/MakeError');
|
||||
const makePlainError = require('../util/MakePlainError');
|
||||
const Util = require('../util/Util');
|
||||
|
||||
/**
|
||||
* Helper class for sharded clients spawned as a child process, such as from a ShardingManager
|
||||
@@ -59,7 +58,7 @@ class ShardClientUtil {
|
||||
const listener = message => {
|
||||
if (!message || message._sFetchProp !== prop) return;
|
||||
process.removeListener('message', listener);
|
||||
if (!message._error) resolve(message._result); else reject(makeError(message._error));
|
||||
if (!message._error) resolve(message._result); else reject(Util.makeError(message._error));
|
||||
};
|
||||
process.on('message', listener);
|
||||
|
||||
@@ -80,7 +79,7 @@ class ShardClientUtil {
|
||||
const listener = message => {
|
||||
if (!message || message._sEval !== script) return;
|
||||
process.removeListener('message', listener);
|
||||
if (!message._error) resolve(message._result); else reject(makeError(message._error));
|
||||
if (!message._error) resolve(message._result); else reject(Util.makeError(message._error));
|
||||
};
|
||||
process.on('message', listener);
|
||||
|
||||
@@ -107,7 +106,7 @@ class ShardClientUtil {
|
||||
try {
|
||||
this._respond('eval', { _eval: message._eval, _result: this.client._eval(message._eval) });
|
||||
} catch (err) {
|
||||
this._respond('eval', { _eval: message._eval, _error: makePlainError(err) });
|
||||
this._respond('eval', { _eval: message._eval, _error: Util.makePlainError(err) });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
const mergeDefault = require('../util/MergeDefault');
|
||||
const Shard = require('./Shard');
|
||||
const Collection = require('../util/Collection');
|
||||
const fetchRecommendedShards = require('../util/FetchRecommendedShards');
|
||||
const Util = require('../util/Util');
|
||||
|
||||
/**
|
||||
* This is a utility class that can be used to help you spawn shards of your Client. Each shard is completely separate
|
||||
@@ -23,7 +22,7 @@ class ShardingManager extends EventEmitter {
|
||||
*/
|
||||
constructor(file, options = {}) {
|
||||
super();
|
||||
options = mergeDefault({
|
||||
options = Util.mergeDefault({
|
||||
totalShards: 'auto',
|
||||
respawn: true,
|
||||
shardArgs: [],
|
||||
@@ -105,7 +104,7 @@ class ShardingManager extends EventEmitter {
|
||||
*/
|
||||
spawn(amount = this.totalShards, delay = 5500) {
|
||||
if (amount === 'auto') {
|
||||
return fetchRecommendedShards(this.token).then(count => {
|
||||
return Util.fetchRecommendedShards(this.token).then(count => {
|
||||
this.totalShards = count;
|
||||
return this._spawn(count, delay);
|
||||
});
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
const User = require('./User');
|
||||
const OAuth2Application = require('./OAuth2Application');
|
||||
|
||||
/**
|
||||
* Represents the client's OAuth2 Application
|
||||
* @extends {OAuth2Application}
|
||||
*/
|
||||
class ClientOAuth2Application extends OAuth2Application {
|
||||
setup(data) {
|
||||
super.setup(data);
|
||||
|
||||
/**
|
||||
* The app's flags
|
||||
* @type {number}
|
||||
*/
|
||||
this.flags = data.flags;
|
||||
|
||||
/**
|
||||
* The app's owner
|
||||
* @type {User}
|
||||
*/
|
||||
this.owner = new User(this.client, data.owner);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ClientOAuth2Application;
|
||||
@@ -5,9 +5,7 @@ const Presence = require('./Presence').Presence;
|
||||
const GuildMember = require('./GuildMember');
|
||||
const Constants = require('../util/Constants');
|
||||
const Collection = require('../util/Collection');
|
||||
const cloneObject = require('../util/CloneObject');
|
||||
const arraysEqual = require('../util/ArraysEqual');
|
||||
const moveElementInArray = require('../util/MoveElementInArray');
|
||||
const Util = require('../util/Util');
|
||||
|
||||
/**
|
||||
* Represents a guild (or a server) on Discord.
|
||||
@@ -331,6 +329,24 @@ class Guild {
|
||||
return this.client.rest.methods.fetchVoiceRegions(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a user to the guild using OAuth2. Requires the `CREATE_INSTANT_INVITE` permission.
|
||||
* @param {UserResolvable} user User to add to the guild
|
||||
* @param {Object} options Options for the addition
|
||||
* @param {string} options.accessToken An OAuth2 access token for the user with the `guilds.join` scope granted to the
|
||||
* bot's application
|
||||
* @param {string} [options.nick] Nickname to give the member (requires `MANAGE_NICKNAMES`)
|
||||
* @param {Collection<Snowflake, Role>|Role[]|Snowflake[]} [options.roles] Roles to add to the member
|
||||
* (requires `MANAGE_ROLES`)
|
||||
* @param {boolean} [options.mute] Whether the member should be muted (requires `MUTE_MEMBERS`)
|
||||
* @param {boolean} [options.deaf] Whether the member should be deafened (requires `DEAFEN_MEMBERS`)
|
||||
* @returns {Promise<GuildMember>}
|
||||
*/
|
||||
addMember(user, options) {
|
||||
if (this.members.has(user.id)) return Promise.resolve(this.members.get(user.id));
|
||||
return this.client.rest.methods.putGuildMember(this, user, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a single guild member from a user.
|
||||
* @param {UserResolvable} user The user to fetch the member for
|
||||
@@ -552,8 +568,10 @@ class Guild {
|
||||
* If the GuildMember cannot be resolved, the User will instead be attempted to be resolved. If that also cannot
|
||||
* be resolved, the user ID will be the result.
|
||||
* @example
|
||||
* // ban a user
|
||||
* guild.ban('123123123123');
|
||||
* // ban a user by ID (or with a user/guild member object)
|
||||
* guild.ban('some user ID')
|
||||
* .then(user => console.log(`Banned ${user.username || user.id || user} from ${guild.name}`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
ban(user, deleteDays = 0) {
|
||||
return this.client.rest.methods.banGuildMember(this, user, deleteDays);
|
||||
@@ -564,10 +582,10 @@ class Guild {
|
||||
* @param {UserResolvable} user The user to unban
|
||||
* @returns {Promise<User>}
|
||||
* @example
|
||||
* // unban a user
|
||||
* guild.unban('123123123123')
|
||||
* // unban a user by ID (or with a user/guild member object)
|
||||
* guild.unban('some user ID')
|
||||
* .then(user => console.log(`Unbanned ${user.username} from ${guild.name}`))
|
||||
* .catch(reject);
|
||||
* .catch(console.error);
|
||||
*/
|
||||
unban(user) {
|
||||
return this.client.rest.methods.unbanGuildMember(this, user);
|
||||
@@ -659,7 +677,7 @@ class Guild {
|
||||
let updatedRoles = Object.assign([], this.roles.array()
|
||||
.sort((r1, r2) => r1.position !== r2.position ? r1.position - r2.position : r1.id - r2.id));
|
||||
|
||||
moveElementInArray(updatedRoles, role, position, relative);
|
||||
Util.moveElementInArray(updatedRoles, role, position, relative);
|
||||
|
||||
updatedRoles = updatedRoles.map((r, i) => ({ id: r.id, position: i }));
|
||||
return this.client.rest.methods.setRolePositions(this.id, updatedRoles);
|
||||
@@ -687,9 +705,10 @@ class Guild {
|
||||
if (typeof attachment === 'string' && attachment.startsWith('data:')) {
|
||||
resolve(this.client.rest.methods.createEmoji(this, attachment, name, roles));
|
||||
} else {
|
||||
this.client.resolver.resolveBuffer(attachment).then(data =>
|
||||
resolve(this.client.rest.methods.createEmoji(this, data, name, roles))
|
||||
);
|
||||
this.client.resolver.resolveBuffer(attachment).then(data => {
|
||||
const dataURI = this.client.resolver.resolveBase64(data);
|
||||
resolve(this.client.rest.methods.createEmoji(this, dataURI, name, roles));
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -748,7 +767,7 @@ class Guild {
|
||||
this.memberCount === guild.member_count &&
|
||||
this.large === guild.large &&
|
||||
this.icon === guild.icon &&
|
||||
arraysEqual(this.features, guild.features) &&
|
||||
Util.arraysEqual(this.features, guild.features) &&
|
||||
this.ownerID === guild.owner_id &&
|
||||
this.verificationLevel === guild.verification_level &&
|
||||
this.embedEnabled === guild.embed_enabled;
|
||||
@@ -814,12 +833,12 @@ class Guild {
|
||||
}
|
||||
|
||||
_updateMember(member, data) {
|
||||
const oldMember = cloneObject(member);
|
||||
const oldMember = Util.cloneObject(member);
|
||||
|
||||
if (data.roles) member._roles = data.roles;
|
||||
if (typeof data.nick !== 'undefined') member.nickname = data.nick;
|
||||
|
||||
const notSame = member.nickname !== oldMember.nickname || !arraysEqual(member._roles, oldMember._roles);
|
||||
const notSame = member.nickname !== oldMember.nickname || !Util.arraysEqual(member._roles, oldMember._roles);
|
||||
|
||||
if (this.client.ws.status === Constants.Status.READY && notSame) {
|
||||
/**
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
const Attachment = require('./MessageAttachment');
|
||||
const Embed = require('./MessageEmbed');
|
||||
const MessageReaction = require('./MessageReaction');
|
||||
const Util = require('../util/Util');
|
||||
const Collection = require('../util/Collection');
|
||||
const Constants = require('../util/Constants');
|
||||
const escapeMarkdown = require('../util/EscapeMarkdown');
|
||||
let GuildMember;
|
||||
|
||||
/**
|
||||
@@ -56,7 +56,7 @@ class Message {
|
||||
/**
|
||||
* Represents the author of the message as a guild member. Only available if the message comes from a guild
|
||||
* where the author is still a member.
|
||||
* @type {GuildMember}
|
||||
* @type {?GuildMember}
|
||||
*/
|
||||
this.member = this.guild ? this.guild.member(this.author) || null : null;
|
||||
|
||||
@@ -409,7 +409,7 @@ class Message {
|
||||
* @returns {Promise<Message>}
|
||||
*/
|
||||
editCode(lang, content) {
|
||||
content = escapeMarkdown(this.client.resolver.resolveString(content), true);
|
||||
content = Util.escapeMarkdown(this.client.resolver.resolveString(content), true);
|
||||
return this.edit(`\`\`\`${lang || ''}\n${content}\n\`\`\``);
|
||||
}
|
||||
|
||||
|
||||
@@ -47,9 +47,51 @@ class OAuth2Application {
|
||||
|
||||
/**
|
||||
* The app's RPC origins
|
||||
* @type {Array<string>}
|
||||
* @type {?string[]}
|
||||
*/
|
||||
this.rpcOrigins = data.rpc_origins;
|
||||
|
||||
/**
|
||||
* The app's redirect URIs
|
||||
* @type {string[]}
|
||||
*/
|
||||
this.redirectURIs = data.redirect_uris;
|
||||
|
||||
/**
|
||||
* If this app's bot requires a code grant when using the oauth2 flow
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.botRequireCodeGrant = data.bot_require_code_grant;
|
||||
|
||||
/**
|
||||
* If this app's bot is public
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.botPublic = data.bot_public;
|
||||
|
||||
/**
|
||||
* If this app can use rpc
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.rpcApplicationState = data.rpc_application_state;
|
||||
|
||||
/**
|
||||
* Object containing basic info about this app's bot
|
||||
* @type {Object}
|
||||
*/
|
||||
this.bot = data.bot;
|
||||
|
||||
/**
|
||||
* Flags for the app
|
||||
* @type {number}
|
||||
*/
|
||||
this.flags = data.flags;
|
||||
|
||||
/**
|
||||
* oauth2 secret for the app
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.secret = data.secret;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,6 +112,14 @@ class OAuth2Application {
|
||||
return new Date(this.createdTimestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the app's secret and bot token
|
||||
* @returns {OAuth2Application}
|
||||
*/
|
||||
reset() {
|
||||
return this.client.rest.methods.resetApplication(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* When concatenated with a string, this automatically concatenates the app name rather than the app object.
|
||||
* @returns {string}
|
||||
|
||||
@@ -37,7 +37,7 @@ class PartialGuildChannel {
|
||||
* The type of this guild channel - `text` or `voice`
|
||||
* @type {string}
|
||||
*/
|
||||
this.type = Constants.ChannelTypes.text === data.type ? 'text' : 'voice';
|
||||
this.type = Constants.ChannelTypes.TEXT === data.type ? 'text' : 'voice';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -65,6 +65,12 @@ class RichEmbed {
|
||||
* @type {Object}
|
||||
*/
|
||||
this.footer = data.footer;
|
||||
|
||||
/**
|
||||
* File to upload alongside this Embed
|
||||
* @type {string}
|
||||
*/
|
||||
this.file = data.file;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -150,6 +156,15 @@ class RichEmbed {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function for `<RichEmbed>.addField('\u200B', '\u200B', inline)`.
|
||||
* @param {boolean} [inline=false] Set the field to display inline
|
||||
* @returns {RichEmbed} This embed
|
||||
*/
|
||||
addBlankField(inline = false) {
|
||||
return this.addField('\u200B', '\u200B', inline);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the thumbnail of this embed
|
||||
* @param {string} url The URL of the thumbnail
|
||||
@@ -162,7 +177,7 @@ class RichEmbed {
|
||||
|
||||
/**
|
||||
* Set the image of this embed
|
||||
* @param {string} url The URL of the thumbnail
|
||||
* @param {string} url The URL of the image
|
||||
* @returns {RichEmbed} This embed
|
||||
*/
|
||||
setImage(url) {
|
||||
@@ -182,6 +197,18 @@ class RichEmbed {
|
||||
this.footer = { text, icon_url: icon };
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the file to upload alongside the embed. This file can be accessed via `attachment://fileName.extension` when
|
||||
* setting an embed image or author/footer icons. Only one file may be attached.
|
||||
* @param {FileOptions|string} file Local path or URL to the file to attach, or valid FileOptions for a file to attach
|
||||
* @returns {RichEmbed} This embed
|
||||
*/
|
||||
attachFile(file) {
|
||||
if (this.file) throw new RangeError('You may not upload more than one file at once.');
|
||||
this.file = file;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RichEmbed;
|
||||
|
||||
@@ -140,11 +140,7 @@ class Role {
|
||||
* console.log(role.serialize());
|
||||
*/
|
||||
serialize() {
|
||||
const serializedPermissions = {};
|
||||
for (const permissionName in Constants.PermissionFlags) {
|
||||
serializedPermissions[permissionName] = this.hasPermission(permissionName);
|
||||
}
|
||||
return serializedPermissions;
|
||||
return this.client.resolver.serializePermissions(this.permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -160,10 +156,8 @@ class Role {
|
||||
* console.log('This role can\'t ban members');
|
||||
* }
|
||||
*/
|
||||
hasPermission(permission, explicit = false) {
|
||||
permission = this.client.resolver.resolvePermission(permission);
|
||||
if (!explicit && (this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) return true;
|
||||
return (this.permissions & permission) > 0;
|
||||
hasPermission(permission, explicit) {
|
||||
return this.client.resolver.hasPermission(this.permissions, permission, explicit);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -78,6 +78,8 @@ class TextBasedChannel {
|
||||
options = {};
|
||||
}
|
||||
|
||||
if (options.embed && options.embed.file) options.file = options.embed.file;
|
||||
|
||||
if (options.file) {
|
||||
if (typeof options.file === 'string') options.file = { attachment: options.file };
|
||||
if (!options.file.name) {
|
||||
@@ -223,6 +225,33 @@ class TextBasedChannel {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {Object} MessageSearchOptions
|
||||
* @property {string} [content] Message content
|
||||
* @property {string} [maxID] Maximum ID for the filter
|
||||
* @property {string} [minID] Minimum ID for the filter
|
||||
* @property {string} [has] One of `link`, `embed`, `file`, `video`, `image`, or `sound`,
|
||||
* or add `-` to negate (e.g. `-file`)
|
||||
* @property {ChannelResolvable} [channel] Channel to limit search to (only for guild search endpoint)
|
||||
* @property {UserResolvable} [author] Author to limit search
|
||||
* @property {string} [authorType] One of `user`, `bot`, `webhook`, or add `-` to negate (e.g. `-webhook`)
|
||||
* @property {string} [sortBy='recent'] `recent` or `relevant`
|
||||
* @property {string} [sortOrder='desc'] `asc` or `desc`
|
||||
* @property {number} [contextSize=2] How many messages to get around the matched message (0 to 2)
|
||||
* @property {number} [limit=25] Maximum number of results to get (1 to 25)
|
||||
* @property {number} [offset=0] Offset the "pages" of results (since you can only see 25 at a time)
|
||||
* @property {UserResolvable} [mentions] Mentioned user filter
|
||||
* @property {boolean} [mentionsEveryone] If everyone is mentioned
|
||||
* @property {string} [linkHostname] Filter links by hostname
|
||||
* @property {string} [embedProvider] The name of an embed provider
|
||||
* @property {string} [embedType] one of `image`, `video`, `url`, `rich`
|
||||
* @property {string} [attachmentFilename] The name of an attachment
|
||||
* @property {string} [attachmentExtension] The extension of an attachment
|
||||
* @property {Date} [before] Date to find messages before
|
||||
* @property {Date} [after] Date to find messages before
|
||||
* @property {Date} [during] Date to find messages during (range of date to date + 24 hours)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Performs a search within the channel.
|
||||
* @param {MessageSearchOptions} [options={}] Options to pass to the search
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
module.exports = function arraysEqual(a, b) {
|
||||
if (a === b) return true;
|
||||
if (a.length !== b.length) return false;
|
||||
|
||||
for (const itemInd in a) {
|
||||
const item = a[itemInd];
|
||||
const ind = b.indexOf(item);
|
||||
if (ind) {
|
||||
b.splice(ind, 1);
|
||||
}
|
||||
}
|
||||
|
||||
return b.length === 0;
|
||||
};
|
||||
@@ -1,5 +0,0 @@
|
||||
module.exports = function cloneObject(obj) {
|
||||
const cloned = Object.create(obj);
|
||||
Object.assign(cloned, obj);
|
||||
return cloned;
|
||||
};
|
||||
@@ -6,6 +6,8 @@ exports.Package = require('../../package.json');
|
||||
* @property {string} [apiRequestMethod='sequential'] One of `sequential` or `burst`. The sequential handler executes
|
||||
* all requests in the order they are triggered, whereas the burst handler runs multiple in parallel, and doesn't
|
||||
* provide the guarantee of any particular order.
|
||||
* <warn>Burst mode is more likely to hit a 429 ratelimit by its nature,
|
||||
* be advised if you are very unlucky you could be IP banned</warn>
|
||||
* @property {number} [shardId=0] ID of the shard to run
|
||||
* @property {number} [shardCount=0] Total number of shards
|
||||
* @property {number} [messageCacheMaxSize=200] Maximum number of messages to cache per channel
|
||||
@@ -156,7 +158,7 @@ const Endpoints = exports.Endpoints = {
|
||||
webhook: (webhookID, token) => `${API}/webhooks/${webhookID}${token ? `/${token}` : ''}`,
|
||||
|
||||
// oauth
|
||||
myApplication: `${API}/oauth2/applications/@me`,
|
||||
oauth2Application: (appID) => `${API}/oauth2/applications/${appID}`,
|
||||
getApp: (id) => `${API}/oauth2/authorize?client_id=${id}`,
|
||||
|
||||
// emoji
|
||||
@@ -200,10 +202,10 @@ exports.VoiceStatus = {
|
||||
};
|
||||
|
||||
exports.ChannelTypes = {
|
||||
text: 0,
|
||||
TEXT: 0,
|
||||
DM: 1,
|
||||
voice: 2,
|
||||
groupDM: 3,
|
||||
VOICE: 2,
|
||||
GROUP_DM: 3,
|
||||
};
|
||||
|
||||
exports.OPCodes = {
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
function str2ab(str) {
|
||||
const buffer = new ArrayBuffer(str.length * 2);
|
||||
const view = new Uint16Array(buffer);
|
||||
for (var i = 0, strLen = str.length; i < strLen; i++) view[i] = str.charCodeAt(i);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
module.exports = function convertArrayBuffer(x) {
|
||||
if (typeof x === 'string') x = str2ab(x);
|
||||
return Buffer.from(x);
|
||||
};
|
||||
@@ -1,5 +0,0 @@
|
||||
module.exports = function escapeMarkdown(text, onlyCodeBlock = false, onlyInlineCode = false) {
|
||||
if (onlyCodeBlock) return text.replace(/```/g, '`\u200b``');
|
||||
if (onlyInlineCode) return text.replace(/\\(`|\\)/g, '$1').replace(/(`|\\)/g, '\\$1');
|
||||
return text.replace(/\\(\*|_|`|~|\\)/g, '$1').replace(/(\*|_|`|~|\\)/g, '\\$1');
|
||||
};
|
||||
@@ -1,22 +0,0 @@
|
||||
const superagent = require('superagent');
|
||||
const botGateway = require('./Constants').Endpoints.botGateway;
|
||||
|
||||
/**
|
||||
* Gets the recommended shard count from Discord
|
||||
* @param {string} token Discord auth token
|
||||
* @param {number} [guildsPerShard=1000] Number of guilds per shard
|
||||
* @returns {Promise<number>} the recommended number of shards
|
||||
*/
|
||||
function fetchRecommendedShards(token, guildsPerShard = 1000) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!token) throw new Error('A token must be provided.');
|
||||
superagent.get(botGateway)
|
||||
.set('Authorization', `Bot ${token.replace(/^Bot\s*/i, '')}`)
|
||||
.end((err, res) => {
|
||||
if (err) reject(err);
|
||||
resolve(res.body.shards * (1000 / guildsPerShard));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = fetchRecommendedShards;
|
||||
@@ -1,6 +0,0 @@
|
||||
module.exports = function makeError(obj) {
|
||||
const err = new Error(obj.message);
|
||||
err.name = obj.name;
|
||||
err.stack = obj.stack;
|
||||
return err;
|
||||
};
|
||||
@@ -1,7 +0,0 @@
|
||||
module.exports = function makePlainError(err) {
|
||||
const obj = {};
|
||||
obj.name = err.name;
|
||||
obj.message = err.message;
|
||||
obj.stack = err.stack;
|
||||
return obj;
|
||||
};
|
||||
@@ -1,12 +0,0 @@
|
||||
module.exports = function merge(def, given) {
|
||||
if (!given) return def;
|
||||
for (const key in def) {
|
||||
if (!{}.hasOwnProperty.call(given, key)) {
|
||||
given[key] = def[key];
|
||||
} else if (given[key] === Object(given[key])) {
|
||||
given[key] = merge(def[key], given[key]);
|
||||
}
|
||||
}
|
||||
|
||||
return given;
|
||||
};
|
||||
@@ -1,17 +0,0 @@
|
||||
/**
|
||||
* Moves an element in an array *in place*
|
||||
* @param {Array} array Array to modify
|
||||
* @param {*} element Element to move
|
||||
* @param {number} newIndex Index or offset to move the element to
|
||||
* @param {boolean} [offset=false] Move the element by an offset amount rather than to a set index
|
||||
* @returns {Array}
|
||||
*/
|
||||
module.exports = function moveElementInArray(array, element, newIndex, offset = false) {
|
||||
const index = array.indexOf(element);
|
||||
newIndex = (offset ? index : 0) + newIndex;
|
||||
if (newIndex > -1 && newIndex < array.length) {
|
||||
const removedElement = array.splice(index, 1)[0];
|
||||
array.splice(newIndex, 0, removedElement);
|
||||
}
|
||||
return array;
|
||||
};
|
||||
@@ -1,14 +0,0 @@
|
||||
module.exports = function parseEmoji(text) {
|
||||
if (text.includes('%')) {
|
||||
text = decodeURIComponent(text);
|
||||
}
|
||||
if (text.includes(':')) {
|
||||
const [name, id] = text.split(':');
|
||||
return { name, id };
|
||||
} else {
|
||||
return {
|
||||
name: text,
|
||||
id: null,
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -1,16 +0,0 @@
|
||||
module.exports = function splitMessage(text, { maxLength = 1950, char = '\n', prepend = '', append = '' } = {}) {
|
||||
if (text.length <= maxLength) return text;
|
||||
const splitText = text.split(char);
|
||||
if (splitText.length === 1) throw new Error('Message exceeds the max length and contains no split characters.');
|
||||
const messages = [''];
|
||||
let msg = 0;
|
||||
for (let i = 0; i < splitText.length; i++) {
|
||||
if (messages[msg].length + splitText[i].length + 1 > maxLength) {
|
||||
messages[msg] += append;
|
||||
messages.push(prepend);
|
||||
msg++;
|
||||
}
|
||||
messages[msg] += (messages[msg].length > 0 && messages[msg] !== prepend ? char : '') + splitText[i];
|
||||
}
|
||||
return messages;
|
||||
};
|
||||
@@ -1,75 +0,0 @@
|
||||
const long = require('long');
|
||||
|
||||
/**
|
||||
* @typedef {Object} MessageSearchOptions
|
||||
* @property {string} [content] Message content
|
||||
* @property {string} [maxID] Maximum ID for the filter
|
||||
* @property {string} [minID] Minimum ID for the filter
|
||||
* @property {string} [has] One of `link`, `embed`, `file`, `video`, `image`, or `sound`,
|
||||
* or add `-` to negate (e.g. `-file`)
|
||||
* @property {ChannelResolvable} [channel] Channel to limit search to (only for guild search endpoint)
|
||||
* @property {UserResolvable} [author] Author to limit search
|
||||
* @property {string} [authorType] One of `user`, `bot`, `webhook`, or add `-` to negate (e.g. `-webhook`)
|
||||
* @property {string} [sortBy='recent'] `recent` or `relevant`
|
||||
* @property {string} [sortOrder='desc'] `asc` or `desc`
|
||||
* @property {number} [contextSize=2] How many messages to get around the matched message (0 to 2)
|
||||
* @property {number} [limit=25] Maximum number of results to get (1 to 25)
|
||||
* @property {number} [offset=0] Offset the "pages" of results (since you can only see 25 at a time)
|
||||
* @property {UserResolvable} [mentions] Mentioned user filter
|
||||
* @property {boolean} [mentionsEveryone] If everyone is mentioned
|
||||
* @property {string} [linkHostname] Filter links by hostname
|
||||
* @property {string} [embedProvider] The name of an embed provider
|
||||
* @property {string} [embedType] one of `image`, `video`, `url`, `rich`
|
||||
* @property {string} [attachmentFilename] The name of an attachment
|
||||
* @property {string} [attachmentExtension] The extension of an attachment
|
||||
* @property {Date} [before] Date to find messages before
|
||||
* @property {Date} [after] Date to find messages before
|
||||
* @property {Date} [during] Date to find messages during (range of date to date + 24 hours)
|
||||
*/
|
||||
|
||||
module.exports = function TransformSearchOptions(options, client) {
|
||||
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 = client.resolver.resolveChannelID(options.channel);
|
||||
|
||||
if (options.author) options.author = client.resolver.resolveUserID(options.author);
|
||||
|
||||
if (options.mentions) options.mentions = client.resolver.resolveUserID(options.options.mentions);
|
||||
|
||||
return {
|
||||
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,
|
||||
};
|
||||
};
|
||||
213
src/util/Util.js
Normal file
213
src/util/Util.js
Normal file
@@ -0,0 +1,213 @@
|
||||
const superagent = require('superagent');
|
||||
const botGateway = require('./Constants').Endpoints.botGateway;
|
||||
|
||||
/**
|
||||
* Contains various general-purpose utility methods. These functions are also available on the base `Discord` object.
|
||||
*/
|
||||
class Util {
|
||||
constructor() {
|
||||
throw new Error(`The ${this.constructor.name} class may not be instantiated.`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits a string into multiple chunks at a designated character that do not exceed a specific length.
|
||||
* @param {string} text Content to split
|
||||
* @param {SplitOptions} [options] Options controlling the behaviour of the split
|
||||
* @returns {string|string[]}
|
||||
*/
|
||||
static splitMessage(text, { maxLength = 1950, char = '\n', prepend = '', append = '' } = {}) {
|
||||
if (text.length <= maxLength) return text;
|
||||
const splitText = text.split(char);
|
||||
if (splitText.length === 1) throw new Error('Message exceeds the max length and contains no split characters.');
|
||||
const messages = [''];
|
||||
let msg = 0;
|
||||
for (let i = 0; i < splitText.length; i++) {
|
||||
if (messages[msg].length + splitText[i].length + 1 > maxLength) {
|
||||
messages[msg] += append;
|
||||
messages.push(prepend);
|
||||
msg++;
|
||||
}
|
||||
messages[msg] += (messages[msg].length > 0 && messages[msg] !== prepend ? char : '') + splitText[i];
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes any Discord-flavour markdown in a string.
|
||||
* @param {string} text Content to escape
|
||||
* @param {boolean} [onlyCodeBlock=false] Whether to only escape codeblocks (takes priority)
|
||||
* @param {boolean} [onlyInlineCode=false] Whether to only escape inline code
|
||||
* @returns {string}
|
||||
*/
|
||||
static escapeMarkdown(text, onlyCodeBlock = false, onlyInlineCode = false) {
|
||||
if (onlyCodeBlock) return text.replace(/```/g, '`\u200b``');
|
||||
if (onlyInlineCode) return text.replace(/\\(`|\\)/g, '$1').replace(/(`|\\)/g, '\\$1');
|
||||
return text.replace(/\\(\*|_|`|~|\\)/g, '$1').replace(/(\*|_|`|~|\\)/g, '\\$1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the recommended shard count from Discord.
|
||||
* @param {string} token Discord auth token
|
||||
* @param {number} [guildsPerShard=1000] Number of guilds per shard
|
||||
* @returns {Promise<number>} the recommended number of shards
|
||||
*/
|
||||
static fetchRecommendedShards(token, guildsPerShard = 1000) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!token) throw new Error('A token must be provided.');
|
||||
superagent.get(botGateway)
|
||||
.set('Authorization', `Bot ${token.replace(/^Bot\s*/i, '')}`)
|
||||
.end((err, res) => {
|
||||
if (err) reject(err);
|
||||
resolve(res.body.shards * (1000 / guildsPerShard));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses emoji info out of a string. The string must be one of:
|
||||
* - A UTF-8 emoji (no ID)
|
||||
* - A URL-encoded UTF-8 emoji (no ID)
|
||||
* - A Discord custom emoji (`<:name:id>`)
|
||||
* @param {string} text Emoji string to parse
|
||||
* @returns {Object} Object with `name` and `id` properties
|
||||
* @private
|
||||
*/
|
||||
static parseEmoji(text) {
|
||||
if (text.includes('%')) text = decodeURIComponent(text);
|
||||
if (text.includes(':')) {
|
||||
const [name, id] = text.split(':');
|
||||
return { name, id };
|
||||
} else {
|
||||
return {
|
||||
name: text,
|
||||
id: null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Does some weird shit to test the equality of two arrays' elements.
|
||||
* <warn>Do not use. This will give your dog/cat severe untreatable cancer of the everything. RIP Fluffykins.</warn>
|
||||
* @param {Array<*>} a ????
|
||||
* @param {Array<*>} b ?????????
|
||||
* @returns {boolean}
|
||||
* @private
|
||||
*/
|
||||
static arraysEqual(a, b) {
|
||||
if (a === b) return true;
|
||||
if (a.length !== b.length) return false;
|
||||
|
||||
for (const itemInd in a) {
|
||||
const item = a[itemInd];
|
||||
const ind = b.indexOf(item);
|
||||
if (ind) b.splice(ind, 1);
|
||||
}
|
||||
|
||||
return b.length === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shallow-copies an object with its class/prototype intact.
|
||||
* @param {Object} obj Object to clone
|
||||
* @returns {Object}
|
||||
* @private
|
||||
*/
|
||||
static cloneObject(obj) {
|
||||
return Object.assign(Object.create(obj), obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets default properties on an object that aren't already specified.
|
||||
* @param {Object} def Default properties
|
||||
* @param {Object} given Object to assign defaults to
|
||||
* @returns {Object}
|
||||
* @private
|
||||
*/
|
||||
static mergeDefault(def, given) {
|
||||
if (!given) return def;
|
||||
for (const key in def) {
|
||||
if (!{}.hasOwnProperty.call(given, key)) {
|
||||
given[key] = def[key];
|
||||
} else if (given[key] === Object(given[key])) {
|
||||
given[key] = this.mergeDefault(def[key], given[key]);
|
||||
}
|
||||
}
|
||||
|
||||
return given;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an ArrayBuffer or string to a Buffer.
|
||||
* @param {ArrayBuffer|string} ab ArrayBuffer to convert
|
||||
* @returns {Buffer}
|
||||
* @private
|
||||
*/
|
||||
static convertToBuffer(ab) {
|
||||
if (typeof ab === 'string') ab = this.str2ab(ab);
|
||||
return Buffer.from(ab);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a string to an ArrayBuffer.
|
||||
* @param {string} str String to convert
|
||||
* @returns {ArrayBuffer}
|
||||
* @private
|
||||
*/
|
||||
static str2ab(str) {
|
||||
const buffer = new ArrayBuffer(str.length * 2);
|
||||
const view = new Uint16Array(buffer);
|
||||
for (var i = 0, strLen = str.length; i < strLen; i++) view[i] = str.charCodeAt(i);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes an Error from a plain info object
|
||||
* @param {Object} obj Error info
|
||||
* @param {string} obj.name Error type
|
||||
* @param {string} obj.message Message for the error
|
||||
* @param {string} obj.stack Stack for the error
|
||||
* @returns {Error}
|
||||
* @private
|
||||
*/
|
||||
static makeError(obj) {
|
||||
const err = new Error(obj.message);
|
||||
err.name = obj.name;
|
||||
err.stack = obj.stack;
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a plain error info object from an Error
|
||||
* @param {Error} err Error to get info from
|
||||
* @returns {Object}
|
||||
* @private
|
||||
*/
|
||||
static makePlainError(err) {
|
||||
const obj = {};
|
||||
obj.name = err.name;
|
||||
obj.message = err.message;
|
||||
obj.stack = err.stack;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves an element in an array *in place*
|
||||
* @param {Array<*>} array Array to modify
|
||||
* @param {*} element Element to move
|
||||
* @param {number} newIndex Index or offset to move the element to
|
||||
* @param {boolean} [offset=false] Move the element by an offset amount rather than to a set index
|
||||
* @returns {Array<*>}
|
||||
* @private
|
||||
*/
|
||||
static moveElementInArray(array, element, newIndex, offset = false) {
|
||||
const index = array.indexOf(element);
|
||||
newIndex = (offset ? index : 0) + newIndex;
|
||||
if (newIndex > -1 && newIndex < array.length) {
|
||||
const removedElement = array.splice(index, 1)[0];
|
||||
array.splice(newIndex, 0, removedElement);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Util;
|
||||
2
typings
2
typings
Submodule typings updated: 3dbeb51fd2...997abfd2d5
Reference in New Issue
Block a user