From 625cba95ad551e6f953b8ae4106f4b455e01d570 Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Wed, 28 Jul 2021 05:26:16 -0500 Subject: [PATCH 01/11] sticker sending ability --- index.d.ts | 4 +++- lib/Client.js | 4 +++- lib/structures/PrivateChannel.js | 1 + lib/structures/TextChannel.js | 1 + 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 211b3e785..95110b019 100644 --- a/index.d.ts +++ b/index.d.ts @@ -62,6 +62,7 @@ declare namespace Eris { messageReference?: MessageReferenceReply; /** @deprecated */ messageReferenceID?: string; + stickerIDS?: string[]; tts?: boolean; }; type ImageFormat = "jpg" | "jpeg" | "png" | "gif" | "webp"; @@ -2300,9 +2301,10 @@ declare namespace Eris { reactions: { [s: string]: { count: number; me: boolean } }; referencedMessage?: Message | null; roleMentions: string[]; + stickerItems?: StickerItems[]; /** @deprecated */ stickers?: Sticker[]; - stickerItems?: StickerItems[]; + timestamp: number; tts: boolean; type: number; diff --git a/lib/Client.js b/lib/Client.js index 9cc885869..2de00e89f 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -588,6 +588,7 @@ class Client extends EventEmitter { * @arg {String} [content.messageReference.guildID] The guild ID of the referenced message * @arg {String} content.messageReference.messageID The message ID of the referenced message. This cannot reference a system message * @arg {String} [content.messageReferenceID] [DEPRECATED] The ID of the message should be replied to. Use `messageReference` instead + * @arg {Array} [content.stickerIDS] An array of ids corresponding to stickers to send * @arg {Boolean} [content.tts] Set the message TTS flag * @arg {Object | Array} [file] A file object (or an Array of them) * @arg {Buffer} file.file A buffer containing file data @@ -609,6 +610,7 @@ class Client extends EventEmitter { content.embeds = [content.embed]; } content.allowed_mentions = this._formatAllowedMentions(content.allowedMentions); + content.sticker_ids = content.stickerIDS; if(content.messageReference) { content.message_reference = content.messageReference; if(content.messageReference.messageID !== undefined) { @@ -2735,7 +2737,7 @@ class Client extends EventEmitter { _formatImage(url, format, size) { if(!format || !Constants.ImageFormats.includes(format.toLowerCase())) { - format = url.includes("/a_") ? "gif": this.options.defaultImageFormat; + format = url.includes("/a_") ? "gif" : this.options.defaultImageFormat; } if(!size || size < Constants.ImageSizeBoundaries.MINIMUM || size > Constants.ImageSizeBoundaries.MAXIMUM || (size & (size - 1))) { size = this.options.defaultImageSize; diff --git a/lib/structures/PrivateChannel.js b/lib/structures/PrivateChannel.js index 1eff5c616..fb0615591 100644 --- a/lib/structures/PrivateChannel.js +++ b/lib/structures/PrivateChannel.js @@ -55,6 +55,7 @@ class PrivateChannel extends Channel { * @arg {String} [content.messageReference.guildID] The guild ID of the referenced message * @arg {String} content.messageReference.messageID The message ID of the referenced message. This cannot reference a system message * @arg {String} [content.messageReferenceID] [DEPRECATED] The ID of the message should be replied to. Use `messageReference` instead + * @arg {Array} [content.stickerIDS] An array of ids corresponding to the stickers to send * @arg {Boolean} [content.tts] Set the message TTS flag * @arg {Object} [file] A file object * @arg {Buffer} file.file A buffer containing file data diff --git a/lib/structures/TextChannel.js b/lib/structures/TextChannel.js index 9ff0373bd..03be56624 100644 --- a/lib/structures/TextChannel.js +++ b/lib/structures/TextChannel.js @@ -76,6 +76,7 @@ class TextChannel extends GuildChannel { * @arg {String} [content.messageReference.guildID] The guild ID of the referenced message * @arg {String} content.messageReference.messageID The message ID of the referenced message. This cannot reference a system message * @arg {String} [content.messageReferenceID] [DEPRECATED] The ID of the message should be replied to. Use `messageReference` instead + * @arg {Array} [content.stickerIDS] An array of ids corresponding to the stickers to send * @arg {Boolean} [content.tts] Set the message TTS flag * @arg {Object} [file] A file object * @arg {Buffer} file.file A buffer containing file data From ffd3211934963a16662b11dbebd72b5d008f47da Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Wed, 28 Jul 2021 05:31:03 -0500 Subject: [PATCH 02/11] allow only stickers to be sent --- lib/Client.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Client.js b/lib/Client.js index 2de00e89f..b1e833a59 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -603,8 +603,8 @@ class Client extends EventEmitter { }; } else if(content.content !== undefined && typeof content.content !== "string") { content.content = "" + content.content; - } else if(content.content === undefined && !content.embed && !content.embeds && !file) { - return Promise.reject(new Error("No content, file, or embeds")); + } else if(content.content === undefined && !content.embed && !content.embeds && !file && (content.stickerIDS === undefined || content.stickerIDS.length === 0)) { + return Promise.reject(new Error("No content, file, stickers, or embeds")); } else if(content.embed && !content.embeds) { this.emit("warn", "[DEPRECATED] content.embed is deprecated. Use content.embeds instead"); content.embeds = [content.embed]; From fe56a39cfd59b4abf577936532ea8eaf506344fb Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Wed, 28 Jul 2021 20:52:56 -0500 Subject: [PATCH 03/11] stickerIDS -> stickerIDs --- index.d.ts | 2 +- lib/Client.js | 6 +++--- lib/structures/PrivateChannel.js | 2 +- lib/structures/TextChannel.js | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 95110b019..3c6e74224 100644 --- a/index.d.ts +++ b/index.d.ts @@ -62,7 +62,7 @@ declare namespace Eris { messageReference?: MessageReferenceReply; /** @deprecated */ messageReferenceID?: string; - stickerIDS?: string[]; + stickerIDs?: string[]; tts?: boolean; }; type ImageFormat = "jpg" | "jpeg" | "png" | "gif" | "webp"; diff --git a/lib/Client.js b/lib/Client.js index b1e833a59..4e1df92b7 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -588,7 +588,7 @@ class Client extends EventEmitter { * @arg {String} [content.messageReference.guildID] The guild ID of the referenced message * @arg {String} content.messageReference.messageID The message ID of the referenced message. This cannot reference a system message * @arg {String} [content.messageReferenceID] [DEPRECATED] The ID of the message should be replied to. Use `messageReference` instead - * @arg {Array} [content.stickerIDS] An array of ids corresponding to stickers to send + * @arg {Array} [content.stickerIDs] An array of ids corresponding to stickers to send * @arg {Boolean} [content.tts] Set the message TTS flag * @arg {Object | Array} [file] A file object (or an Array of them) * @arg {Buffer} file.file A buffer containing file data @@ -603,14 +603,14 @@ class Client extends EventEmitter { }; } else if(content.content !== undefined && typeof content.content !== "string") { content.content = "" + content.content; - } else if(content.content === undefined && !content.embed && !content.embeds && !file && (content.stickerIDS === undefined || content.stickerIDS.length === 0)) { + } else if(content.content === undefined && !content.embed && !content.embeds && !file && (content.stickerIDs === undefined || content.stickerIDs.length === 0)) { return Promise.reject(new Error("No content, file, stickers, or embeds")); } else if(content.embed && !content.embeds) { this.emit("warn", "[DEPRECATED] content.embed is deprecated. Use content.embeds instead"); content.embeds = [content.embed]; } content.allowed_mentions = this._formatAllowedMentions(content.allowedMentions); - content.sticker_ids = content.stickerIDS; + content.sticker_ids = content.stickerIDs; if(content.messageReference) { content.message_reference = content.messageReference; if(content.messageReference.messageID !== undefined) { diff --git a/lib/structures/PrivateChannel.js b/lib/structures/PrivateChannel.js index fb0615591..bd50d8206 100644 --- a/lib/structures/PrivateChannel.js +++ b/lib/structures/PrivateChannel.js @@ -55,7 +55,7 @@ class PrivateChannel extends Channel { * @arg {String} [content.messageReference.guildID] The guild ID of the referenced message * @arg {String} content.messageReference.messageID The message ID of the referenced message. This cannot reference a system message * @arg {String} [content.messageReferenceID] [DEPRECATED] The ID of the message should be replied to. Use `messageReference` instead - * @arg {Array} [content.stickerIDS] An array of ids corresponding to the stickers to send + * @arg {Array} [content.stickerIDs] An array of ids corresponding to the stickers to send * @arg {Boolean} [content.tts] Set the message TTS flag * @arg {Object} [file] A file object * @arg {Buffer} file.file A buffer containing file data diff --git a/lib/structures/TextChannel.js b/lib/structures/TextChannel.js index 03be56624..0056f4a00 100644 --- a/lib/structures/TextChannel.js +++ b/lib/structures/TextChannel.js @@ -76,7 +76,7 @@ class TextChannel extends GuildChannel { * @arg {String} [content.messageReference.guildID] The guild ID of the referenced message * @arg {String} content.messageReference.messageID The message ID of the referenced message. This cannot reference a system message * @arg {String} [content.messageReferenceID] [DEPRECATED] The ID of the message should be replied to. Use `messageReference` instead - * @arg {Array} [content.stickerIDS] An array of ids corresponding to the stickers to send + * @arg {Array} [content.stickerIDs] An array of ids corresponding to the stickers to send * @arg {Boolean} [content.tts] Set the message TTS flag * @arg {Object} [file] A file object * @arg {Buffer} file.file A buffer containing file data From c64a32b1497418cab3bff5bbe71b9de129a8e0ce Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Wed, 28 Jul 2021 20:53:09 -0500 Subject: [PATCH 04/11] random newline --- index.d.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 3c6e74224..bce2fd99d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2304,7 +2304,6 @@ declare namespace Eris { stickerItems?: StickerItems[]; /** @deprecated */ stickers?: Sticker[]; - timestamp: number; tts: boolean; type: number; From 198e09350e6c23284ba5ac2d531809b49eb1a450 Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Fri, 6 Aug 2021 12:14:55 -0500 Subject: [PATCH 05/11] include embeds length check & use not instead of explicit undefined --- lib/Client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Client.js b/lib/Client.js index 4e1df92b7..627487cca 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -603,7 +603,7 @@ class Client extends EventEmitter { }; } else if(content.content !== undefined && typeof content.content !== "string") { content.content = "" + content.content; - } else if(content.content === undefined && !content.embed && !content.embeds && !file && (content.stickerIDs === undefined || content.stickerIDs.length === 0)) { + } else if(content.content === undefined && !content.embed && (!content.embeds || content.embeds.length === 0) && !file && (!content.stickerIDs || content.stickerIDs.length === 0)) { return Promise.reject(new Error("No content, file, stickers, or embeds")); } else if(content.embed && !content.embeds) { this.emit("warn", "[DEPRECATED] content.embed is deprecated. Use content.embeds instead"); From 951bac5c45980cbef6a555e96a586623d1789e4a Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Tue, 17 Aug 2021 18:19:24 -0500 Subject: [PATCH 06/11] add embeds check to editMessage --- lib/Client.js | 312 +++++++++++++++++++++++++------------------------- 1 file changed, 156 insertions(+), 156 deletions(-) diff --git a/lib/Client.js b/lib/Client.js index 627487cca..f548728bd 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -28,21 +28,21 @@ const VoiceConnectionManager = require("./voice/VoiceConnectionManager"); let EventEmitter; try { EventEmitter = require("eventemitter3"); -} catch(err) { +} catch (err) { EventEmitter = require("events"); } let Erlpack; try { Erlpack = require("erlpack"); -} catch(err) { // eslint-disable no-empty +} catch (err) { // eslint-disable no-empty } let ZlibSync; try { ZlibSync = require("zlib-sync"); -} catch(err) { +} catch (err) { try { ZlibSync = require("pako"); - } catch(err) { // eslint-disable no-empty + } catch (err) { // eslint-disable no-empty } } const sleep = (ms) => new Promise((res) => setTimeout(res, ms)); @@ -149,31 +149,31 @@ class Client extends EventEmitter { reconnectDelay: (lastDelay, attempts) => Math.pow(attempts + 1, 0.7) * 20000 }, options); this.options.allowedMentions = this._formatAllowedMentions(this.options.allowedMentions); - if(this.options.lastShardID === undefined && this.options.maxShards !== "auto") { + if (this.options.lastShardID === undefined && this.options.maxShards !== "auto") { this.options.lastShardID = this.options.maxShards - 1; } - if(typeof window !== "undefined" || !ZlibSync) { + if (typeof window !== "undefined" || !ZlibSync) { this.options.compress = false; // zlib does not like Blobs, Pako is not here } - if(!Constants.ImageFormats.includes(this.options.defaultImageFormat.toLowerCase())) { + if (!Constants.ImageFormats.includes(this.options.defaultImageFormat.toLowerCase())) { throw new TypeError(`Invalid default image format: ${this.options.defaultImageFormat}`); } const defaultImageSize = this.options.defaultImageSize; - if(defaultImageSize < Constants.ImageSizeBoundaries.MINIMUM || defaultImageSize > Constants.ImageSizeBoundaries.MAXIMUM || (defaultImageSize & (defaultImageSize - 1))) { + if (defaultImageSize < Constants.ImageSizeBoundaries.MINIMUM || defaultImageSize > Constants.ImageSizeBoundaries.MAXIMUM || (defaultImageSize & (defaultImageSize - 1))) { throw new TypeError(`Invalid default image size: ${defaultImageSize}`); } // Set HTTP Agent on Websockets if not already set - if(this.options.agent && !(this.options.ws && this.options.ws.agent)) { + if (this.options.agent && !(this.options.ws && this.options.ws.agent)) { this.options.ws = this.options.ws || {}; this.options.ws.agent = this.options.agent; } - if(this.options.hasOwnProperty("intents")) { + if (this.options.hasOwnProperty("intents")) { // Resolve intents option to the proper integer - if(Array.isArray(this.options.intents)) { + if (Array.isArray(this.options.intents)) { let bitmask = 0; - for(const intent of this.options.intents) { - if(Constants.Intents[intent]) { + for (const intent of this.options.intents) { + if (Constants.Intents[intent]) { bitmask |= Constants.Intents[intent]; } } @@ -181,7 +181,7 @@ class Client extends EventEmitter { } // Ensure requesting all guild members isn't destined to fail - if(this.options.getAllUsers && !(this.options.intents & Constants.Intents.guildMembers)) { + if (this.options.getAllUsers && !(this.options.intents & Constants.Intents.guildMembers)) { throw new Error("Cannot request all members without guildMembers intent"); } } @@ -257,7 +257,7 @@ class Client extends EventEmitter { * @returns {Promise} */ addGuildDiscoverySubcategory(guildID, categoryID, reason) { - return this.requestHandler.request("POST", Endpoints.GUILD_DISCOVERY_CATEGORY(guildID, categoryID), true, {reason}); + return this.requestHandler.request("POST", Endpoints.GUILD_DISCOVERY_CATEGORY(guildID, categoryID), true, { reason }); } /** @@ -283,10 +283,10 @@ class Client extends EventEmitter { * @returns {Promise} */ addMessageReaction(channelID, messageID, reaction, userID) { - if(userID !== undefined) { + if (userID !== undefined) { this.emit("warn", "[DEPRECATED] addMessageReaction() was called without an \"@me\" `userID` argument"); } - if(reaction === decodeURI(reaction)) { + if (reaction === decodeURI(reaction)) { reaction = encodeURIComponent(reaction); } return this.requestHandler.request("PUT", Endpoints.CHANNEL_MESSAGE_REACTION_USER(channelID, messageID, reaction, userID || "@me"), true); @@ -328,7 +328,7 @@ class Client extends EventEmitter { * @returns {Promise} */ banGuildMember(guildID, userID, deleteMessageDays, reason) { - if(!isNaN(deleteMessageDays) && (deleteMessageDays < 0 || deleteMessageDays > 7)) { + if (!isNaN(deleteMessageDays) && (deleteMessageDays < 0 || deleteMessageDays > 7)) { return Promise.reject(new Error(`Invalid deleteMessageDays value (${deleteMessageDays}), should be a number between 0-7 inclusive`)); } return this.requestHandler.request("PUT", Endpoints.GUILD_BAN(guildID, userID), true, { @@ -358,36 +358,36 @@ class Client extends EventEmitter { async connect() { try { const data = await (this.options.maxShards === "auto" ? this.getBotGateway() : this.getGateway()); - if(!data.url || (this.options.maxShards === "auto" && !data.shards)) { + if (!data.url || (this.options.maxShards === "auto" && !data.shards)) { throw new Error("Invalid response from gateway REST call"); } - if(data.url.includes("?")) { + if (data.url.includes("?")) { data.url = data.url.substring(0, data.url.indexOf("?")); } - if(!data.url.endsWith("/")) { + if (!data.url.endsWith("/")) { data.url += "/"; } this.gatewayURL = `${data.url}?v=${Constants.GATEWAY_VERSION}&encoding=${Erlpack ? "etf" : "json"}`; - if(this.options.compress) { + if (this.options.compress) { this.gatewayURL += "&compress=zlib-stream"; } - if(this.options.maxShards === "auto") { - if(!data.shards) { + if (this.options.maxShards === "auto") { + if (!data.shards) { throw new Error("Failed to autoshard due to lack of data from Discord."); } this.options.maxShards = data.shards; - if(this.options.lastShardID === undefined) { + if (this.options.lastShardID === undefined) { this.options.lastShardID = data.shards - 1; } } - for(let i = this.options.firstShardID; i <= this.options.lastShardID; ++i) { + for (let i = this.options.firstShardID; i <= this.options.lastShardID; ++i) { this.shards.spawn(i); } - } catch(err) { - if(!this.options.autoreconnect) { + } catch (err) { + if (!this.options.autoreconnect) { throw err; } const reconnectDelay = this.options.reconnectDelay(this.lastReconnectDelay, this.reconnectAttempts); @@ -415,17 +415,17 @@ class Client extends EventEmitter { * @returns {Promise} */ createChannel(guildID, name, type, reason, options = {}) { - if(typeof options === "string") { // This used to be parentID, back-compat + if (typeof options === "string") { // This used to be parentID, back-compat this.emit("warn", "[DEPRECATED] createChannel() was called with a string `options` argument"); options = { parentID: options }; } - if(typeof reason === "string") { // Reason is deprecated, will be folded into options + if (typeof reason === "string") { // Reason is deprecated, will be folded into options this.emit("warn", "[DEPRECATED] createChannel() was called with a string `reason` argument"); options.reason = reason; reason = undefined; - } else if(typeof reason === "object" && reason !== null) { + } else if (typeof reason === "object" && reason !== null) { options = reason; reason = undefined; } @@ -507,7 +507,7 @@ class Client extends EventEmitter { * @returns {Promise} */ createGuild(name, options) { - if(this.guilds.size > 9) { + if (this.guilds.size > 9) { throw new Error("This method can't be used when in 10 or more guilds."); } @@ -596,44 +596,44 @@ class Client extends EventEmitter { * @returns {Promise} */ createMessage(channelID, content, file) { - if(content !== undefined) { - if(typeof content !== "object" || content === null) { + if (content !== undefined) { + if (typeof content !== "object" || content === null) { content = { content: "" + content }; - } else if(content.content !== undefined && typeof content.content !== "string") { + } else if (content.content !== undefined && typeof content.content !== "string") { content.content = "" + content.content; - } else if(content.content === undefined && !content.embed && (!content.embeds || content.embeds.length === 0) && !file && (!content.stickerIDs || content.stickerIDs.length === 0)) { + } else if (content.content === undefined && !content.embed && (!content.embeds || content.embeds.length === 0) && !file && (!content.stickerIDs || content.stickerIDs.length === 0)) { return Promise.reject(new Error("No content, file, stickers, or embeds")); - } else if(content.embed && !content.embeds) { + } else if (content.embed && !content.embeds) { this.emit("warn", "[DEPRECATED] content.embed is deprecated. Use content.embeds instead"); content.embeds = [content.embed]; } content.allowed_mentions = this._formatAllowedMentions(content.allowedMentions); content.sticker_ids = content.stickerIDs; - if(content.messageReference) { + if (content.messageReference) { content.message_reference = content.messageReference; - if(content.messageReference.messageID !== undefined) { + if (content.messageReference.messageID !== undefined) { content.message_reference.message_id = content.messageReference.messageID; content.messageReference.messageID = undefined; } - if(content.messageReference.channelID !== undefined) { + if (content.messageReference.channelID !== undefined) { content.message_reference.channel_id = content.messageReference.channelID; content.messageReference.channelID = undefined; } - if(content.messageReference.guildID !== undefined) { + if (content.messageReference.guildID !== undefined) { content.message_reference.guild_id = content.messageReference.guildID; content.messageReference.guildID = undefined; } - if(content.messageReference.failIfNotExists !== undefined) { + if (content.messageReference.failIfNotExists !== undefined) { content.message_reference.fail_if_not_exists = content.messageReference.failIfNotExists; content.messageReference.failIfNotExists = undefined; } - } else if(content.messageReferenceID) { + } else if (content.messageReferenceID) { this.emit("warn", "[DEPRECATED] content.messageReferenceID is deprecated. Use content.messageReference instead"); - content.message_reference = {message_id: content.messageReferenceID}; + content.message_reference = { message_id: content.messageReferenceID }; } - } else if(!file) { + } else if (!file) { return Promise.reject(new Error("No content, file, or embeds")); } return this.requestHandler.request("POST", Endpoints.CHANNEL_MESSAGES(channelID), true, content, file).then((message) => new Message(message, this)); @@ -661,7 +661,7 @@ class Client extends EventEmitter { reason: reason }).then((role) => { const guild = this.guilds.get(guildID); - if(guild) { + if (guild) { return guild.roles.add(role, guild); } else { return new Role(role); @@ -721,7 +721,7 @@ class Client extends EventEmitter { * @returns {Promise} */ deleteGuildDiscoverySubcategory(guildID, categoryID, reason) { - return this.requestHandler.request("DELETE", Endpoints.GUILD_DISCOVERY_CATEGORY(guildID, categoryID), true, {reason}); + return this.requestHandler.request("DELETE", Endpoints.GUILD_DISCOVERY_CATEGORY(guildID, categoryID), true, { reason }); } /** @@ -790,20 +790,20 @@ class Client extends EventEmitter { * @returns {Promise} */ deleteMessages(channelID, messageIDs, reason) { - if(messageIDs.length === 0) { + if (messageIDs.length === 0) { return Promise.resolve(); } - if(messageIDs.length === 1) { + if (messageIDs.length === 1) { return this.deleteMessage(channelID, messageIDs[0]); } const oldestAllowedSnowflake = (Date.now() - 1421280000000) * 4194304; const invalidMessage = messageIDs.find((messageID) => messageID < oldestAllowedSnowflake); - if(invalidMessage) { + if (invalidMessage) { return Promise.reject(new Error(`Message ${invalidMessage} is more than 2 weeks old.`)); } - if(messageIDs.length > 100) { + if (messageIDs.length > 100) { return this.requestHandler.request("POST", Endpoints.CHANNEL_BULK_DELETE(channelID), true, { messages: messageIDs.splice(0, 100), reason: reason @@ -887,7 +887,7 @@ class Client extends EventEmitter { return this.requestHandler.request("POST", Endpoints.USER_MFA_TOTP_DISABLE("@me"), true, { code }).then((data) => { - if(data.token) { + if (data.token) { this._token = data.token; } }); @@ -964,7 +964,7 @@ class Client extends EventEmitter { * @returns {Promise} */ editChannelPermission(channelID, overwriteID, allow, deny, type, reason) { - if(typeof type === "string") { // backward compatibility + if (typeof type === "string") { // backward compatibility type = type === "member" ? 1 : 0; } return this.requestHandler.request("PUT", Endpoints.CHANNEL_PERMISSION(channelID, overwriteID), true, { @@ -987,10 +987,10 @@ class Client extends EventEmitter { editChannelPosition(channelID, position, options = {}) { let channels = this.guilds.get(this.channelGuildMap[channelID]).channels; const channel = channels.get(channelID); - if(!channel) { + if (!channel) { return Promise.reject(new Error(`Channel ${channelID} not found`)); } - if(channel.position === position) { + if (channel.position === position) { return Promise.resolve(); } const min = Math.min(position, channel.position); @@ -1001,7 +1001,7 @@ class Client extends EventEmitter { && chan.position <= max && chan.id !== channelID; }).sort((a, b) => a.position - b.position); - if(position > channel.position) { + if (position > channel.position) { channels.push(channel); } else { channels.unshift(channel); @@ -1237,20 +1237,20 @@ class Client extends EventEmitter { * @returns {Promise} */ editMessage(channelID, messageID, content) { - if(content !== undefined) { - if(typeof content !== "object" || content === null) { + if (content !== undefined) { + if (typeof content !== "object" || content === null) { content = { content: "" + content }; - } else if(content.content !== undefined && typeof content.content !== "string") { + } else if (content.content !== undefined && typeof content.content !== "string") { content.content = "" + content.content; - } else if(content.content === undefined && !content.embed && !content.embeds && content.flags === undefined) { + } else if (content.content === undefined && !content.embed && (!content.embeds || content.embeds.length === 0) && content.flags === undefined) { return Promise.reject(new Error("No content, embeds or flags")); - } else if(content.embed && !content.embeds) { + } else if (content.embed && !content.embeds) { this.emit("warn", "[DEPRECATED] content.embed is deprecated. Use content.embeds instead"); content.embeds = [content.embed]; } - if(content.content !== undefined || content.embeds || content.allowedMentions) { + if (content.content !== undefined || content.embeds || content.allowedMentions) { content.allowed_mentions = this._formatAllowedMentions(content.allowedMentions); } } @@ -1298,21 +1298,21 @@ class Client extends EventEmitter { * @returns {Promise} */ editRolePosition(guildID, roleID, position) { - if(guildID === roleID) { + if (guildID === roleID) { return Promise.reject(new Error("Cannot move default role")); } let roles = this.guilds.get(guildID).roles; const role = roles.get(roleID); - if(!role) { + if (!role) { return Promise.reject(new Error(`Role ${roleID} not found`)); } - if(role.position === position) { + if (role.position === position) { return Promise.resolve(); } const min = Math.min(position, role.position); const max = Math.max(position, role.position); roles = roles.filter((role) => min <= role.position && role.position <= max && role.id !== roleID).sort((a, b) => a.position - b.position); - if(position > role.position) { + if (position > role.position) { roles.push(role); } else { roles.unshift(role); @@ -1376,15 +1376,15 @@ class Client extends EventEmitter { */ editSelfSettings(data) { let friendSourceFlags = undefined; - if(data.friendSourceFlags) { + if (data.friendSourceFlags) { friendSourceFlags = {}; - if(data.friendSourceFlags.all) { + if (data.friendSourceFlags.all) { friendSourceFlags.all = true; } - if(data.friendSourceFlags.mutualFriends) { + if (data.friendSourceFlags.mutualFriends) { friendSourceFlags.mutual_friends = true; } - if(data.friendSourceFlags.mutualGuilds) { + if (data.friendSourceFlags.mutualGuilds) { friendSourceFlags.mutual_guilds = true; } } @@ -1418,19 +1418,19 @@ class Client extends EventEmitter { * @arg {String} [game.url] Sets the url of the shard's active game */ editStatus(status, activities) { - if(activities === undefined && typeof status === "object") { + if (activities === undefined && typeof status === "object") { activities = status; status = undefined; } - if(status) { + if (status) { this.presence.status = status; } - if(activities === null) { + if (activities === null) { activities = []; - } else if(activities && !Array.isArray(activities)) { + } else if (activities && !Array.isArray(activities)) { activities = [activities]; } - if(activities !== undefined) { + if (activities !== undefined) { this.presence.activities = activities; } @@ -1493,10 +1493,10 @@ class Client extends EventEmitter { * @returns {Promise} */ editWebhookMessage(webhookID, token, messageID, options) { - if(!options.content && !options.embeds && !options.file) { + if (!options.content && !options.embeds && !options.file) { return Promise.reject(new Error("No content, embeds or file")); } - if(options.allowedMentions) { + if (options.allowedMentions) { options.allowed_mentions = this._formatAllowedMentions(options.allowedMentions); } return this.requestHandler.request("PATCH", Endpoints.WEBHOOK_MESSAGE(webhookID, token, messageID), false, options, options.file).then((response) => new Message(response, this)); @@ -1513,7 +1513,7 @@ class Client extends EventEmitter { secret, code }).then((data) => { - if(data.token) { + if (data.token) { this._token = data.token; } }); @@ -1558,7 +1558,7 @@ class Client extends EventEmitter { * @returns {Promise} */ executeWebhook(webhookID, token, options) { - if(!options.content && !options.file && !options.embeds) { + if (!options.content && !options.file && !options.embeds) { return Promise.reject(new Error("No content, file, or embeds")); } return this.requestHandler.request("POST", Endpoints.WEBHOOK_TOKEN(webhookID, token) + (options.wait ? "?wait=true" : ""), !!options.auth, { @@ -1578,7 +1578,7 @@ class Client extends EventEmitter { * @returns {Object} An object containing the NewsChannel's ID and the new webhook's ID */ followChannel(channelID, webhookChannelID) { - return this.requestHandler.request("POST", Endpoints.CHANNEL_FOLLOW(channelID), true, {webhook_channel_id: webhookChannelID}); + return this.requestHandler.request("POST", Endpoints.CHANNEL_FOLLOW(channelID), true, { webhook_channel_id: webhookChannelID }); } /** @@ -1586,7 +1586,7 @@ class Client extends EventEmitter { * @returns {Promise} Resolves with an object containing gateway connection info */ getBotGateway() { - if(!this._token.startsWith("Bot ")) { + if (!this._token.startsWith("Bot ")) { this._token = "Bot " + this._token; } return this.requestHandler.request("GET", Endpoints.GATEWAY_BOT, true); @@ -1598,11 +1598,11 @@ class Client extends EventEmitter { * @returns {CategoryChannel | GroupChannel | PrivateChannel | TextChannel | VoiceChannel | NewsChannel} */ getChannel(channelID) { - if(!channelID) { + if (!channelID) { throw new Error(`Invalid channel ID: ${channelID}`); } - if(this.channelGuildMap[channelID] && this.guilds.get(this.channelGuildMap[channelID])) { + if (this.channelGuildMap[channelID] && this.guilds.get(this.channelGuildMap[channelID])) { return this.guilds.get(this.channelGuildMap[channelID]).channels.get(channelID); } return this.privateChannels.get(channelID) || this.groupChannels.get(channelID); @@ -1640,7 +1640,7 @@ class Client extends EventEmitter { * @returns {Promise} */ getDMChannel(userID) { - if(this.privateChannelMap[userID]) { + if (this.privateChannelMap[userID]) { return Promise.resolve(this.privateChannels.get(this.privateChannelMap[userID])); } return this.requestHandler.request("POST", Endpoints.USER_CHANNELS("@me"), true, { @@ -1677,27 +1677,27 @@ class Client extends EventEmitter { * @returns {Promise<{users: User[], entries: GuildAuditLogEntry[], integrations: PartialIntegration[], webhooks: Webhook[]}>} */ getGuildAuditLog(guildID, options = {}, before, actionType, userID) { - if(!options || typeof options !== "object") { + if (!options || typeof options !== "object") { options = { limit: options }; } - if(options.limit === undefined) { // Legacy behavior + if (options.limit === undefined) { // Legacy behavior options.limit = 50; } - if(actionType !== undefined) { + if (actionType !== undefined) { options.actionType = actionType; } - if(before !== undefined) { + if (before !== undefined) { options.before = before; } - if(userID !== undefined) { + if (userID !== undefined) { options.userID = userID; } - if(options.actionType !== undefined) { + if (options.actionType !== undefined) { options.action_type = options.actionType; } - if(options.userID !== undefined) { + if (options.userID !== undefined) { options.user_id = options.userID; } return this.requestHandler.request("GET", Endpoints.GUILD_AUDIT_LOGS(guildID), true, options).then((data) => { @@ -1886,24 +1886,24 @@ class Client extends EventEmitter { * @returns {Promise>} */ getMessageReaction(channelID, messageID, reaction, options = {}, before, after) { - if(reaction === decodeURI(reaction)) { + if (reaction === decodeURI(reaction)) { reaction = encodeURIComponent(reaction); } - if(!options || typeof options !== "object") { + if (!options || typeof options !== "object") { options = { limit: options }; } - if(options.limit === undefined) { // Legacy behavior + if (options.limit === undefined) { // Legacy behavior options.limit = 100; } - if(before !== undefined) { + if (before !== undefined) { options.before = before; } - if(after !== undefined) { + if (after !== undefined) { options.after = after; } - if(options.before) { + if (options.before) { this.emit("warn", "[DEPRECATED] getMessageReaction() was called with a `before` parameter. Discord no longer supports this parameter"); } return this.requestHandler.request("GET", Endpoints.CHANNEL_MESSAGE_REACTION(channelID, messageID, reaction), true, options).then((users) => users.map((user) => new User(user, this))); @@ -1923,25 +1923,25 @@ class Client extends EventEmitter { * @returns {Promise>} */ async getMessages(channelID, options = {}, before, after, around) { - if(!options || typeof options !== "object") { + if (!options || typeof options !== "object") { options = { limit: options }; } - if(options.limit === undefined) { // Legacy behavior + if (options.limit === undefined) { // Legacy behavior options.limit = 50; } - if(after !== undefined) { + if (after !== undefined) { options.after = after; } - if(around !== undefined) { + if (around !== undefined) { options.around = around; } - if(before !== undefined) { + if (before !== undefined) { options.before = before; } let limit = options.limit; - if(limit && limit > 100) { + if (limit && limit > 100) { let logs = []; const get = async (_before, _after) => { const messages = await this.requestHandler.request("GET", Endpoints.CHANNEL_MESSAGES(channelID), true, { @@ -1949,12 +1949,12 @@ class Client extends EventEmitter { before: _before || undefined, after: _after || undefined }); - if(limit <= messages.length) { + if (limit <= messages.length) { return (_after ? messages.slice(messages.length - limit, messages.length).map((message) => new Message(message, this)).concat(logs) : logs.concat(messages.slice(0, limit).map((message) => new Message(message, this)))); } limit -= messages.length; logs = (_after ? messages.map((message) => new Message(message, this)).concat(logs) : logs.concat(messages.map((message) => new Message(message, this)))); - if(messages.length < 100) { + if (messages.length < 100) { return logs; } this.emit("debug", `Getting ${limit} more messages during getMessages for ${channelID}: ${_before} ${_after}`, -1); @@ -1966,7 +1966,7 @@ class Client extends EventEmitter { return messages.map((message) => { try { return new Message(message, this); - } catch(err) { + } catch (err) { this.emit("error", `Error creating message from channel messages\n${err.stack}\n${JSON.stringify(messages)}`); return null; } @@ -2012,7 +2012,7 @@ class Client extends EventEmitter { * @returns {Promise} */ getRESTChannel(channelID) { - if(!this.options.restMode) { + if (!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.CHANNEL(channelID), true) @@ -2026,7 +2026,7 @@ class Client extends EventEmitter { * @returns {Promise} */ getRESTGuild(guildID, withCounts = false) { - if(!this.options.restMode) { + if (!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.GUILD(guildID), true, { @@ -2040,7 +2040,7 @@ class Client extends EventEmitter { * @returns {Promise} */ getRESTGuildChannels(guildID) { - if(!this.options.restMode) { + if (!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.GUILD_CHANNELS(guildID), true) @@ -2054,7 +2054,7 @@ class Client extends EventEmitter { * @returns {Promise} An emoji object */ getRESTGuildEmoji(guildID, emojiID) { - if(!this.options.restMode) { + if (!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.GUILD_EMOJI(guildID, emojiID), true); @@ -2066,7 +2066,7 @@ class Client extends EventEmitter { * @returns {Promise>} An array of guild emoji objects */ getRESTGuildEmojis(guildID) { - if(!this.options.restMode) { + if (!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.GUILD_EMOJIS(guildID), true); @@ -2079,7 +2079,7 @@ class Client extends EventEmitter { * @returns {Promise} */ getRESTGuildMember(guildID, memberID) { - if(!this.options.restMode) { + if (!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.GUILD_MEMBER(guildID, memberID), true).then((member) => new Member(member, this.guilds.get(guildID), this)); @@ -2095,15 +2095,15 @@ class Client extends EventEmitter { * @returns {Promise>} */ getRESTGuildMembers(guildID, options = {}, after) { - if(!this.options.restMode) { + if (!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } - if(!options || typeof options !== "object") { + if (!options || typeof options !== "object") { options = { limit: options }; } - if(after !== undefined) { + if (after !== undefined) { options.after = after; } return this.requestHandler.request("GET", Endpoints.GUILD_MEMBERS(guildID), true, options).then((members) => members.map((member) => new Member(member, this.guilds.get(guildID), this))); @@ -2115,7 +2115,7 @@ class Client extends EventEmitter { * @returns {Promise>} */ getRESTGuildRoles(guildID) { - if(!this.options.restMode) { + if (!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.GUILD_ROLES(guildID), true).then((roles) => roles.map((role) => new Role(role, null))); @@ -2133,18 +2133,18 @@ class Client extends EventEmitter { */ getRESTGuilds(options = {}, before, after) { // TODO type - if(!this.options.restMode) { + if (!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } - if(!options || typeof options !== "object") { + if (!options || typeof options !== "object") { options = { limit: options }; } - if(after !== undefined) { + if (after !== undefined) { options.after = after; } - if(before !== undefined) { + if (before !== undefined) { options.before = before; } return this.requestHandler.request("GET", Endpoints.USER_GUILDS("@me"), true, options).then((guilds) => guilds.map((guild) => new Guild(guild, this))); @@ -2156,7 +2156,7 @@ class Client extends EventEmitter { * @returns {Promise} */ getRESTUser(userID) { - if(!this.options.restMode) { + if (!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.USER(userID), true).then((user) => new User(user, this)); @@ -2264,10 +2264,10 @@ class Client extends EventEmitter { */ joinVoiceChannel(channelID, options = {}) { const channel = this.getChannel(channelID); - if(!channel) { + if (!channel) { return Promise.reject(new Error("Channel not found")); } - if(channel.guild && !(channel.permissionsOf(this.user.id).allow & Constants.Permissions.voiceConnect)) { + if (channel.guild && !(channel.permissionsOf(this.user.id).allow & Constants.Permissions.voiceConnect)) { return Promise.reject(new Error("Insufficient permission to connect to voice channel")); } this.shards.get(this.guildShardMap[this.channelGuildMap[channelID]] || 0).sendWS(Constants.GatewayOPCodes.VOICE_STATE_UPDATE, { @@ -2276,7 +2276,7 @@ class Client extends EventEmitter { self_mute: false, self_deaf: false }); - if(options.opusOnly === undefined) { + if (options.opusOnly === undefined) { options.opusOnly = this.options.opusOnly; } return this.voiceConnections.join(this.channelGuildMap[channelID] || "call", channelID, options); @@ -2309,7 +2309,7 @@ class Client extends EventEmitter { * @arg {String} channelID The ID of the voice channel */ leaveVoiceChannel(channelID) { - if(!channelID || !this.channelGuildMap[channelID]) { + if (!channelID || !this.channelGuildMap[channelID]) { return; } this.closeVoiceConnection(this.channelGuildMap[channelID]); @@ -2360,32 +2360,32 @@ class Client extends EventEmitter { * @returns {Promise} Resolves with the number of messages deleted */ async purgeChannel(channelID, options, filter, before, after, reason) { - if(!options || typeof options !== "object") { + if (!options || typeof options !== "object") { options = { limit: options }; } - if(after !== undefined) { + if (after !== undefined) { options.after = after; } - if(before !== undefined) { + if (before !== undefined) { options.before = before; } - if(filter !== undefined) { + if (filter !== undefined) { options.filter = filter; } - if(reason !== undefined) { + if (reason !== undefined) { options.reason = reason; } - if(typeof options.filter === "string") { + if (typeof options.filter === "string") { const filter = options.filter; options.filter = (msg) => msg.content.includes(filter); } let limit = options.limit; - if(typeof limit !== "number") { + if (typeof limit !== "number") { throw new TypeError(`Invalid limit: ${limit}`); } - if(limit !== -1 && limit <= 0) { + if (limit !== -1 && limit <= 0) { return 0; } const toDelete = []; @@ -2393,15 +2393,15 @@ class Client extends EventEmitter { let done = false; const checkToDelete = async () => { const messageIDs = (done && toDelete) || (toDelete.length >= 100 && toDelete.splice(0, 100)); - if(messageIDs) { + if (messageIDs) { deleted += messageIDs.length; await this.deleteMessages(channelID, messageIDs, options.reason); - if(done) { + if (done) { return deleted; } await sleep(1000); return checkToDelete(); - } else if(done) { + } else if (done) { return deleted; } else { await sleep(250); @@ -2414,26 +2414,26 @@ class Client extends EventEmitter { before: _before, after: _after }); - if(limit !== -1 && limit <= 0) { + if (limit !== -1 && limit <= 0) { done = true; return; } - for(const message of messages) { - if(limit !== -1 && limit <= 0) { + for (const message of messages) { + if (limit !== -1 && limit <= 0) { break; } - if(message.timestamp < Date.now() - 1209600000) { // 14d * 24h * 60m * 60s * 1000ms + if (message.timestamp < Date.now() - 1209600000) { // 14d * 24h * 60m * 60s * 1000ms done = true; return; } - if(!options.filter || options.filter(message)) { + if (!options.filter || options.filter(message)) { toDelete.push(message.id); } - if(limit !== -1) { + if (limit !== -1) { limit--; } } - if((limit !== -1 && limit <= 0) || messages.length < 100) { + if ((limit !== -1 && limit <= 0) || messages.length < 100) { done = true; return; } @@ -2476,7 +2476,7 @@ class Client extends EventEmitter { * @returns {Promise} */ removeMessageReaction(channelID, messageID, reaction, userID) { - if(reaction === decodeURI(reaction)) { + if (reaction === decodeURI(reaction)) { reaction = encodeURIComponent(reaction); } return this.requestHandler.request("DELETE", Endpoints.CHANNEL_MESSAGE_REACTION_USER(channelID, messageID, reaction, userID || "@me"), true); @@ -2490,7 +2490,7 @@ class Client extends EventEmitter { * @returns {Promise} */ removeMessageReactionEmoji(channelID, messageID, reaction) { - if(reaction === decodeURI(reaction)) { + if (reaction === decodeURI(reaction)) { reaction = encodeURIComponent(reaction); } return this.requestHandler.request("DELETE", Endpoints.CHANNEL_MESSAGE_REACTION(channelID, messageID, reaction), true); @@ -2704,42 +2704,42 @@ class Client extends EventEmitter { } _formatAllowedMentions(allowed) { - if(!allowed) { + if (!allowed) { return this.options.allowedMentions; } const result = { parse: [] }; - if(allowed.everyone) { + if (allowed.everyone) { result.parse.push("everyone"); } - if(allowed.roles === true) { + if (allowed.roles === true) { result.parse.push("roles"); - } else if(Array.isArray(allowed.roles)) { - if(allowed.roles.length > 100) { + } else if (Array.isArray(allowed.roles)) { + if (allowed.roles.length > 100) { throw new Error("Allowed role mentions cannot exceed 100."); } result.roles = allowed.roles; } - if(allowed.users === true) { + if (allowed.users === true) { result.parse.push("users"); - } else if(Array.isArray(allowed.users)) { - if(allowed.users.length > 100) { + } else if (Array.isArray(allowed.users)) { + if (allowed.users.length > 100) { throw new Error("Allowed user mentions cannot exceed 100."); } result.users = allowed.users; } - if(allowed.repliedUser !== undefined) { + if (allowed.repliedUser !== undefined) { result.replied_user = allowed.repliedUser; } return result; } _formatImage(url, format, size) { - if(!format || !Constants.ImageFormats.includes(format.toLowerCase())) { + if (!format || !Constants.ImageFormats.includes(format.toLowerCase())) { format = url.includes("/a_") ? "gif" : this.options.defaultImageFormat; } - if(!size || size < Constants.ImageSizeBoundaries.MINIMUM || size > Constants.ImageSizeBoundaries.MAXIMUM || (size & (size - 1))) { + if (!size || size < Constants.ImageSizeBoundaries.MINIMUM || size > Constants.ImageSizeBoundaries.MAXIMUM || (size & (size - 1))) { size = this.options.defaultImageSize; } return `${Endpoints.CDN_URL}${url}.${format}?size=${size}`; From f23bd454a234915e2ebeddc8cc236cd6773d4a12 Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Tue, 17 Aug 2021 18:25:42 -0500 Subject: [PATCH 07/11] lint because vscode sucks --- lib/Client.js | 312 +++++++++++++++++++++++++------------------------- 1 file changed, 156 insertions(+), 156 deletions(-) diff --git a/lib/Client.js b/lib/Client.js index f548728bd..c24647b88 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -28,21 +28,21 @@ const VoiceConnectionManager = require("./voice/VoiceConnectionManager"); let EventEmitter; try { EventEmitter = require("eventemitter3"); -} catch (err) { +} catch(err) { EventEmitter = require("events"); } let Erlpack; try { Erlpack = require("erlpack"); -} catch (err) { // eslint-disable no-empty +} catch(err) { // eslint-disable no-empty } let ZlibSync; try { ZlibSync = require("zlib-sync"); -} catch (err) { +} catch(err) { try { ZlibSync = require("pako"); - } catch (err) { // eslint-disable no-empty + } catch(err) { // eslint-disable no-empty } } const sleep = (ms) => new Promise((res) => setTimeout(res, ms)); @@ -149,31 +149,31 @@ class Client extends EventEmitter { reconnectDelay: (lastDelay, attempts) => Math.pow(attempts + 1, 0.7) * 20000 }, options); this.options.allowedMentions = this._formatAllowedMentions(this.options.allowedMentions); - if (this.options.lastShardID === undefined && this.options.maxShards !== "auto") { + if(this.options.lastShardID === undefined && this.options.maxShards !== "auto") { this.options.lastShardID = this.options.maxShards - 1; } - if (typeof window !== "undefined" || !ZlibSync) { + if(typeof window !== "undefined" || !ZlibSync) { this.options.compress = false; // zlib does not like Blobs, Pako is not here } - if (!Constants.ImageFormats.includes(this.options.defaultImageFormat.toLowerCase())) { + if(!Constants.ImageFormats.includes(this.options.defaultImageFormat.toLowerCase())) { throw new TypeError(`Invalid default image format: ${this.options.defaultImageFormat}`); } const defaultImageSize = this.options.defaultImageSize; - if (defaultImageSize < Constants.ImageSizeBoundaries.MINIMUM || defaultImageSize > Constants.ImageSizeBoundaries.MAXIMUM || (defaultImageSize & (defaultImageSize - 1))) { + if(defaultImageSize < Constants.ImageSizeBoundaries.MINIMUM || defaultImageSize > Constants.ImageSizeBoundaries.MAXIMUM || (defaultImageSize & (defaultImageSize - 1))) { throw new TypeError(`Invalid default image size: ${defaultImageSize}`); } // Set HTTP Agent on Websockets if not already set - if (this.options.agent && !(this.options.ws && this.options.ws.agent)) { + if(this.options.agent && !(this.options.ws && this.options.ws.agent)) { this.options.ws = this.options.ws || {}; this.options.ws.agent = this.options.agent; } - if (this.options.hasOwnProperty("intents")) { + if(this.options.hasOwnProperty("intents")) { // Resolve intents option to the proper integer - if (Array.isArray(this.options.intents)) { + if(Array.isArray(this.options.intents)) { let bitmask = 0; - for (const intent of this.options.intents) { - if (Constants.Intents[intent]) { + for(const intent of this.options.intents) { + if(Constants.Intents[intent]) { bitmask |= Constants.Intents[intent]; } } @@ -181,7 +181,7 @@ class Client extends EventEmitter { } // Ensure requesting all guild members isn't destined to fail - if (this.options.getAllUsers && !(this.options.intents & Constants.Intents.guildMembers)) { + if(this.options.getAllUsers && !(this.options.intents & Constants.Intents.guildMembers)) { throw new Error("Cannot request all members without guildMembers intent"); } } @@ -257,7 +257,7 @@ class Client extends EventEmitter { * @returns {Promise} */ addGuildDiscoverySubcategory(guildID, categoryID, reason) { - return this.requestHandler.request("POST", Endpoints.GUILD_DISCOVERY_CATEGORY(guildID, categoryID), true, { reason }); + return this.requestHandler.request("POST", Endpoints.GUILD_DISCOVERY_CATEGORY(guildID, categoryID), true, {reason}); } /** @@ -283,10 +283,10 @@ class Client extends EventEmitter { * @returns {Promise} */ addMessageReaction(channelID, messageID, reaction, userID) { - if (userID !== undefined) { + if(userID !== undefined) { this.emit("warn", "[DEPRECATED] addMessageReaction() was called without an \"@me\" `userID` argument"); } - if (reaction === decodeURI(reaction)) { + if(reaction === decodeURI(reaction)) { reaction = encodeURIComponent(reaction); } return this.requestHandler.request("PUT", Endpoints.CHANNEL_MESSAGE_REACTION_USER(channelID, messageID, reaction, userID || "@me"), true); @@ -328,7 +328,7 @@ class Client extends EventEmitter { * @returns {Promise} */ banGuildMember(guildID, userID, deleteMessageDays, reason) { - if (!isNaN(deleteMessageDays) && (deleteMessageDays < 0 || deleteMessageDays > 7)) { + if(!isNaN(deleteMessageDays) && (deleteMessageDays < 0 || deleteMessageDays > 7)) { return Promise.reject(new Error(`Invalid deleteMessageDays value (${deleteMessageDays}), should be a number between 0-7 inclusive`)); } return this.requestHandler.request("PUT", Endpoints.GUILD_BAN(guildID, userID), true, { @@ -358,36 +358,36 @@ class Client extends EventEmitter { async connect() { try { const data = await (this.options.maxShards === "auto" ? this.getBotGateway() : this.getGateway()); - if (!data.url || (this.options.maxShards === "auto" && !data.shards)) { + if(!data.url || (this.options.maxShards === "auto" && !data.shards)) { throw new Error("Invalid response from gateway REST call"); } - if (data.url.includes("?")) { + if(data.url.includes("?")) { data.url = data.url.substring(0, data.url.indexOf("?")); } - if (!data.url.endsWith("/")) { + if(!data.url.endsWith("/")) { data.url += "/"; } this.gatewayURL = `${data.url}?v=${Constants.GATEWAY_VERSION}&encoding=${Erlpack ? "etf" : "json"}`; - if (this.options.compress) { + if(this.options.compress) { this.gatewayURL += "&compress=zlib-stream"; } - if (this.options.maxShards === "auto") { - if (!data.shards) { + if(this.options.maxShards === "auto") { + if(!data.shards) { throw new Error("Failed to autoshard due to lack of data from Discord."); } this.options.maxShards = data.shards; - if (this.options.lastShardID === undefined) { + if(this.options.lastShardID === undefined) { this.options.lastShardID = data.shards - 1; } } - for (let i = this.options.firstShardID; i <= this.options.lastShardID; ++i) { + for(let i = this.options.firstShardID; i <= this.options.lastShardID; ++i) { this.shards.spawn(i); } - } catch (err) { - if (!this.options.autoreconnect) { + } catch(err) { + if(!this.options.autoreconnect) { throw err; } const reconnectDelay = this.options.reconnectDelay(this.lastReconnectDelay, this.reconnectAttempts); @@ -415,17 +415,17 @@ class Client extends EventEmitter { * @returns {Promise} */ createChannel(guildID, name, type, reason, options = {}) { - if (typeof options === "string") { // This used to be parentID, back-compat + if(typeof options === "string") { // This used to be parentID, back-compat this.emit("warn", "[DEPRECATED] createChannel() was called with a string `options` argument"); options = { parentID: options }; } - if (typeof reason === "string") { // Reason is deprecated, will be folded into options + if(typeof reason === "string") { // Reason is deprecated, will be folded into options this.emit("warn", "[DEPRECATED] createChannel() was called with a string `reason` argument"); options.reason = reason; reason = undefined; - } else if (typeof reason === "object" && reason !== null) { + } else if(typeof reason === "object" && reason !== null) { options = reason; reason = undefined; } @@ -507,7 +507,7 @@ class Client extends EventEmitter { * @returns {Promise} */ createGuild(name, options) { - if (this.guilds.size > 9) { + if(this.guilds.size > 9) { throw new Error("This method can't be used when in 10 or more guilds."); } @@ -596,44 +596,44 @@ class Client extends EventEmitter { * @returns {Promise} */ createMessage(channelID, content, file) { - if (content !== undefined) { - if (typeof content !== "object" || content === null) { + if(content !== undefined) { + if(typeof content !== "object" || content === null) { content = { content: "" + content }; - } else if (content.content !== undefined && typeof content.content !== "string") { + } else if(content.content !== undefined && typeof content.content !== "string") { content.content = "" + content.content; - } else if (content.content === undefined && !content.embed && (!content.embeds || content.embeds.length === 0) && !file && (!content.stickerIDs || content.stickerIDs.length === 0)) { + } else if(content.content === undefined && !content.embed && (!content.embeds || content.embeds.length === 0) && !file && (!content.stickerIDs || content.stickerIDs.length === 0)) { return Promise.reject(new Error("No content, file, stickers, or embeds")); - } else if (content.embed && !content.embeds) { + } else if(content.embed && !content.embeds) { this.emit("warn", "[DEPRECATED] content.embed is deprecated. Use content.embeds instead"); content.embeds = [content.embed]; } content.allowed_mentions = this._formatAllowedMentions(content.allowedMentions); content.sticker_ids = content.stickerIDs; - if (content.messageReference) { + if(content.messageReference) { content.message_reference = content.messageReference; - if (content.messageReference.messageID !== undefined) { + if(content.messageReference.messageID !== undefined) { content.message_reference.message_id = content.messageReference.messageID; content.messageReference.messageID = undefined; } - if (content.messageReference.channelID !== undefined) { + if(content.messageReference.channelID !== undefined) { content.message_reference.channel_id = content.messageReference.channelID; content.messageReference.channelID = undefined; } - if (content.messageReference.guildID !== undefined) { + if(content.messageReference.guildID !== undefined) { content.message_reference.guild_id = content.messageReference.guildID; content.messageReference.guildID = undefined; } - if (content.messageReference.failIfNotExists !== undefined) { + if(content.messageReference.failIfNotExists !== undefined) { content.message_reference.fail_if_not_exists = content.messageReference.failIfNotExists; content.messageReference.failIfNotExists = undefined; } - } else if (content.messageReferenceID) { + } else if(content.messageReferenceID) { this.emit("warn", "[DEPRECATED] content.messageReferenceID is deprecated. Use content.messageReference instead"); - content.message_reference = { message_id: content.messageReferenceID }; + content.message_reference = {message_id: content.messageReferenceID}; } - } else if (!file) { + } else if(!file) { return Promise.reject(new Error("No content, file, or embeds")); } return this.requestHandler.request("POST", Endpoints.CHANNEL_MESSAGES(channelID), true, content, file).then((message) => new Message(message, this)); @@ -661,7 +661,7 @@ class Client extends EventEmitter { reason: reason }).then((role) => { const guild = this.guilds.get(guildID); - if (guild) { + if(guild) { return guild.roles.add(role, guild); } else { return new Role(role); @@ -721,7 +721,7 @@ class Client extends EventEmitter { * @returns {Promise} */ deleteGuildDiscoverySubcategory(guildID, categoryID, reason) { - return this.requestHandler.request("DELETE", Endpoints.GUILD_DISCOVERY_CATEGORY(guildID, categoryID), true, { reason }); + return this.requestHandler.request("DELETE", Endpoints.GUILD_DISCOVERY_CATEGORY(guildID, categoryID), true, {reason}); } /** @@ -790,20 +790,20 @@ class Client extends EventEmitter { * @returns {Promise} */ deleteMessages(channelID, messageIDs, reason) { - if (messageIDs.length === 0) { + if(messageIDs.length === 0) { return Promise.resolve(); } - if (messageIDs.length === 1) { + if(messageIDs.length === 1) { return this.deleteMessage(channelID, messageIDs[0]); } const oldestAllowedSnowflake = (Date.now() - 1421280000000) * 4194304; const invalidMessage = messageIDs.find((messageID) => messageID < oldestAllowedSnowflake); - if (invalidMessage) { + if(invalidMessage) { return Promise.reject(new Error(`Message ${invalidMessage} is more than 2 weeks old.`)); } - if (messageIDs.length > 100) { + if(messageIDs.length > 100) { return this.requestHandler.request("POST", Endpoints.CHANNEL_BULK_DELETE(channelID), true, { messages: messageIDs.splice(0, 100), reason: reason @@ -887,7 +887,7 @@ class Client extends EventEmitter { return this.requestHandler.request("POST", Endpoints.USER_MFA_TOTP_DISABLE("@me"), true, { code }).then((data) => { - if (data.token) { + if(data.token) { this._token = data.token; } }); @@ -964,7 +964,7 @@ class Client extends EventEmitter { * @returns {Promise} */ editChannelPermission(channelID, overwriteID, allow, deny, type, reason) { - if (typeof type === "string") { // backward compatibility + if(typeof type === "string") { // backward compatibility type = type === "member" ? 1 : 0; } return this.requestHandler.request("PUT", Endpoints.CHANNEL_PERMISSION(channelID, overwriteID), true, { @@ -987,10 +987,10 @@ class Client extends EventEmitter { editChannelPosition(channelID, position, options = {}) { let channels = this.guilds.get(this.channelGuildMap[channelID]).channels; const channel = channels.get(channelID); - if (!channel) { + if(!channel) { return Promise.reject(new Error(`Channel ${channelID} not found`)); } - if (channel.position === position) { + if(channel.position === position) { return Promise.resolve(); } const min = Math.min(position, channel.position); @@ -1001,7 +1001,7 @@ class Client extends EventEmitter { && chan.position <= max && chan.id !== channelID; }).sort((a, b) => a.position - b.position); - if (position > channel.position) { + if(position > channel.position) { channels.push(channel); } else { channels.unshift(channel); @@ -1237,20 +1237,20 @@ class Client extends EventEmitter { * @returns {Promise} */ editMessage(channelID, messageID, content) { - if (content !== undefined) { - if (typeof content !== "object" || content === null) { + if(content !== undefined) { + if(typeof content !== "object" || content === null) { content = { content: "" + content }; - } else if (content.content !== undefined && typeof content.content !== "string") { + } else if(content.content !== undefined && typeof content.content !== "string") { content.content = "" + content.content; - } else if (content.content === undefined && !content.embed && (!content.embeds || content.embeds.length === 0) && content.flags === undefined) { + } else if(content.content === undefined && !content.embed && (!content.embeds || content.embeds.length === 0) && content.flags === undefined) { return Promise.reject(new Error("No content, embeds or flags")); - } else if (content.embed && !content.embeds) { + } else if(content.embed && !content.embeds) { this.emit("warn", "[DEPRECATED] content.embed is deprecated. Use content.embeds instead"); content.embeds = [content.embed]; } - if (content.content !== undefined || content.embeds || content.allowedMentions) { + if(content.content !== undefined || content.embeds || content.allowedMentions) { content.allowed_mentions = this._formatAllowedMentions(content.allowedMentions); } } @@ -1298,21 +1298,21 @@ class Client extends EventEmitter { * @returns {Promise} */ editRolePosition(guildID, roleID, position) { - if (guildID === roleID) { + if(guildID === roleID) { return Promise.reject(new Error("Cannot move default role")); } let roles = this.guilds.get(guildID).roles; const role = roles.get(roleID); - if (!role) { + if(!role) { return Promise.reject(new Error(`Role ${roleID} not found`)); } - if (role.position === position) { + if(role.position === position) { return Promise.resolve(); } const min = Math.min(position, role.position); const max = Math.max(position, role.position); roles = roles.filter((role) => min <= role.position && role.position <= max && role.id !== roleID).sort((a, b) => a.position - b.position); - if (position > role.position) { + if(position > role.position) { roles.push(role); } else { roles.unshift(role); @@ -1376,15 +1376,15 @@ class Client extends EventEmitter { */ editSelfSettings(data) { let friendSourceFlags = undefined; - if (data.friendSourceFlags) { + if(data.friendSourceFlags) { friendSourceFlags = {}; - if (data.friendSourceFlags.all) { + if(data.friendSourceFlags.all) { friendSourceFlags.all = true; } - if (data.friendSourceFlags.mutualFriends) { + if(data.friendSourceFlags.mutualFriends) { friendSourceFlags.mutual_friends = true; } - if (data.friendSourceFlags.mutualGuilds) { + if(data.friendSourceFlags.mutualGuilds) { friendSourceFlags.mutual_guilds = true; } } @@ -1418,19 +1418,19 @@ class Client extends EventEmitter { * @arg {String} [game.url] Sets the url of the shard's active game */ editStatus(status, activities) { - if (activities === undefined && typeof status === "object") { + if(activities === undefined && typeof status === "object") { activities = status; status = undefined; } - if (status) { + if(status) { this.presence.status = status; } - if (activities === null) { + if(activities === null) { activities = []; - } else if (activities && !Array.isArray(activities)) { + } else if(activities && !Array.isArray(activities)) { activities = [activities]; } - if (activities !== undefined) { + if(activities !== undefined) { this.presence.activities = activities; } @@ -1493,10 +1493,10 @@ class Client extends EventEmitter { * @returns {Promise} */ editWebhookMessage(webhookID, token, messageID, options) { - if (!options.content && !options.embeds && !options.file) { + if(!options.content && !options.embeds && !options.file) { return Promise.reject(new Error("No content, embeds or file")); } - if (options.allowedMentions) { + if(options.allowedMentions) { options.allowed_mentions = this._formatAllowedMentions(options.allowedMentions); } return this.requestHandler.request("PATCH", Endpoints.WEBHOOK_MESSAGE(webhookID, token, messageID), false, options, options.file).then((response) => new Message(response, this)); @@ -1513,7 +1513,7 @@ class Client extends EventEmitter { secret, code }).then((data) => { - if (data.token) { + if(data.token) { this._token = data.token; } }); @@ -1558,7 +1558,7 @@ class Client extends EventEmitter { * @returns {Promise} */ executeWebhook(webhookID, token, options) { - if (!options.content && !options.file && !options.embeds) { + if(!options.content && !options.file && !options.embeds) { return Promise.reject(new Error("No content, file, or embeds")); } return this.requestHandler.request("POST", Endpoints.WEBHOOK_TOKEN(webhookID, token) + (options.wait ? "?wait=true" : ""), !!options.auth, { @@ -1578,7 +1578,7 @@ class Client extends EventEmitter { * @returns {Object} An object containing the NewsChannel's ID and the new webhook's ID */ followChannel(channelID, webhookChannelID) { - return this.requestHandler.request("POST", Endpoints.CHANNEL_FOLLOW(channelID), true, { webhook_channel_id: webhookChannelID }); + return this.requestHandler.request("POST", Endpoints.CHANNEL_FOLLOW(channelID), true, {webhook_channel_id: webhookChannelID}); } /** @@ -1586,7 +1586,7 @@ class Client extends EventEmitter { * @returns {Promise} Resolves with an object containing gateway connection info */ getBotGateway() { - if (!this._token.startsWith("Bot ")) { + if(!this._token.startsWith("Bot ")) { this._token = "Bot " + this._token; } return this.requestHandler.request("GET", Endpoints.GATEWAY_BOT, true); @@ -1598,11 +1598,11 @@ class Client extends EventEmitter { * @returns {CategoryChannel | GroupChannel | PrivateChannel | TextChannel | VoiceChannel | NewsChannel} */ getChannel(channelID) { - if (!channelID) { + if(!channelID) { throw new Error(`Invalid channel ID: ${channelID}`); } - if (this.channelGuildMap[channelID] && this.guilds.get(this.channelGuildMap[channelID])) { + if(this.channelGuildMap[channelID] && this.guilds.get(this.channelGuildMap[channelID])) { return this.guilds.get(this.channelGuildMap[channelID]).channels.get(channelID); } return this.privateChannels.get(channelID) || this.groupChannels.get(channelID); @@ -1640,7 +1640,7 @@ class Client extends EventEmitter { * @returns {Promise} */ getDMChannel(userID) { - if (this.privateChannelMap[userID]) { + if(this.privateChannelMap[userID]) { return Promise.resolve(this.privateChannels.get(this.privateChannelMap[userID])); } return this.requestHandler.request("POST", Endpoints.USER_CHANNELS("@me"), true, { @@ -1677,27 +1677,27 @@ class Client extends EventEmitter { * @returns {Promise<{users: User[], entries: GuildAuditLogEntry[], integrations: PartialIntegration[], webhooks: Webhook[]}>} */ getGuildAuditLog(guildID, options = {}, before, actionType, userID) { - if (!options || typeof options !== "object") { + if(!options || typeof options !== "object") { options = { limit: options }; } - if (options.limit === undefined) { // Legacy behavior + if(options.limit === undefined) { // Legacy behavior options.limit = 50; } - if (actionType !== undefined) { + if(actionType !== undefined) { options.actionType = actionType; } - if (before !== undefined) { + if(before !== undefined) { options.before = before; } - if (userID !== undefined) { + if(userID !== undefined) { options.userID = userID; } - if (options.actionType !== undefined) { + if(options.actionType !== undefined) { options.action_type = options.actionType; } - if (options.userID !== undefined) { + if(options.userID !== undefined) { options.user_id = options.userID; } return this.requestHandler.request("GET", Endpoints.GUILD_AUDIT_LOGS(guildID), true, options).then((data) => { @@ -1886,24 +1886,24 @@ class Client extends EventEmitter { * @returns {Promise>} */ getMessageReaction(channelID, messageID, reaction, options = {}, before, after) { - if (reaction === decodeURI(reaction)) { + if(reaction === decodeURI(reaction)) { reaction = encodeURIComponent(reaction); } - if (!options || typeof options !== "object") { + if(!options || typeof options !== "object") { options = { limit: options }; } - if (options.limit === undefined) { // Legacy behavior + if(options.limit === undefined) { // Legacy behavior options.limit = 100; } - if (before !== undefined) { + if(before !== undefined) { options.before = before; } - if (after !== undefined) { + if(after !== undefined) { options.after = after; } - if (options.before) { + if(options.before) { this.emit("warn", "[DEPRECATED] getMessageReaction() was called with a `before` parameter. Discord no longer supports this parameter"); } return this.requestHandler.request("GET", Endpoints.CHANNEL_MESSAGE_REACTION(channelID, messageID, reaction), true, options).then((users) => users.map((user) => new User(user, this))); @@ -1923,25 +1923,25 @@ class Client extends EventEmitter { * @returns {Promise>} */ async getMessages(channelID, options = {}, before, after, around) { - if (!options || typeof options !== "object") { + if(!options || typeof options !== "object") { options = { limit: options }; } - if (options.limit === undefined) { // Legacy behavior + if(options.limit === undefined) { // Legacy behavior options.limit = 50; } - if (after !== undefined) { + if(after !== undefined) { options.after = after; } - if (around !== undefined) { + if(around !== undefined) { options.around = around; } - if (before !== undefined) { + if(before !== undefined) { options.before = before; } let limit = options.limit; - if (limit && limit > 100) { + if(limit && limit > 100) { let logs = []; const get = async (_before, _after) => { const messages = await this.requestHandler.request("GET", Endpoints.CHANNEL_MESSAGES(channelID), true, { @@ -1949,12 +1949,12 @@ class Client extends EventEmitter { before: _before || undefined, after: _after || undefined }); - if (limit <= messages.length) { + if(limit <= messages.length) { return (_after ? messages.slice(messages.length - limit, messages.length).map((message) => new Message(message, this)).concat(logs) : logs.concat(messages.slice(0, limit).map((message) => new Message(message, this)))); } limit -= messages.length; logs = (_after ? messages.map((message) => new Message(message, this)).concat(logs) : logs.concat(messages.map((message) => new Message(message, this)))); - if (messages.length < 100) { + if(messages.length < 100) { return logs; } this.emit("debug", `Getting ${limit} more messages during getMessages for ${channelID}: ${_before} ${_after}`, -1); @@ -1966,7 +1966,7 @@ class Client extends EventEmitter { return messages.map((message) => { try { return new Message(message, this); - } catch (err) { + } catch(err) { this.emit("error", `Error creating message from channel messages\n${err.stack}\n${JSON.stringify(messages)}`); return null; } @@ -2012,7 +2012,7 @@ class Client extends EventEmitter { * @returns {Promise} */ getRESTChannel(channelID) { - if (!this.options.restMode) { + if(!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.CHANNEL(channelID), true) @@ -2026,7 +2026,7 @@ class Client extends EventEmitter { * @returns {Promise} */ getRESTGuild(guildID, withCounts = false) { - if (!this.options.restMode) { + if(!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.GUILD(guildID), true, { @@ -2040,7 +2040,7 @@ class Client extends EventEmitter { * @returns {Promise} */ getRESTGuildChannels(guildID) { - if (!this.options.restMode) { + if(!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.GUILD_CHANNELS(guildID), true) @@ -2054,7 +2054,7 @@ class Client extends EventEmitter { * @returns {Promise} An emoji object */ getRESTGuildEmoji(guildID, emojiID) { - if (!this.options.restMode) { + if(!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.GUILD_EMOJI(guildID, emojiID), true); @@ -2066,7 +2066,7 @@ class Client extends EventEmitter { * @returns {Promise>} An array of guild emoji objects */ getRESTGuildEmojis(guildID) { - if (!this.options.restMode) { + if(!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.GUILD_EMOJIS(guildID), true); @@ -2079,7 +2079,7 @@ class Client extends EventEmitter { * @returns {Promise} */ getRESTGuildMember(guildID, memberID) { - if (!this.options.restMode) { + if(!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.GUILD_MEMBER(guildID, memberID), true).then((member) => new Member(member, this.guilds.get(guildID), this)); @@ -2095,15 +2095,15 @@ class Client extends EventEmitter { * @returns {Promise>} */ getRESTGuildMembers(guildID, options = {}, after) { - if (!this.options.restMode) { + if(!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } - if (!options || typeof options !== "object") { + if(!options || typeof options !== "object") { options = { limit: options }; } - if (after !== undefined) { + if(after !== undefined) { options.after = after; } return this.requestHandler.request("GET", Endpoints.GUILD_MEMBERS(guildID), true, options).then((members) => members.map((member) => new Member(member, this.guilds.get(guildID), this))); @@ -2115,7 +2115,7 @@ class Client extends EventEmitter { * @returns {Promise>} */ getRESTGuildRoles(guildID) { - if (!this.options.restMode) { + if(!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.GUILD_ROLES(guildID), true).then((roles) => roles.map((role) => new Role(role, null))); @@ -2133,18 +2133,18 @@ class Client extends EventEmitter { */ getRESTGuilds(options = {}, before, after) { // TODO type - if (!this.options.restMode) { + if(!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } - if (!options || typeof options !== "object") { + if(!options || typeof options !== "object") { options = { limit: options }; } - if (after !== undefined) { + if(after !== undefined) { options.after = after; } - if (before !== undefined) { + if(before !== undefined) { options.before = before; } return this.requestHandler.request("GET", Endpoints.USER_GUILDS("@me"), true, options).then((guilds) => guilds.map((guild) => new Guild(guild, this))); @@ -2156,7 +2156,7 @@ class Client extends EventEmitter { * @returns {Promise} */ getRESTUser(userID) { - if (!this.options.restMode) { + if(!this.options.restMode) { return Promise.reject(new Error("Eris REST mode is not enabled")); } return this.requestHandler.request("GET", Endpoints.USER(userID), true).then((user) => new User(user, this)); @@ -2264,10 +2264,10 @@ class Client extends EventEmitter { */ joinVoiceChannel(channelID, options = {}) { const channel = this.getChannel(channelID); - if (!channel) { + if(!channel) { return Promise.reject(new Error("Channel not found")); } - if (channel.guild && !(channel.permissionsOf(this.user.id).allow & Constants.Permissions.voiceConnect)) { + if(channel.guild && !(channel.permissionsOf(this.user.id).allow & Constants.Permissions.voiceConnect)) { return Promise.reject(new Error("Insufficient permission to connect to voice channel")); } this.shards.get(this.guildShardMap[this.channelGuildMap[channelID]] || 0).sendWS(Constants.GatewayOPCodes.VOICE_STATE_UPDATE, { @@ -2276,7 +2276,7 @@ class Client extends EventEmitter { self_mute: false, self_deaf: false }); - if (options.opusOnly === undefined) { + if(options.opusOnly === undefined) { options.opusOnly = this.options.opusOnly; } return this.voiceConnections.join(this.channelGuildMap[channelID] || "call", channelID, options); @@ -2309,7 +2309,7 @@ class Client extends EventEmitter { * @arg {String} channelID The ID of the voice channel */ leaveVoiceChannel(channelID) { - if (!channelID || !this.channelGuildMap[channelID]) { + if(!channelID || !this.channelGuildMap[channelID]) { return; } this.closeVoiceConnection(this.channelGuildMap[channelID]); @@ -2360,32 +2360,32 @@ class Client extends EventEmitter { * @returns {Promise} Resolves with the number of messages deleted */ async purgeChannel(channelID, options, filter, before, after, reason) { - if (!options || typeof options !== "object") { + if(!options || typeof options !== "object") { options = { limit: options }; } - if (after !== undefined) { + if(after !== undefined) { options.after = after; } - if (before !== undefined) { + if(before !== undefined) { options.before = before; } - if (filter !== undefined) { + if(filter !== undefined) { options.filter = filter; } - if (reason !== undefined) { + if(reason !== undefined) { options.reason = reason; } - if (typeof options.filter === "string") { + if(typeof options.filter === "string") { const filter = options.filter; options.filter = (msg) => msg.content.includes(filter); } let limit = options.limit; - if (typeof limit !== "number") { + if(typeof limit !== "number") { throw new TypeError(`Invalid limit: ${limit}`); } - if (limit !== -1 && limit <= 0) { + if(limit !== -1 && limit <= 0) { return 0; } const toDelete = []; @@ -2393,15 +2393,15 @@ class Client extends EventEmitter { let done = false; const checkToDelete = async () => { const messageIDs = (done && toDelete) || (toDelete.length >= 100 && toDelete.splice(0, 100)); - if (messageIDs) { + if(messageIDs) { deleted += messageIDs.length; await this.deleteMessages(channelID, messageIDs, options.reason); - if (done) { + if(done) { return deleted; } await sleep(1000); return checkToDelete(); - } else if (done) { + } else if(done) { return deleted; } else { await sleep(250); @@ -2414,26 +2414,26 @@ class Client extends EventEmitter { before: _before, after: _after }); - if (limit !== -1 && limit <= 0) { + if(limit !== -1 && limit <= 0) { done = true; return; } - for (const message of messages) { - if (limit !== -1 && limit <= 0) { + for(const message of messages) { + if(limit !== -1 && limit <= 0) { break; } - if (message.timestamp < Date.now() - 1209600000) { // 14d * 24h * 60m * 60s * 1000ms + if(message.timestamp < Date.now() - 1209600000) { // 14d * 24h * 60m * 60s * 1000ms done = true; return; } - if (!options.filter || options.filter(message)) { + if(!options.filter || options.filter(message)) { toDelete.push(message.id); } - if (limit !== -1) { + if(limit !== -1) { limit--; } } - if ((limit !== -1 && limit <= 0) || messages.length < 100) { + if((limit !== -1 && limit <= 0) || messages.length < 100) { done = true; return; } @@ -2476,7 +2476,7 @@ class Client extends EventEmitter { * @returns {Promise} */ removeMessageReaction(channelID, messageID, reaction, userID) { - if (reaction === decodeURI(reaction)) { + if(reaction === decodeURI(reaction)) { reaction = encodeURIComponent(reaction); } return this.requestHandler.request("DELETE", Endpoints.CHANNEL_MESSAGE_REACTION_USER(channelID, messageID, reaction, userID || "@me"), true); @@ -2490,7 +2490,7 @@ class Client extends EventEmitter { * @returns {Promise} */ removeMessageReactionEmoji(channelID, messageID, reaction) { - if (reaction === decodeURI(reaction)) { + if(reaction === decodeURI(reaction)) { reaction = encodeURIComponent(reaction); } return this.requestHandler.request("DELETE", Endpoints.CHANNEL_MESSAGE_REACTION(channelID, messageID, reaction), true); @@ -2704,42 +2704,42 @@ class Client extends EventEmitter { } _formatAllowedMentions(allowed) { - if (!allowed) { + if(!allowed) { return this.options.allowedMentions; } const result = { parse: [] }; - if (allowed.everyone) { + if(allowed.everyone) { result.parse.push("everyone"); } - if (allowed.roles === true) { + if(allowed.roles === true) { result.parse.push("roles"); - } else if (Array.isArray(allowed.roles)) { - if (allowed.roles.length > 100) { + } else if(Array.isArray(allowed.roles)) { + if(allowed.roles.length > 100) { throw new Error("Allowed role mentions cannot exceed 100."); } result.roles = allowed.roles; } - if (allowed.users === true) { + if(allowed.users === true) { result.parse.push("users"); - } else if (Array.isArray(allowed.users)) { - if (allowed.users.length > 100) { + } else if(Array.isArray(allowed.users)) { + if(allowed.users.length > 100) { throw new Error("Allowed user mentions cannot exceed 100."); } result.users = allowed.users; } - if (allowed.repliedUser !== undefined) { + if(allowed.repliedUser !== undefined) { result.replied_user = allowed.repliedUser; } return result; } _formatImage(url, format, size) { - if (!format || !Constants.ImageFormats.includes(format.toLowerCase())) { + if(!format || !Constants.ImageFormats.includes(format.toLowerCase())) { format = url.includes("/a_") ? "gif" : this.options.defaultImageFormat; } - if (!size || size < Constants.ImageSizeBoundaries.MINIMUM || size > Constants.ImageSizeBoundaries.MAXIMUM || (size & (size - 1))) { + if(!size || size < Constants.ImageSizeBoundaries.MINIMUM || size > Constants.ImageSizeBoundaries.MAXIMUM || (size & (size - 1))) { size = this.options.defaultImageSize; } return `${Endpoints.CDN_URL}${url}.${format}?size=${size}`; From af43237d2efe501cd5697379656c15ebab3a1e20 Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Thu, 19 Aug 2021 07:24:18 -0500 Subject: [PATCH 08/11] remove no content checks --- lib/Client.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/lib/Client.js b/lib/Client.js index c24647b88..53064347e 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -603,8 +603,6 @@ class Client extends EventEmitter { }; } else if(content.content !== undefined && typeof content.content !== "string") { content.content = "" + content.content; - } else if(content.content === undefined && !content.embed && (!content.embeds || content.embeds.length === 0) && !file && (!content.stickerIDs || content.stickerIDs.length === 0)) { - return Promise.reject(new Error("No content, file, stickers, or embeds")); } else if(content.embed && !content.embeds) { this.emit("warn", "[DEPRECATED] content.embed is deprecated. Use content.embeds instead"); content.embeds = [content.embed]; @@ -633,8 +631,6 @@ class Client extends EventEmitter { this.emit("warn", "[DEPRECATED] content.messageReferenceID is deprecated. Use content.messageReference instead"); content.message_reference = {message_id: content.messageReferenceID}; } - } else if(!file) { - return Promise.reject(new Error("No content, file, or embeds")); } return this.requestHandler.request("POST", Endpoints.CHANNEL_MESSAGES(channelID), true, content, file).then((message) => new Message(message, this)); } @@ -1244,8 +1240,6 @@ class Client extends EventEmitter { }; } else if(content.content !== undefined && typeof content.content !== "string") { content.content = "" + content.content; - } else if(content.content === undefined && !content.embed && (!content.embeds || content.embeds.length === 0) && content.flags === undefined) { - return Promise.reject(new Error("No content, embeds or flags")); } else if(content.embed && !content.embeds) { this.emit("warn", "[DEPRECATED] content.embed is deprecated. Use content.embeds instead"); content.embeds = [content.embed]; @@ -1493,9 +1487,6 @@ class Client extends EventEmitter { * @returns {Promise} */ editWebhookMessage(webhookID, token, messageID, options) { - if(!options.content && !options.embeds && !options.file) { - return Promise.reject(new Error("No content, embeds or file")); - } if(options.allowedMentions) { options.allowed_mentions = this._formatAllowedMentions(options.allowedMentions); } @@ -1558,9 +1549,6 @@ class Client extends EventEmitter { * @returns {Promise} */ executeWebhook(webhookID, token, options) { - if(!options.content && !options.file && !options.embeds) { - return Promise.reject(new Error("No content, file, or embeds")); - } return this.requestHandler.request("POST", Endpoints.WEBHOOK_TOKEN(webhookID, token) + (options.wait ? "?wait=true" : ""), !!options.auth, { content: options.content, embeds: options.embeds, From eb92f1f3254144a1e7a29ec0393c5de787ccec7b Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Thu, 19 Aug 2021 09:19:36 -0500 Subject: [PATCH 09/11] AdvancedMessageContent interface --- index.d.ts | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/index.d.ts b/index.d.ts index 01e23376d..d4224e0c3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -53,20 +53,6 @@ declare namespace Eris { type VerificationLevel = 0 | 1 | 2 | 3 | 4; // Message - type AdvancedMessageContent = { - allowedMentions?: AllowedMentions; - components?: ActionRow[]; - content?: string; - /** @deprecated */ - embed?: EmbedOptions; - embeds?: EmbedOptions[]; - flags?: number; - messageReference?: MessageReferenceReply; - /** @deprecated */ - messageReferenceID?: string; - stickerIDs?: string[]; - tts?: boolean; - }; type ActionRowComponents = Button | SelectMenu; type Button = InteractionButton | URLButton; type Component = ActionRow | ActionRowComponents; @@ -855,6 +841,21 @@ declare namespace Eris { command: Command; timeout: NodeJS.Timer; } + + interface AdvancedMessageContent { + allowedMentions?: AllowedMentions; + components?: ActionRow[]; + content?: string; + /** @deprecated */ + embed?: EmbedOptions; + embeds?: EmbedOptions[]; + flags?: number; + messageReference?: MessageReferenceReply; + /** @deprecated */ + messageReferenceID?: string; + stickerIDs?: string[]; + tts?: boolean; + } interface AllowedMentions { everyone?: boolean; repliedUser?: boolean; From 297779d906dc83258cde10fc98bcdad2f81ab1d4 Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Thu, 19 Aug 2021 12:02:13 -0500 Subject: [PATCH 10/11] PartialChannel#id & PartialRole#id are not number & required Co-Authored-By: Catboy --- index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index d4224e0c3..8c10759c6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -141,7 +141,7 @@ declare namespace Eris { } interface PartialChannel { bitrate?: number; - id?: number; + id: string; name?: string; nsfw?: boolean; parent_id?: number; @@ -1013,7 +1013,7 @@ declare namespace Eris { interface PartialRole { color?: number; hoist?: boolean; - id?: number; + id: string; mentionable?: boolean; name?: string; permissions?: number; From e93f03ce996983e5b2d696b92902b5d0f070b522 Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Thu, 2 Sep 2021 15:51:41 -0500 Subject: [PATCH 11/11] bsian a bitch Co-authored-by: bsian03 --- index.d.ts | 1 - lib/Client.js | 2 +- lib/structures/PrivateChannel.js | 2 +- lib/structures/TextChannel.js | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index a33599129..4bb8cc93f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -839,7 +839,6 @@ declare namespace Eris { command: Command; timeout: NodeJS.Timer; } - interface AdvancedMessageContent { allowedMentions?: AllowedMentions; components?: ActionRow[]; diff --git a/lib/Client.js b/lib/Client.js index 71fbd57f8..3b8593b25 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -607,7 +607,7 @@ class Client extends EventEmitter { * @arg {String} [content.messageReference.guildID] The guild ID of the referenced message * @arg {String} content.messageReference.messageID The message ID of the referenced message. This cannot reference a system message * @arg {String} [content.messageReferenceID] [DEPRECATED] The ID of the message should be replied to. Use `messageReference` instead - * @arg {Array} [content.stickerIDs] An array of ids corresponding to stickers to send + * @arg {Array} [content.stickerIDs] An array of IDs corresponding to stickers to send * @arg {Boolean} [content.tts] Set the message TTS flag * @arg {Object | Array} [file] A file object (or an Array of them) * @arg {Buffer} file.file A buffer containing file data diff --git a/lib/structures/PrivateChannel.js b/lib/structures/PrivateChannel.js index aecefde3c..54ff072f8 100644 --- a/lib/structures/PrivateChannel.js +++ b/lib/structures/PrivateChannel.js @@ -72,7 +72,7 @@ class PrivateChannel extends Channel { * @arg {String} [content.messageReference.guildID] The guild ID of the referenced message * @arg {String} content.messageReference.messageID The message ID of the referenced message. This cannot reference a system message * @arg {String} [content.messageReferenceID] [DEPRECATED] The ID of the message should be replied to. Use `messageReference` instead - * @arg {Array} [content.stickerIDs] An array of ids corresponding to the stickers to send + * @arg {Array} [content.stickerIDs] An array of IDs corresponding to the stickers to send * @arg {Boolean} [content.tts] Set the message TTS flag * @arg {Object} [file] A file object * @arg {Buffer} file.file A buffer containing file data diff --git a/lib/structures/TextChannel.js b/lib/structures/TextChannel.js index 52d5be394..88c5faf5e 100644 --- a/lib/structures/TextChannel.js +++ b/lib/structures/TextChannel.js @@ -93,7 +93,7 @@ class TextChannel extends GuildChannel { * @arg {String} [content.messageReference.guildID] The guild ID of the referenced message * @arg {String} content.messageReference.messageID The message ID of the referenced message. This cannot reference a system message * @arg {String} [content.messageReferenceID] [DEPRECATED] The ID of the message should be replied to. Use `messageReference` instead - * @arg {Array} [content.stickerIDs] An array of ids corresponding to the stickers to send + * @arg {Array} [content.stickerIDs] An array of IDs corresponding to the stickers to send * @arg {Boolean} [content.tts] Set the message TTS flag * @arg {Object} [file] A file object * @arg {Buffer} file.file A buffer containing file data