mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 01:23:31 +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 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');
|
||||
@@ -32,7 +32,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();
|
||||
|
||||
/**
|
||||
|
||||
@@ -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');
|
||||
@@ -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');
|
||||
@@ -268,7 +268,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,7 +277,7 @@ 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);
|
||||
});
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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');
|
||||
@@ -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;
|
||||
@@ -736,7 +771,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
|
||||
);
|
||||
@@ -751,7 +786,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
|
||||
);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const Collection = require('../../util/Collection');
|
||||
const mergeDefault = require('../../util/MergeDefault');
|
||||
const Constants = require('../../util/Constants');
|
||||
const Util = require('../../util/Util');
|
||||
const VoiceConnection = require('./VoiceConnection');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
|
||||
@@ -58,7 +58,7 @@ class ClientVoiceManager {
|
||||
throw new Error('You do not have permission to join this voice channel.');
|
||||
}
|
||||
|
||||
options = mergeDefault({
|
||||
options = Util.mergeDefault({
|
||||
guild_id: channel.guild.id,
|
||||
channel_id: channel.id,
|
||||
self_mute: false,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user