Downgrade to v13
[vi] cảm giác đau khổ
This commit is contained in:
136
src/util/Util.js
136
src/util/Util.js
@@ -1,13 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
const { parse } = require('node:path');
|
||||
const process = require('node:process');
|
||||
const { Collection } = require('@discordjs/collection');
|
||||
const { ChannelType, RouteBases, Routes } = require('discord-api-types/v9');
|
||||
const { fetch } = require('undici');
|
||||
const Colors = require('./Colors');
|
||||
const fetch = require('node-fetch');
|
||||
const { Colors, Endpoints } = require('./Constants');
|
||||
const Options = require('./Options');
|
||||
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;
|
||||
|
||||
let deprecationEmittedForRemoveMentions = false;
|
||||
|
||||
/**
|
||||
* Contains various general-purpose utility methods.
|
||||
*/
|
||||
@@ -268,7 +272,8 @@ class Util extends null {
|
||||
*/
|
||||
static async fetchRecommendedShards(token, { guildsPerShard = 1_000, multipleOf = 1 } = {}) {
|
||||
if (!token) throw new DiscordError('TOKEN_MISSING');
|
||||
const response = await fetch(RouteBases.api + Routes.gatewayBot(), {
|
||||
const defaults = Options.createDefault();
|
||||
const response = await fetch(`${defaults.http.api}/v${defaults.http.version}${Endpoints.botGateway}`, {
|
||||
method: 'GET',
|
||||
headers: { Authorization: `Bot ${token.replace(/^Bot\s*/i, '')}` },
|
||||
});
|
||||
@@ -330,7 +335,7 @@ class Util extends null {
|
||||
static mergeDefault(def, given) {
|
||||
if (!given) return def;
|
||||
for (const key in def) {
|
||||
if (!Object.hasOwn(given, key) || given[key] === undefined) {
|
||||
if (!has(given, key) || given[key] === undefined) {
|
||||
given[key] = def[key];
|
||||
} else if (given[key] === Object(given[key])) {
|
||||
given[key] = Util.mergeDefault(def[key], given[key]);
|
||||
@@ -419,37 +424,37 @@ class Util extends null {
|
||||
* [255, 0, 255] // purple
|
||||
* ```
|
||||
* or one of the following strings:
|
||||
* - `Default`
|
||||
* - `White`
|
||||
* - `Aqua`
|
||||
* - `Green`
|
||||
* - `Blue`
|
||||
* - `Yellow`
|
||||
* - `Purple`
|
||||
* - `LuminousVividPink`
|
||||
* - `Fuchsia`
|
||||
* - `Gold`
|
||||
* - `Orange`
|
||||
* - `Red`
|
||||
* - `Grey`
|
||||
* - `Navy`
|
||||
* - `DarkAqua`
|
||||
* - `DarkGreen`
|
||||
* - `DarkBlue`
|
||||
* - `DarkPurple`
|
||||
* - `DarkVividPink`
|
||||
* - `DarkGold`
|
||||
* - `DarkOrange`
|
||||
* - `DarkRed`
|
||||
* - `DarkGrey`
|
||||
* - `DarkerGrey`
|
||||
* - `LightGrey`
|
||||
* - `DarkNavy`
|
||||
* - `Blurple`
|
||||
* - `Greyple`
|
||||
* - `DarkButNotBlack`
|
||||
* - `NotQuiteBlack`
|
||||
* - `Random`
|
||||
* - `DEFAULT`
|
||||
* - `WHITE`
|
||||
* - `AQUA`
|
||||
* - `GREEN`
|
||||
* - `BLUE`
|
||||
* - `YELLOW`
|
||||
* - `PURPLE`
|
||||
* - `LUMINOUS_VIVID_PINK`
|
||||
* - `FUCHSIA`
|
||||
* - `GOLD`
|
||||
* - `ORANGE`
|
||||
* - `RED`
|
||||
* - `GREY`
|
||||
* - `NAVY`
|
||||
* - `DARK_AQUA`
|
||||
* - `DARK_GREEN`
|
||||
* - `DARK_BLUE`
|
||||
* - `DARK_PURPLE`
|
||||
* - `DARK_VIVID_PINK`
|
||||
* - `DARK_GOLD`
|
||||
* - `DARK_ORANGE`
|
||||
* - `DARK_RED`
|
||||
* - `DARK_GREY`
|
||||
* - `DARKER_GREY`
|
||||
* - `LIGHT_GREY`
|
||||
* - `DARK_NAVY`
|
||||
* - `BLURPLE`
|
||||
* - `GREYPLE`
|
||||
* - `DARK_BUT_NOT_BLACK`
|
||||
* - `NOT_QUITE_BLACK`
|
||||
* - `RANDOM`
|
||||
* @typedef {string|number|number[]} ColorResolvable
|
||||
*/
|
||||
|
||||
@@ -460,8 +465,8 @@ class Util extends null {
|
||||
*/
|
||||
static resolveColor(color) {
|
||||
if (typeof color === 'string') {
|
||||
if (color === 'Random') return Math.floor(Math.random() * (0xffffff + 1));
|
||||
if (color === 'Default') return 0;
|
||||
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];
|
||||
@@ -493,17 +498,16 @@ class Util extends null {
|
||||
* @param {number} position New position for the object
|
||||
* @param {boolean} relative Whether `position` is relative to its current position
|
||||
* @param {Collection<string, Channel|Role>} sorted A collection of the objects sorted properly
|
||||
* @param {Client} client The client to use to patch the data
|
||||
* @param {string} route Route to call PATCH on
|
||||
* @param {APIRouter} route Route to call PATCH on
|
||||
* @param {string} [reason] Reason for the change
|
||||
* @returns {Promise<Channel[]|Role[]>} Updated item list, with `id` and `position` properties
|
||||
* @private
|
||||
*/
|
||||
static async setPosition(item, position, relative, sorted, client, route, reason) {
|
||||
static async setPosition(item, position, relative, sorted, route, reason) {
|
||||
let updatedItems = [...sorted.values()];
|
||||
Util.moveElementInArray(updatedItems, item, position, relative);
|
||||
updatedItems = updatedItems.map((r, i) => ({ id: r.id, position: i }));
|
||||
await client.rest.patch(route, { body: updatedItems, reason });
|
||||
await route.patch({ data: updatedItems, reason });
|
||||
return updatedItems;
|
||||
}
|
||||
|
||||
@@ -518,8 +522,34 @@ class Util extends null {
|
||||
const res = parse(path);
|
||||
return ext && res.ext.startsWith(ext) ? res.name : res.base.split('?')[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Breaks user, role and everyone/here mentions by adding a zero width space after every @ character
|
||||
* @param {string} str The string to sanitize
|
||||
* @returns {string}
|
||||
* @deprecated Use {@link BaseMessageOptions#allowedMentions} instead.
|
||||
*/
|
||||
static removeMentions(str) {
|
||||
if (!deprecationEmittedForRemoveMentions) {
|
||||
process.emitWarning(
|
||||
'The Util.removeMentions method is deprecated. Use MessageOptions#allowedMentions instead.',
|
||||
'DeprecationWarning',
|
||||
);
|
||||
|
||||
deprecationEmittedForRemoveMentions = true;
|
||||
}
|
||||
|
||||
return Util._removeMentions(str);
|
||||
}
|
||||
|
||||
static _removeMentions(str) {
|
||||
return str.replaceAll('@', '@\u200b');
|
||||
}
|
||||
|
||||
/**
|
||||
* The content to have all mentions replaced by the equivalent text.
|
||||
* <warn>When {@link Util.removeMentions} is removed, this method will no longer sanitize mentions.
|
||||
* Use {@link BaseMessageOptions#allowedMentions} instead to prevent mentions when sending a message.</warn>
|
||||
* @param {string} str The string to be converted
|
||||
* @param {TextBasedChannels} channel The channel the string was sent in
|
||||
* @returns {string}
|
||||
@@ -528,17 +558,17 @@ class Util extends null {
|
||||
str = str
|
||||
.replace(/<@!?[0-9]+>/g, input => {
|
||||
const id = input.replace(/<|!|>|@/g, '');
|
||||
if (channel.type === ChannelType.DM) {
|
||||
if (channel.type === 'DM') {
|
||||
const user = channel.client.users.cache.get(id);
|
||||
return user ? `@${user.username}` : input;
|
||||
return user ? Util._removeMentions(`@${user.username}`) : input;
|
||||
}
|
||||
|
||||
const member = channel.guild.members.cache.get(id);
|
||||
if (member) {
|
||||
return `@${member.displayName}`;
|
||||
return Util._removeMentions(`@${member.displayName}`);
|
||||
} else {
|
||||
const user = channel.client.users.cache.get(id);
|
||||
return user ? `@${user.username}` : input;
|
||||
return user ? Util._removeMentions(`@${user.username}`) : input;
|
||||
}
|
||||
})
|
||||
.replace(/<#[0-9]+>/g, input => {
|
||||
@@ -546,7 +576,7 @@ class Util extends null {
|
||||
return mentionedChannel ? `#${mentionedChannel.name}` : input;
|
||||
})
|
||||
.replace(/<@&[0-9]+>/g, input => {
|
||||
if (channel.type === ChannelType.DM) return input;
|
||||
if (channel.type === 'DM') return input;
|
||||
const role = channel.guild.roles.cache.get(input.replace(/<|@|>|&/g, ''));
|
||||
return role ? `@${role.name}` : input;
|
||||
});
|
||||
@@ -561,6 +591,18 @@ class Util extends null {
|
||||
static cleanCodeBlockContent(text) {
|
||||
return text.replaceAll('```', '`\u200b``');
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a sweep filter that sweeps archived threads
|
||||
* @param {number} [lifetime=14400] How long a thread has to be archived to be valid for sweeping
|
||||
* @deprecated When not using with `makeCache` use `Sweepers.archivedThreadSweepFilter` instead
|
||||
* @returns {SweepFilter}
|
||||
*/
|
||||
static archivedThreadSweepFilter(lifetime = 14400) {
|
||||
const filter = require('./Sweepers').archivedThreadSweepFilter(lifetime);
|
||||
filter.isDefault = true;
|
||||
return filter;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Util;
|
||||
|
||||
Reference in New Issue
Block a user