mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 20:13:30 +01:00
Move all util methods into class
Remove TransformMessageOptions altogether
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
const os = require('os');
|
const os = require('os');
|
||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
const mergeDefault = require('../util/MergeDefault');
|
|
||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
|
const Util = require('../util/Util');
|
||||||
const RESTManager = require('./rest/RESTManager');
|
const RESTManager = require('./rest/RESTManager');
|
||||||
const ClientDataManager = require('./ClientDataManager');
|
const ClientDataManager = require('./ClientDataManager');
|
||||||
const ClientManager = require('./ClientManager');
|
const ClientManager = require('./ClientManager');
|
||||||
@@ -32,7 +32,7 @@ class Client extends EventEmitter {
|
|||||||
* The options the client was instantiated with
|
* The options the client was instantiated with
|
||||||
* @type {ClientOptions}
|
* @type {ClientOptions}
|
||||||
*/
|
*/
|
||||||
this.options = mergeDefault(Constants.DefaultOptions, options);
|
this.options = Util.mergeDefault(Constants.DefaultOptions, options);
|
||||||
this._validateOptions();
|
this._validateOptions();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
const cloneObject = require('../util/CloneObject');
|
const Util = require('../util/Util');
|
||||||
const Guild = require('../structures/Guild');
|
const Guild = require('../structures/Guild');
|
||||||
const User = require('../structures/User');
|
const User = require('../structures/User');
|
||||||
const DMChannel = require('../structures/DMChannel');
|
const DMChannel = require('../structures/DMChannel');
|
||||||
@@ -110,7 +110,7 @@ class ClientDataManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateGuild(currentGuild, newData) {
|
updateGuild(currentGuild, newData) {
|
||||||
const oldGuild = cloneObject(currentGuild);
|
const oldGuild = Util.cloneObject(currentGuild);
|
||||||
currentGuild.setup(newData);
|
currentGuild.setup(newData);
|
||||||
if (this.pastReady) this.client.emit(Constants.Events.GUILD_UPDATE, oldGuild, currentGuild);
|
if (this.pastReady) this.client.emit(Constants.Events.GUILD_UPDATE, oldGuild, currentGuild);
|
||||||
}
|
}
|
||||||
@@ -120,7 +120,7 @@ class ClientDataManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateEmoji(currentEmoji, newData) {
|
updateEmoji(currentEmoji, newData) {
|
||||||
const oldEmoji = cloneObject(currentEmoji);
|
const oldEmoji = Util.cloneObject(currentEmoji);
|
||||||
currentEmoji.setup(newData);
|
currentEmoji.setup(newData);
|
||||||
this.client.emit(Constants.Events.GUILD_EMOJI_UPDATE, oldEmoji, currentEmoji);
|
this.client.emit(Constants.Events.GUILD_EMOJI_UPDATE, oldEmoji, currentEmoji);
|
||||||
return currentEmoji;
|
return currentEmoji;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ const fs = require('fs');
|
|||||||
const request = require('superagent');
|
const request = require('superagent');
|
||||||
|
|
||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
const convertArrayBuffer = require('../util/ConvertArrayBuffer');
|
const convertToBuffer = require('../util/Util').convertToBuffer;
|
||||||
const User = require('../structures/User');
|
const User = require('../structures/User');
|
||||||
const Message = require('../structures/Message');
|
const Message = require('../structures/Message');
|
||||||
const Guild = require('../structures/Guild');
|
const Guild = require('../structures/Guild');
|
||||||
@@ -268,7 +268,7 @@ class ClientDataResolver {
|
|||||||
*/
|
*/
|
||||||
resolveBuffer(resource) {
|
resolveBuffer(resource) {
|
||||||
if (resource instanceof Buffer) return Promise.resolve(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') {
|
if (typeof resource === 'string') {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@@ -277,7 +277,7 @@ class ClientDataResolver {
|
|||||||
if (this.client.browser) req.responseType('arraybuffer');
|
if (this.client.browser) req.responseType('arraybuffer');
|
||||||
req.end((err, res) => {
|
req.end((err, res) => {
|
||||||
if (err) return reject(err);
|
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.'));
|
if (!(res.body instanceof Buffer)) return reject(new TypeError('The response body isn\'t a Buffer.'));
|
||||||
return resolve(res.body);
|
return resolve(res.body);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
const Webhook = require('../structures/Webhook');
|
const Webhook = require('../structures/Webhook');
|
||||||
const RESTManager = require('./rest/RESTManager');
|
const RESTManager = require('./rest/RESTManager');
|
||||||
const ClientDataResolver = require('./ClientDataResolver');
|
const ClientDataResolver = require('./ClientDataResolver');
|
||||||
const mergeDefault = require('../util/MergeDefault');
|
|
||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
|
const Util = require('../util/Util');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Webhook Client
|
* The Webhook Client
|
||||||
@@ -25,7 +25,7 @@ class WebhookClient extends Webhook {
|
|||||||
* The options the client was instantiated with
|
* The options the client was instantiated with
|
||||||
* @type {ClientOptions}
|
* @type {ClientOptions}
|
||||||
*/
|
*/
|
||||||
this.options = mergeDefault(Constants.DefaultOptions, options);
|
this.options = Util.mergeDefault(Constants.DefaultOptions, options);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The REST manager of the client
|
* The REST manager of the client
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
const Action = require('./Action');
|
const Action = require('./Action');
|
||||||
const Constants = require('../../util/Constants');
|
const Constants = require('../../util/Constants');
|
||||||
const cloneObject = require('../../util/CloneObject');
|
const Util = require('../../util/Util');
|
||||||
|
|
||||||
class ChannelUpdateAction extends Action {
|
class ChannelUpdateAction extends Action {
|
||||||
handle(data) {
|
handle(data) {
|
||||||
@@ -8,7 +8,7 @@ class ChannelUpdateAction extends Action {
|
|||||||
|
|
||||||
const channel = client.channels.get(data.id);
|
const channel = client.channels.get(data.id);
|
||||||
if (channel) {
|
if (channel) {
|
||||||
const oldChannel = cloneObject(channel);
|
const oldChannel = Util.cloneObject(channel);
|
||||||
channel.setup(data);
|
channel.setup(data);
|
||||||
client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel);
|
client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel);
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
const Action = require('./Action');
|
const Action = require('./Action');
|
||||||
const Constants = require('../../util/Constants');
|
const Constants = require('../../util/Constants');
|
||||||
const cloneObject = require('../../util/CloneObject');
|
const Util = require('../../util/Util');
|
||||||
|
|
||||||
class GuildRoleUpdateAction extends Action {
|
class GuildRoleUpdateAction extends Action {
|
||||||
handle(data) {
|
handle(data) {
|
||||||
@@ -13,7 +13,7 @@ class GuildRoleUpdateAction extends Action {
|
|||||||
|
|
||||||
const role = guild.roles.get(roleData.id);
|
const role = guild.roles.get(roleData.id);
|
||||||
if (role) {
|
if (role) {
|
||||||
oldRole = cloneObject(role);
|
oldRole = Util.cloneObject(role);
|
||||||
role.setup(data.role);
|
role.setup(data.role);
|
||||||
client.emit(Constants.Events.GUILD_ROLE_UPDATE, oldRole, role);
|
client.emit(Constants.Events.GUILD_ROLE_UPDATE, oldRole, role);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
const Action = require('./Action');
|
const Action = require('./Action');
|
||||||
const Constants = require('../../util/Constants');
|
const Constants = require('../../util/Constants');
|
||||||
const cloneObject = require('../../util/CloneObject');
|
const Util = require('../../util/Util');
|
||||||
|
|
||||||
class GuildUpdateAction extends Action {
|
class GuildUpdateAction extends Action {
|
||||||
handle(data) {
|
handle(data) {
|
||||||
@@ -8,7 +8,7 @@ class GuildUpdateAction extends Action {
|
|||||||
|
|
||||||
const guild = client.guilds.get(data.id);
|
const guild = client.guilds.get(data.id);
|
||||||
if (guild) {
|
if (guild) {
|
||||||
const oldGuild = cloneObject(guild);
|
const oldGuild = Util.cloneObject(guild);
|
||||||
guild.setup(data);
|
guild.setup(data);
|
||||||
client.emit(Constants.Events.GUILD_UPDATE, oldGuild, guild);
|
client.emit(Constants.Events.GUILD_UPDATE, oldGuild, guild);
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
const Action = require('./Action');
|
const Action = require('./Action');
|
||||||
const Constants = require('../../util/Constants');
|
const Constants = require('../../util/Constants');
|
||||||
const cloneObject = require('../../util/CloneObject');
|
const Util = require('../../util/Util');
|
||||||
|
|
||||||
class MessageUpdateAction extends Action {
|
class MessageUpdateAction extends Action {
|
||||||
handle(data) {
|
handle(data) {
|
||||||
@@ -10,7 +10,7 @@ class MessageUpdateAction extends Action {
|
|||||||
if (channel) {
|
if (channel) {
|
||||||
const message = channel.messages.get(data.id);
|
const message = channel.messages.get(data.id);
|
||||||
if (message) {
|
if (message) {
|
||||||
const oldMessage = cloneObject(message);
|
const oldMessage = Util.cloneObject(message);
|
||||||
message.patch(data);
|
message.patch(data);
|
||||||
message._edits.unshift(oldMessage);
|
message._edits.unshift(oldMessage);
|
||||||
client.emit(Constants.Events.MESSAGE_UPDATE, oldMessage, message);
|
client.emit(Constants.Events.MESSAGE_UPDATE, oldMessage, message);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
const Action = require('./Action');
|
const Action = require('./Action');
|
||||||
const Constants = require('../../util/Constants');
|
const Constants = require('../../util/Constants');
|
||||||
const cloneObject = require('../../util/CloneObject');
|
const Util = require('../../util/Util');
|
||||||
|
|
||||||
class UserUpdateAction extends Action {
|
class UserUpdateAction extends Action {
|
||||||
handle(data) {
|
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.user.patch(data);
|
||||||
client.emit(Constants.Events.USER_UPDATE, oldUser, client.user);
|
client.emit(Constants.Events.USER_UPDATE, oldUser, client.user);
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
const querystring = require('querystring');
|
const querystring = require('querystring');
|
||||||
|
const long = require('long');
|
||||||
const Constants = require('../../util/Constants');
|
const Constants = require('../../util/Constants');
|
||||||
const Collection = require('../../util/Collection');
|
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 Snowflake = require('../../util/Snowflake');
|
||||||
|
const Util = require('../../util/Util');
|
||||||
|
|
||||||
const User = require('../../structures/User');
|
const User = require('../../structures/User');
|
||||||
const GuildMember = require('../../structures/GuildMember');
|
const GuildMember = require('../../structures/GuildMember');
|
||||||
@@ -66,7 +64,7 @@ class RESTMethods {
|
|||||||
|
|
||||||
// Wrap everything in a code block
|
// Wrap everything in a code block
|
||||||
if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) {
|
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\`\`\``;
|
content = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n${content}\n\`\`\``;
|
||||||
if (split) {
|
if (split) {
|
||||||
split.prepend = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n`;
|
split.prepend = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n`;
|
||||||
@@ -88,7 +86,7 @@ class RESTMethods {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Split the content
|
// 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') {
|
} else if (reply && !(channel instanceof User || channel instanceof GuildMember) && channel.type !== 'dm') {
|
||||||
const id = this.client.resolver.resolveUserID(reply);
|
const id = this.client.resolver.resolveUserID(reply);
|
||||||
content = `<@${reply instanceof GuildMember && reply.nickname ? '!' : ''}${id}>`;
|
content = `<@${reply instanceof GuildMember && reply.nickname ? '!' : ''}${id}>`;
|
||||||
@@ -125,7 +123,7 @@ class RESTMethods {
|
|||||||
|
|
||||||
// Wrap everything in a code block
|
// Wrap everything in a code block
|
||||||
if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) {
|
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\`\`\``;
|
content = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n${content}\n\`\`\``;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,9 +166,46 @@ class RESTMethods {
|
|||||||
}
|
}
|
||||||
|
|
||||||
search(target, options) {
|
search(target, options) {
|
||||||
options = transformSearchOptions(options, this.client);
|
if (options.before) {
|
||||||
for (const key in options) if (options[key] === undefined) delete options[key];
|
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('&');
|
const queryString = (querystring.stringify(options).match(/[^=&?]+=[^=&?]+/g) || []).join('&');
|
||||||
|
|
||||||
let type;
|
let type;
|
||||||
@@ -736,7 +771,7 @@ class RESTMethods {
|
|||||||
this.client.actions.MessageReactionAdd.handle({
|
this.client.actions.MessageReactionAdd.handle({
|
||||||
user_id: this.client.user.id,
|
user_id: this.client.user.id,
|
||||||
message_id: message.id,
|
message_id: message.id,
|
||||||
emoji: parseEmoji(emoji),
|
emoji: Util.parseEmoji(emoji),
|
||||||
channel_id: message.channel.id,
|
channel_id: message.channel.id,
|
||||||
}).reaction
|
}).reaction
|
||||||
);
|
);
|
||||||
@@ -751,7 +786,7 @@ class RESTMethods {
|
|||||||
this.client.actions.MessageReactionRemove.handle({
|
this.client.actions.MessageReactionRemove.handle({
|
||||||
user_id: user,
|
user_id: user,
|
||||||
message_id: message.id,
|
message_id: message.id,
|
||||||
emoji: parseEmoji(emoji),
|
emoji: Util.parseEmoji(emoji),
|
||||||
channel_id: message.channel.id,
|
channel_id: message.channel.id,
|
||||||
}).reaction
|
}).reaction
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
const Collection = require('../../util/Collection');
|
const Collection = require('../../util/Collection');
|
||||||
const mergeDefault = require('../../util/MergeDefault');
|
|
||||||
const Constants = require('../../util/Constants');
|
const Constants = require('../../util/Constants');
|
||||||
|
const Util = require('../../util/Util');
|
||||||
const VoiceConnection = require('./VoiceConnection');
|
const VoiceConnection = require('./VoiceConnection');
|
||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ class ClientVoiceManager {
|
|||||||
throw new Error('You do not have permission to join this voice channel.');
|
throw new Error('You do not have permission to join this voice channel.');
|
||||||
}
|
}
|
||||||
|
|
||||||
options = mergeDefault({
|
options = Util.mergeDefault({
|
||||||
guild_id: channel.guild.id,
|
guild_id: channel.guild.id,
|
||||||
channel_id: channel.id,
|
channel_id: channel.id,
|
||||||
self_mute: false,
|
self_mute: false,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const browser = require('os').platform() === 'browser';
|
const browser = require('os').platform() === 'browser';
|
||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
const Constants = require('../../util/Constants');
|
const Constants = require('../../util/Constants');
|
||||||
const convertArrayBuffer = require('../../util/ConvertArrayBuffer');
|
const convertToBuffer = require('../../util/Util').convertToBuffer;
|
||||||
const pako = require('pako');
|
const pako = require('pako');
|
||||||
const zlib = require('zlib');
|
const zlib = require('zlib');
|
||||||
const PacketManager = require('./packets/WebSocketPacketManager');
|
const PacketManager = require('./packets/WebSocketPacketManager');
|
||||||
@@ -279,7 +279,7 @@ class WebSocketManager extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
parseEventData(data) {
|
parseEventData(data) {
|
||||||
if (erlpack) {
|
if (erlpack) {
|
||||||
if (data instanceof ArrayBuffer) data = convertArrayBuffer(data);
|
if (data instanceof ArrayBuffer) data = convertToBuffer(data);
|
||||||
return erlpack.unpack(data);
|
return erlpack.unpack(data);
|
||||||
} else {
|
} else {
|
||||||
if (data instanceof ArrayBuffer) data = pako.inflate(data, { to: 'string' });
|
if (data instanceof ArrayBuffer) data = pako.inflate(data, { to: 'string' });
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
const AbstractHandler = require('./AbstractHandler');
|
const AbstractHandler = require('./AbstractHandler');
|
||||||
const Constants = require('../../../../util/Constants');
|
const Constants = require('../../../../util/Constants');
|
||||||
const cloneObject = require('../../../../util/CloneObject');
|
const Util = require('../../../../util/Util');
|
||||||
|
|
||||||
class PresenceUpdateHandler extends AbstractHandler {
|
class PresenceUpdateHandler extends AbstractHandler {
|
||||||
handle(packet) {
|
handle(packet) {
|
||||||
@@ -18,7 +18,7 @@ class PresenceUpdateHandler extends AbstractHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const oldUser = cloneObject(user);
|
const oldUser = Util.cloneObject(user);
|
||||||
user.patch(data.user);
|
user.patch(data.user);
|
||||||
if (!user.equals(oldUser)) {
|
if (!user.equals(oldUser)) {
|
||||||
client.emit(Constants.Events.USER_UPDATE, oldUser, user);
|
client.emit(Constants.Events.USER_UPDATE, oldUser, user);
|
||||||
@@ -40,9 +40,9 @@ class PresenceUpdateHandler extends AbstractHandler {
|
|||||||
guild._setPresence(user.id, data);
|
guild._setPresence(user.id, data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const oldMember = cloneObject(member);
|
const oldMember = Util.cloneObject(member);
|
||||||
if (member.presence) {
|
if (member.presence) {
|
||||||
oldMember.frozenPresence = cloneObject(member.presence);
|
oldMember.frozenPresence = Util.cloneObject(member.presence);
|
||||||
}
|
}
|
||||||
guild._setPresence(user.id, data);
|
guild._setPresence(user.id, data);
|
||||||
client.emit(Constants.Events.PRESENCE_UPDATE, oldMember, member);
|
client.emit(Constants.Events.PRESENCE_UPDATE, oldMember, member);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const AbstractHandler = require('./AbstractHandler');
|
const AbstractHandler = require('./AbstractHandler');
|
||||||
|
|
||||||
const Constants = require('../../../../util/Constants');
|
const Constants = require('../../../../util/Constants');
|
||||||
const cloneObject = require('../../../../util/CloneObject');
|
const Util = require('../../../../util/Util');
|
||||||
|
|
||||||
class VoiceStateUpdateHandler extends AbstractHandler {
|
class VoiceStateUpdateHandler extends AbstractHandler {
|
||||||
handle(packet) {
|
handle(packet) {
|
||||||
@@ -12,7 +12,7 @@ class VoiceStateUpdateHandler extends AbstractHandler {
|
|||||||
if (guild) {
|
if (guild) {
|
||||||
const member = guild.members.get(data.user_id);
|
const member = guild.members.get(data.user_id);
|
||||||
if (member) {
|
if (member) {
|
||||||
const oldVoiceChannelMember = cloneObject(member);
|
const oldVoiceChannelMember = Util.cloneObject(member);
|
||||||
if (member.voiceChannel && member.voiceChannel.id !== data.channel_id) {
|
if (member.voiceChannel && member.voiceChannel.id !== data.channel_id) {
|
||||||
member.voiceChannel.members.delete(oldVoiceChannelMember.id);
|
member.voiceChannel.members.delete(oldVoiceChannelMember.id);
|
||||||
}
|
}
|
||||||
|
|||||||
22
src/index.js
22
src/index.js
@@ -1,17 +1,28 @@
|
|||||||
|
const Util = require('./util/Util');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
// "Root" classes (starting points)
|
||||||
Client: require('./client/Client'),
|
Client: require('./client/Client'),
|
||||||
WebhookClient: require('./client/WebhookClient'),
|
|
||||||
Shard: require('./sharding/Shard'),
|
Shard: require('./sharding/Shard'),
|
||||||
ShardClientUtil: require('./sharding/ShardClientUtil'),
|
ShardClientUtil: require('./sharding/ShardClientUtil'),
|
||||||
ShardingManager: require('./sharding/ShardingManager'),
|
ShardingManager: require('./sharding/ShardingManager'),
|
||||||
|
WebhookClient: require('./client/WebhookClient'),
|
||||||
|
|
||||||
|
// Utilities
|
||||||
Collection: require('./util/Collection'),
|
Collection: require('./util/Collection'),
|
||||||
splitMessage: require('./util/SplitMessage'),
|
Constants: require('./util/Constants'),
|
||||||
escapeMarkdown: require('./util/EscapeMarkdown'),
|
|
||||||
fetchRecommendedShards: require('./util/FetchRecommendedShards'),
|
|
||||||
Snowflake: require('./util/Snowflake'),
|
Snowflake: require('./util/Snowflake'),
|
||||||
SnowflakeUtil: 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'),
|
Channel: require('./structures/Channel'),
|
||||||
ClientOAuth2Application: require('./structures/ClientOAuth2Application'),
|
ClientOAuth2Application: require('./structures/ClientOAuth2Application'),
|
||||||
ClientUser: require('./structures/ClientUser'),
|
ClientUser: require('./structures/ClientUser'),
|
||||||
@@ -41,9 +52,6 @@ module.exports = {
|
|||||||
User: require('./structures/User'),
|
User: require('./structures/User'),
|
||||||
VoiceChannel: require('./structures/VoiceChannel'),
|
VoiceChannel: require('./structures/VoiceChannel'),
|
||||||
Webhook: require('./structures/Webhook'),
|
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
|
if (require('os').platform() === 'browser') window.Discord = module.exports; // eslint-disable-line no-undef
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
const childProcess = require('child_process');
|
const childProcess = require('child_process');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const makeError = require('../util/MakeError');
|
const Util = require('../util/Util');
|
||||||
const makePlainError = require('../util/MakePlainError');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a Shard spawned by the ShardingManager.
|
* Represents a Shard spawned by the ShardingManager.
|
||||||
@@ -110,7 +109,7 @@ class Shard {
|
|||||||
if (!message || message._eval !== script) return;
|
if (!message || message._eval !== script) return;
|
||||||
this.process.removeListener('message', listener);
|
this.process.removeListener('message', listener);
|
||||||
this._evals.delete(script);
|
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);
|
this.process.on('message', listener);
|
||||||
|
|
||||||
@@ -136,7 +135,7 @@ class Shard {
|
|||||||
if (message._sFetchProp) {
|
if (message._sFetchProp) {
|
||||||
this.manager.fetchClientValues(message._sFetchProp).then(
|
this.manager.fetchClientValues(message._sFetchProp).then(
|
||||||
results => this.send({ _sFetchProp: message._sFetchProp, _result: results }),
|
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;
|
return;
|
||||||
}
|
}
|
||||||
@@ -145,7 +144,7 @@ class Shard {
|
|||||||
if (message._sEval) {
|
if (message._sEval) {
|
||||||
this.manager.broadcastEval(message._sEval).then(
|
this.manager.broadcastEval(message._sEval).then(
|
||||||
results => this.send({ _sEval: message._sEval, _result: results }),
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
const makeError = require('../util/MakeError');
|
const Util = require('../util/Util');
|
||||||
const makePlainError = require('../util/MakePlainError');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper class for sharded clients spawned as a child process, such as from a ShardingManager
|
* Helper class for sharded clients spawned as a child process, such as from a ShardingManager
|
||||||
@@ -59,7 +58,7 @@ class ShardClientUtil {
|
|||||||
const listener = message => {
|
const listener = message => {
|
||||||
if (!message || message._sFetchProp !== prop) return;
|
if (!message || message._sFetchProp !== prop) return;
|
||||||
process.removeListener('message', listener);
|
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);
|
process.on('message', listener);
|
||||||
|
|
||||||
@@ -80,7 +79,7 @@ class ShardClientUtil {
|
|||||||
const listener = message => {
|
const listener = message => {
|
||||||
if (!message || message._sEval !== script) return;
|
if (!message || message._sEval !== script) return;
|
||||||
process.removeListener('message', listener);
|
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);
|
process.on('message', listener);
|
||||||
|
|
||||||
@@ -107,7 +106,7 @@ class ShardClientUtil {
|
|||||||
try {
|
try {
|
||||||
this._respond('eval', { _eval: message._eval, _result: this.client._eval(message._eval) });
|
this._respond('eval', { _eval: message._eval, _result: this.client._eval(message._eval) });
|
||||||
} catch (err) {
|
} 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 path = require('path');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
const mergeDefault = require('../util/MergeDefault');
|
|
||||||
const Shard = require('./Shard');
|
const Shard = require('./Shard');
|
||||||
const Collection = require('../util/Collection');
|
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
|
* 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 = {}) {
|
constructor(file, options = {}) {
|
||||||
super();
|
super();
|
||||||
options = mergeDefault({
|
options = Util.mergeDefault({
|
||||||
totalShards: 'auto',
|
totalShards: 'auto',
|
||||||
respawn: true,
|
respawn: true,
|
||||||
shardArgs: [],
|
shardArgs: [],
|
||||||
@@ -105,7 +104,7 @@ class ShardingManager extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
spawn(amount = this.totalShards, delay = 5500) {
|
spawn(amount = this.totalShards, delay = 5500) {
|
||||||
if (amount === 'auto') {
|
if (amount === 'auto') {
|
||||||
return fetchRecommendedShards(this.token).then(count => {
|
return Util.fetchRecommendedShards(this.token).then(count => {
|
||||||
this.totalShards = count;
|
this.totalShards = count;
|
||||||
return this._spawn(count, delay);
|
return this._spawn(count, delay);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,9 +5,7 @@ const Presence = require('./Presence').Presence;
|
|||||||
const GuildMember = require('./GuildMember');
|
const GuildMember = require('./GuildMember');
|
||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
const Collection = require('../util/Collection');
|
const Collection = require('../util/Collection');
|
||||||
const cloneObject = require('../util/CloneObject');
|
const Util = require('../util/Util');
|
||||||
const arraysEqual = require('../util/ArraysEqual');
|
|
||||||
const moveElementInArray = require('../util/MoveElementInArray');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a guild (or a server) on Discord.
|
* Represents a guild (or a server) on Discord.
|
||||||
@@ -681,7 +679,7 @@ class Guild {
|
|||||||
let updatedRoles = Object.assign([], this.roles.array()
|
let updatedRoles = Object.assign([], this.roles.array()
|
||||||
.sort((r1, r2) => r1.position !== r2.position ? r1.position - r2.position : r1.id - r2.id));
|
.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 }));
|
updatedRoles = updatedRoles.map((r, i) => ({ id: r.id, position: i }));
|
||||||
return this.client.rest.methods.setRolePositions(this.id, updatedRoles);
|
return this.client.rest.methods.setRolePositions(this.id, updatedRoles);
|
||||||
@@ -771,7 +769,7 @@ class Guild {
|
|||||||
this.memberCount === guild.member_count &&
|
this.memberCount === guild.member_count &&
|
||||||
this.large === guild.large &&
|
this.large === guild.large &&
|
||||||
this.icon === guild.icon &&
|
this.icon === guild.icon &&
|
||||||
arraysEqual(this.features, guild.features) &&
|
Util.arraysEqual(this.features, guild.features) &&
|
||||||
this.ownerID === guild.owner_id &&
|
this.ownerID === guild.owner_id &&
|
||||||
this.verificationLevel === guild.verification_level &&
|
this.verificationLevel === guild.verification_level &&
|
||||||
this.embedEnabled === guild.embed_enabled;
|
this.embedEnabled === guild.embed_enabled;
|
||||||
@@ -837,12 +835,12 @@ class Guild {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_updateMember(member, data) {
|
_updateMember(member, data) {
|
||||||
const oldMember = cloneObject(member);
|
const oldMember = Util.cloneObject(member);
|
||||||
|
|
||||||
if (data.roles) member._roles = data.roles;
|
if (data.roles) member._roles = data.roles;
|
||||||
if (typeof data.nick !== 'undefined') member.nickname = data.nick;
|
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) {
|
if (this.client.ws.status === Constants.Status.READY && notSame) {
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
const Attachment = require('./MessageAttachment');
|
const Attachment = require('./MessageAttachment');
|
||||||
const Embed = require('./MessageEmbed');
|
const Embed = require('./MessageEmbed');
|
||||||
const MessageReaction = require('./MessageReaction');
|
const MessageReaction = require('./MessageReaction');
|
||||||
|
const Util = require('../util/Util');
|
||||||
const Collection = require('../util/Collection');
|
const Collection = require('../util/Collection');
|
||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
const escapeMarkdown = require('../util/EscapeMarkdown');
|
|
||||||
let GuildMember;
|
let GuildMember;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -409,7 +409,7 @@ class Message {
|
|||||||
* @returns {Promise<Message>}
|
* @returns {Promise<Message>}
|
||||||
*/
|
*/
|
||||||
editCode(lang, content) {
|
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\`\`\``);
|
return this.edit(`\`\`\`${lang || ''}\n${content}\n\`\`\``);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -223,6 +223,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.
|
* Performs a search within the channel.
|
||||||
* @param {MessageSearchOptions} [options={}] Options to pass to the search
|
* @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;
|
|
||||||
};
|
|
||||||
@@ -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;
|
||||||
Reference in New Issue
Block a user