mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 17:43:30 +01:00
chore: consistency/prettier (#3852)
* chore: consistency/prettier * chore: rebase * chore: rebase * chore: include typings * fix: include typings file in prettier lint-staged
This commit is contained in:
@@ -10,7 +10,7 @@ const Util = require('./Util');
|
||||
*/
|
||||
class Collection extends BaseCollection {
|
||||
toJSON() {
|
||||
return this.map(e => typeof e.toJSON === 'function' ? e.toJSON() : Util.flatten(e));
|
||||
return this.map(e => (typeof e.toJSON === 'function' ? e.toJSON() : Util.flatten(e)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
const Package = exports.Package = require('../../package.json');
|
||||
const Package = (exports.Package = require('../../package.json'));
|
||||
const { Error, RangeError } = require('../errors');
|
||||
const browser = exports.browser = typeof window !== 'undefined';
|
||||
const browser = (exports.browser = typeof window !== 'undefined');
|
||||
|
||||
/**
|
||||
* Options for a client.
|
||||
@@ -90,8 +90,9 @@ exports.DefaultOptions = {
|
||||
},
|
||||
};
|
||||
|
||||
exports.UserAgent = browser ? null :
|
||||
`DiscordBot (${Package.homepage.split('#')[0]}, ${Package.version}) Node.js/${process.version}`;
|
||||
exports.UserAgent = browser
|
||||
? null
|
||||
: `DiscordBot (${Package.homepage.split('#')[0]}, ${Package.version}) Node.js/${process.version}`;
|
||||
|
||||
exports.WSCodes = {
|
||||
1000: 'WS_CLOSE_REQUESTED',
|
||||
@@ -100,12 +101,7 @@ exports.WSCodes = {
|
||||
4011: 'SHARDING_REQUIRED',
|
||||
};
|
||||
|
||||
const AllowedImageFormats = [
|
||||
'webp',
|
||||
'png',
|
||||
'jpg',
|
||||
'gif',
|
||||
];
|
||||
const AllowedImageFormats = ['webp', 'png', 'jpg', 'gif'];
|
||||
|
||||
const AllowedImageSizes = Array.from({ length: 8 }, (e, i) => 2 ** (i + 4));
|
||||
|
||||
@@ -303,13 +299,7 @@ exports.ShardEvents = {
|
||||
* sidebar for more information.</warn>
|
||||
* @typedef {string} PartialType
|
||||
*/
|
||||
exports.PartialTypes = keyMirror([
|
||||
'USER',
|
||||
'CHANNEL',
|
||||
'GUILD_MEMBER',
|
||||
'MESSAGE',
|
||||
'REACTION',
|
||||
]);
|
||||
exports.PartialTypes = keyMirror(['USER', 'CHANNEL', 'GUILD_MEMBER', 'MESSAGE', 'REACTION']);
|
||||
|
||||
/**
|
||||
* The type of a websocket message event, e.g. `MESSAGE_CREATE`. Here are the available events:
|
||||
@@ -439,13 +429,7 @@ exports.MessageTypes = [
|
||||
* * CUSTOM_STATUS
|
||||
* @typedef {string} ActivityType
|
||||
*/
|
||||
exports.ActivityTypes = [
|
||||
'PLAYING',
|
||||
'STREAMING',
|
||||
'LISTENING',
|
||||
'WATCHING',
|
||||
'CUSTOM_STATUS',
|
||||
];
|
||||
exports.ActivityTypes = ['PLAYING', 'STREAMING', 'LISTENING', 'WATCHING', 'CUSTOM_STATUS'];
|
||||
|
||||
exports.ChannelTypes = {
|
||||
TEXT: 0,
|
||||
@@ -464,34 +448,34 @@ exports.ClientApplicationAssetTypes = {
|
||||
|
||||
exports.Colors = {
|
||||
DEFAULT: 0x000000,
|
||||
WHITE: 0xFFFFFF,
|
||||
AQUA: 0x1ABC9C,
|
||||
GREEN: 0x2ECC71,
|
||||
BLUE: 0x3498DB,
|
||||
YELLOW: 0xFFFF00,
|
||||
PURPLE: 0x9B59B6,
|
||||
LUMINOUS_VIVID_PINK: 0xE91E63,
|
||||
GOLD: 0xF1C40F,
|
||||
ORANGE: 0xE67E22,
|
||||
RED: 0xE74C3C,
|
||||
GREY: 0x95A5A6,
|
||||
NAVY: 0x34495E,
|
||||
DARK_AQUA: 0x11806A,
|
||||
DARK_GREEN: 0x1F8B4C,
|
||||
WHITE: 0xffffff,
|
||||
AQUA: 0x1abc9c,
|
||||
GREEN: 0x2ecc71,
|
||||
BLUE: 0x3498db,
|
||||
YELLOW: 0xffff00,
|
||||
PURPLE: 0x9b59b6,
|
||||
LUMINOUS_VIVID_PINK: 0xe91e63,
|
||||
GOLD: 0xf1c40f,
|
||||
ORANGE: 0xe67e22,
|
||||
RED: 0xe74c3c,
|
||||
GREY: 0x95a5a6,
|
||||
NAVY: 0x34495e,
|
||||
DARK_AQUA: 0x11806a,
|
||||
DARK_GREEN: 0x1f8b4c,
|
||||
DARK_BLUE: 0x206694,
|
||||
DARK_PURPLE: 0x71368A,
|
||||
DARK_VIVID_PINK: 0xAD1457,
|
||||
DARK_GOLD: 0xC27C0E,
|
||||
DARK_ORANGE: 0xA84300,
|
||||
DARK_RED: 0x992D22,
|
||||
DARK_GREY: 0x979C9F,
|
||||
DARKER_GREY: 0x7F8C8D,
|
||||
LIGHT_GREY: 0xBCC0C0,
|
||||
DARK_NAVY: 0x2C3E50,
|
||||
BLURPLE: 0x7289DA,
|
||||
GREYPLE: 0x99AAB5,
|
||||
DARK_BUT_NOT_BLACK: 0x2C2F33,
|
||||
NOT_QUITE_BLACK: 0x23272A,
|
||||
DARK_PURPLE: 0x71368a,
|
||||
DARK_VIVID_PINK: 0xad1457,
|
||||
DARK_GOLD: 0xc27c0e,
|
||||
DARK_ORANGE: 0xa84300,
|
||||
DARK_RED: 0x992d22,
|
||||
DARK_GREY: 0x979c9f,
|
||||
DARKER_GREY: 0x7f8c8d,
|
||||
LIGHT_GREY: 0xbcc0c0,
|
||||
DARK_NAVY: 0x2c3e50,
|
||||
BLURPLE: 0x7289da,
|
||||
GREYPLE: 0x99aab5,
|
||||
DARK_BUT_NOT_BLACK: 0x2c2f33,
|
||||
NOT_QUITE_BLACK: 0x23272a,
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -501,11 +485,7 @@ exports.Colors = {
|
||||
* * ALL_MEMBERS
|
||||
* @typedef {string} ExplicitContentFilterLevel
|
||||
*/
|
||||
exports.ExplicitContentFilterLevels = [
|
||||
'DISABLED',
|
||||
'MEMBERS_WITHOUT_ROLES',
|
||||
'ALL_MEMBERS',
|
||||
];
|
||||
exports.ExplicitContentFilterLevels = ['DISABLED', 'MEMBERS_WITHOUT_ROLES', 'ALL_MEMBERS'];
|
||||
|
||||
/**
|
||||
* The value set for the verification levels for a guild:
|
||||
@@ -516,13 +496,7 @@ exports.ExplicitContentFilterLevels = [
|
||||
* * VERY_HIGH
|
||||
* @typedef {string} VerificationLevel
|
||||
*/
|
||||
exports.VerificationLevels = [
|
||||
'NONE',
|
||||
'LOW',
|
||||
'MEDIUM',
|
||||
'HIGH',
|
||||
'VERY_HIGH',
|
||||
];
|
||||
exports.VerificationLevels = ['NONE', 'LOW', 'MEDIUM', 'HIGH', 'VERY_HIGH'];
|
||||
|
||||
/**
|
||||
* An error encountered while performing an API request. Here are the potential errors:
|
||||
@@ -641,10 +615,7 @@ exports.APIErrors = {
|
||||
* * MENTIONS
|
||||
* @typedef {string} DefaultMessageNotifications
|
||||
*/
|
||||
exports.DefaultMessageNotifications = [
|
||||
'ALL',
|
||||
'MENTIONS',
|
||||
];
|
||||
exports.DefaultMessageNotifications = ['ALL', 'MENTIONS'];
|
||||
|
||||
/**
|
||||
* The value set for a team members's membership state:
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const fetch = require('node-fetch');
|
||||
const Util = require('../util/Util');
|
||||
const { Error: DiscordError, TypeError } = require('../errors');
|
||||
const { browser } = require('../util/Constants');
|
||||
const Util = require('../util/Util');
|
||||
|
||||
/**
|
||||
* The DataResolver identifies different objects and tries to resolve a specific piece of information from them.
|
||||
@@ -90,7 +90,7 @@ class DataResolver {
|
||||
|
||||
if (typeof resource === 'string') {
|
||||
if (/^https?:\/\//.test(resource)) {
|
||||
return fetch(resource).then(res => browser ? res.blob() : res.buffer());
|
||||
return fetch(resource).then(res => (browser ? res.blob() : res.buffer()));
|
||||
} else if (!browser) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const file = browser ? resource : path.resolve(resource);
|
||||
|
||||
@@ -41,7 +41,9 @@ class SnowflakeUtil {
|
||||
}
|
||||
if (INCREMENT >= 4095) INCREMENT = 0;
|
||||
// eslint-disable-next-line max-len
|
||||
const BINARY = `${(timestamp - EPOCH).toString(2).padStart(42, '0')}0000100000${(INCREMENT++).toString(2).padStart(12, '0')}`;
|
||||
const BINARY = `${(timestamp - EPOCH).toString(2).padStart(42, '0')}0000100000${(INCREMENT++)
|
||||
.toString(2)
|
||||
.padStart(12, '0')}`;
|
||||
return Util.binaryToID(BINARY);
|
||||
}
|
||||
|
||||
@@ -62,7 +64,9 @@ class SnowflakeUtil {
|
||||
* @returns {DeconstructedSnowflake} Deconstructed snowflake
|
||||
*/
|
||||
static deconstruct(snowflake) {
|
||||
const BINARY = Util.idToBinary(snowflake).toString(2).padStart(64, '0');
|
||||
const BINARY = Util.idToBinary(snowflake)
|
||||
.toString(2)
|
||||
.padStart(64, '0');
|
||||
const res = {
|
||||
timestamp: parseInt(BINARY.substring(0, 42), 2) + EPOCH,
|
||||
workerID: parseInt(BINARY.substring(42, 47), 2),
|
||||
@@ -71,7 +75,9 @@ class SnowflakeUtil {
|
||||
binary: BINARY,
|
||||
};
|
||||
Object.defineProperty(res, 'date', {
|
||||
get: function get() { return new Date(this.timestamp); },
|
||||
get: function get() {
|
||||
return new Date(this.timestamp);
|
||||
},
|
||||
enumerable: true,
|
||||
});
|
||||
return res;
|
||||
|
||||
@@ -60,7 +60,7 @@ class Structures {
|
||||
const received = `${extended.name || 'unnamed'}${prototype.name ? ` extends ${prototype.name}` : ''}`;
|
||||
throw new Error(
|
||||
'The class/prototype returned from the extender function must extend the existing structure class/prototype' +
|
||||
` (received function ${received}; expected extension of ${structures[structure].name}).`,
|
||||
` (received function ${received}; expected extension of ${structures[structure].name}).`,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
148
src/util/Util.js
148
src/util/Util.js
@@ -1,11 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
const { Colors, DefaultOptions, Endpoints } = require('./Constants');
|
||||
const { parse } = require('path');
|
||||
const fetch = require('node-fetch');
|
||||
const { Colors, DefaultOptions, Endpoints } = require('./Constants');
|
||||
const { Error: DiscordError, RangeError, TypeError } = require('../errors');
|
||||
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
||||
const isObject = d => typeof d === 'object' && d !== null;
|
||||
const { parse } = require('path');
|
||||
|
||||
/**
|
||||
* Contains various general-purpose utility methods. These functions are also available on the base `Discord` object.
|
||||
@@ -24,7 +24,12 @@ class Util {
|
||||
static flatten(obj, ...props) {
|
||||
if (!isObject(obj)) return obj;
|
||||
|
||||
props = Object.assign(...Object.keys(obj).filter(k => !k.startsWith('_')).map(k => ({ [k]: true })), ...props);
|
||||
props = Object.assign(
|
||||
...Object.keys(obj)
|
||||
.filter(k => !k.startsWith('_'))
|
||||
.map(k => ({ [k]: true })),
|
||||
...props,
|
||||
);
|
||||
|
||||
const out = {};
|
||||
|
||||
@@ -89,43 +94,52 @@ class Util {
|
||||
* @param {boolean} [options.inlineCodeContent=true] Whether to escape text inside inline code or not
|
||||
* @returns {string}
|
||||
*/
|
||||
static escapeMarkdown(text, {
|
||||
codeBlock = true,
|
||||
inlineCode = true,
|
||||
bold = true,
|
||||
italic = true,
|
||||
underline = true,
|
||||
strikethrough = true,
|
||||
spoiler = true,
|
||||
codeBlockContent = true,
|
||||
inlineCodeContent = true,
|
||||
} = {}) {
|
||||
static escapeMarkdown(
|
||||
text,
|
||||
{
|
||||
codeBlock = true,
|
||||
inlineCode = true,
|
||||
bold = true,
|
||||
italic = true,
|
||||
underline = true,
|
||||
strikethrough = true,
|
||||
spoiler = true,
|
||||
codeBlockContent = true,
|
||||
inlineCodeContent = true,
|
||||
} = {},
|
||||
) {
|
||||
if (!codeBlockContent) {
|
||||
return text.split('```').map((subString, index, array) => {
|
||||
if ((index % 2) && index !== array.length - 1) return subString;
|
||||
return Util.escapeMarkdown(subString, {
|
||||
inlineCode,
|
||||
bold,
|
||||
italic,
|
||||
underline,
|
||||
strikethrough,
|
||||
spoiler,
|
||||
inlineCodeContent,
|
||||
});
|
||||
}).join(codeBlock ? '\\`\\`\\`' : '```');
|
||||
return text
|
||||
.split('```')
|
||||
.map((subString, index, array) => {
|
||||
if (index % 2 && index !== array.length - 1) return subString;
|
||||
return Util.escapeMarkdown(subString, {
|
||||
inlineCode,
|
||||
bold,
|
||||
italic,
|
||||
underline,
|
||||
strikethrough,
|
||||
spoiler,
|
||||
inlineCodeContent,
|
||||
});
|
||||
})
|
||||
.join(codeBlock ? '\\`\\`\\`' : '```');
|
||||
}
|
||||
if (!inlineCodeContent) {
|
||||
return text.split(/(?<=^|[^`])`(?=[^`]|$)/g).map((subString, index, array) => {
|
||||
if ((index % 2) && index !== array.length - 1) return subString;
|
||||
return Util.escapeMarkdown(subString, {
|
||||
codeBlock,
|
||||
bold,
|
||||
italic,
|
||||
underline,
|
||||
strikethrough,
|
||||
spoiler,
|
||||
});
|
||||
}).join(inlineCode ? '\\`' : '`');
|
||||
return text
|
||||
.split(/(?<=^|[^`])`(?=[^`]|$)/g)
|
||||
.map((subString, index, array) => {
|
||||
if (index % 2 && index !== array.length - 1) return subString;
|
||||
return Util.escapeMarkdown(subString, {
|
||||
codeBlock,
|
||||
bold,
|
||||
italic,
|
||||
underline,
|
||||
strikethrough,
|
||||
spoiler,
|
||||
});
|
||||
})
|
||||
.join(inlineCode ? '\\`' : '`');
|
||||
}
|
||||
if (inlineCode) text = Util.escapeInlineCode(text);
|
||||
if (codeBlock) text = Util.escapeCodeBlock(text);
|
||||
@@ -228,10 +242,12 @@ class Util {
|
||||
return fetch(`${DefaultOptions.http.api}/v${DefaultOptions.http.version}${Endpoints.botGateway}`, {
|
||||
method: 'GET',
|
||||
headers: { Authorization: `Bot ${token.replace(/^Bot\s*/i, '')}` },
|
||||
}).then(res => {
|
||||
if (res.ok) return res.json();
|
||||
throw res;
|
||||
}).then(data => data.shards * (1000 / guildsPerShard));
|
||||
})
|
||||
.then(res => {
|
||||
if (res.ok) return res.json();
|
||||
throw res;
|
||||
})
|
||||
.then(data => data.shards * (1000 / guildsPerShard));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -415,14 +431,14 @@ class Util {
|
||||
*/
|
||||
static resolveColor(color) {
|
||||
if (typeof color === 'string') {
|
||||
if (color === 'RANDOM') return Math.floor(Math.random() * (0xFFFFFF + 1));
|
||||
if (color === 'RANDOM') return Math.floor(Math.random() * (0xffffff + 1));
|
||||
if (color === 'DEFAULT') return 0;
|
||||
color = Colors[color] || parseInt(color.replace('#', ''), 16);
|
||||
} else if (Array.isArray(color)) {
|
||||
color = (color[0] << 16) + (color[1] << 8) + color[2];
|
||||
}
|
||||
|
||||
if (color < 0 || color > 0xFFFFFF) throw new RangeError('COLOR_RANGE');
|
||||
if (color < 0 || color > 0xffffff) throw new RangeError('COLOR_RANGE');
|
||||
else if (color && isNaN(color)) throw new TypeError('COLOR_CONVERT');
|
||||
|
||||
return color;
|
||||
@@ -434,10 +450,11 @@ class Util {
|
||||
* @returns {Collection}
|
||||
*/
|
||||
static discordSort(collection) {
|
||||
return collection.sorted((a, b) =>
|
||||
a.rawPosition - b.rawPosition ||
|
||||
parseInt(b.id.slice(0, -10)) - parseInt(a.id.slice(0, -10)) ||
|
||||
parseInt(b.id.slice(10)) - parseInt(a.id.slice(10)),
|
||||
return collection.sorted(
|
||||
(a, b) =>
|
||||
a.rawPosition - b.rawPosition ||
|
||||
parseInt(b.id.slice(0, -10)) - parseInt(a.id.slice(0, -10)) ||
|
||||
parseInt(b.id.slice(10)) - parseInt(a.id.slice(10)),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -506,7 +523,11 @@ class Util {
|
||||
const low = parseInt((high % 10).toString(2) + num.slice(-32), 2);
|
||||
|
||||
dec = (low % 10).toString() + dec;
|
||||
num = Math.floor(high / 10).toString(2) + Math.floor(low / 10).toString(2).padStart(32, '0');
|
||||
num =
|
||||
Math.floor(high / 10).toString(2) +
|
||||
Math.floor(low / 10)
|
||||
.toString(2)
|
||||
.padStart(32, '0');
|
||||
}
|
||||
|
||||
num = parseInt(num, 2);
|
||||
@@ -535,7 +556,7 @@ class Util {
|
||||
*/
|
||||
static cleanContent(str, message) {
|
||||
if (message.client.options.disableMentions === 'everyone') {
|
||||
str = str.replace(/@([^<>@ ]*)/gsmu, (match, target) => {
|
||||
str = str.replace(/@([^<>@ ]*)/gmsu, (match, target) => {
|
||||
if (target.match(/^[&!]?\d+$/)) {
|
||||
return `@${target}`;
|
||||
} else {
|
||||
@@ -543,21 +564,22 @@ class Util {
|
||||
}
|
||||
});
|
||||
}
|
||||
str = str.replace(/<@!?[0-9]+>/g, input => {
|
||||
const id = input.replace(/<|!|>|@/g, '');
|
||||
if (message.channel.type === 'dm') {
|
||||
const user = message.client.users.cache.get(id);
|
||||
return user ? `@${user.username}` : input;
|
||||
}
|
||||
str = str
|
||||
.replace(/<@!?[0-9]+>/g, input => {
|
||||
const id = input.replace(/<|!|>|@/g, '');
|
||||
if (message.channel.type === 'dm') {
|
||||
const user = message.client.users.cache.get(id);
|
||||
return user ? `@${user.username}` : input;
|
||||
}
|
||||
|
||||
const member = message.channel.guild.members.cache.get(id);
|
||||
if (member) {
|
||||
return `@${member.displayName}`;
|
||||
} else {
|
||||
const user = message.client.users.cache.get(id);
|
||||
return user ? `@${user.username}` : input;
|
||||
}
|
||||
})
|
||||
const member = message.channel.guild.members.cache.get(id);
|
||||
if (member) {
|
||||
return `@${member.displayName}`;
|
||||
} else {
|
||||
const user = message.client.users.cache.get(id);
|
||||
return user ? `@${user.username}` : input;
|
||||
}
|
||||
})
|
||||
.replace(/<#[0-9]+>/g, input => {
|
||||
const channel = message.client.channels.cache.get(input.replace(/<|#|>/g, ''));
|
||||
return channel ? `#${channel.name}` : input;
|
||||
|
||||
Reference in New Issue
Block a user