From cdd9214212892e30b3eaa161837c37516c5bcaa0 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Sun, 3 Jul 2022 14:36:53 +0100 Subject: [PATCH] fix: Remove global flag on regular expressions (#8177) --- .../src/structures/GuildTemplate.js | 5 ++- packages/discord.js/src/structures/Invite.js | 5 ++- .../src/structures/MessageMentions.js | 37 +++++++++++++------ packages/discord.js/src/util/DataResolver.js | 2 +- packages/discord.js/typings/index.d.ts | 8 ++-- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/packages/discord.js/src/structures/GuildTemplate.js b/packages/discord.js/src/structures/GuildTemplate.js index 311014a60..eba00b75b 100644 --- a/packages/discord.js/src/structures/GuildTemplate.js +++ b/packages/discord.js/src/structures/GuildTemplate.js @@ -12,11 +12,12 @@ const Events = require('../util/Events'); */ class GuildTemplate extends Base { /** - * Regular expression that globally matches guild template links + * A regular expression that globally matches guild template links. + * The `code` group property is present on the `exec()` result of this expression. * @type {RegExp} * @memberof GuildTemplate */ - static GuildTemplatesPattern = /discord(?:app)?\.(?:com\/template|new)\/([\w-]{2,255})/gi; + static GuildTemplatesPattern = /discord(?:app)?\.(?:com\/template|new)\/(?[\w-]{2,255})/i; constructor(client, data) { super(client); diff --git a/packages/discord.js/src/structures/Invite.js b/packages/discord.js/src/structures/Invite.js index f95d5681b..68699aba7 100644 --- a/packages/discord.js/src/structures/Invite.js +++ b/packages/discord.js/src/structures/Invite.js @@ -13,11 +13,12 @@ const { Error, ErrorCodes } = require('../errors'); */ class Invite extends Base { /** - * Regular expression that globally matches Discord invite links + * A regular expression that globally matches Discord invite links. + * The `code` group property is present on the `exec()` result of this expression. * @type {RegExp} * @memberof Invite */ - static InvitesPattern = /discord(?:(?:app)?\.com\/invite|\.gg(?:\/invite)?)\/([\w-]{2,255})/gi; + static InvitesPattern = /discord(?:(?:app)?\.com\/invite|\.gg(?:\/invite)?)\/(?[\w-]{2,255})/i; constructor(client, data) { super(client); diff --git a/packages/discord.js/src/structures/MessageMentions.js b/packages/discord.js/src/structures/MessageMentions.js index ca5c720a1..125d0f102 100644 --- a/packages/discord.js/src/structures/MessageMentions.js +++ b/packages/discord.js/src/structures/MessageMentions.js @@ -1,6 +1,7 @@ 'use strict'; const { Collection } = require('@discordjs/collection'); +const { FormattingPatterns } = require('discord-api-types/v10'); const { flatten } = require('../util/Util'); /** @@ -8,32 +9,44 @@ const { flatten } = require('../util/Util'); */ class MessageMentions { /** - * Regular expression that globally matches `@everyone` and `@here` + * A regular expression that matches `@everyone` and `@here`. + * The `mention` group property is present on the `exec` result of this expression. * @type {RegExp} * @memberof MessageMentions */ - static EveryonePattern = /@(everyone|here)/g; + static EveryonePattern = /@(?everyone|here)/; /** - * Regular expression that globally matches user mentions like `<@81440962496172032>` + * A regular expression that matches user mentions like `<@81440962496172032>`. + * The `id` group property is present on the `exec` result of this expression. * @type {RegExp} * @memberof MessageMentions */ - static UsersPattern = /<@!?(\d{17,19})>/g; + static UsersPattern = FormattingPatterns.UserWithOptionalNickname; /** - * Regular expression that globally matches role mentions like `<@&297577916114403338>` + * A regular expression that matches role mentions like `<@&297577916114403338>`. + * The `id` group property is present on the `exec` result of this expression. * @type {RegExp} * @memberof MessageMentions */ - static RolesPattern = /<@&(\d{17,19})>/g; + static RolesPattern = FormattingPatterns.Role; /** - * Regular expression that globally matches channel mentions like `<#222079895583457280>` + * A regular expression that matches channel mentions like `<#222079895583457280>`. + * The `id` group property is present on the `exec` result of this expression. * @type {RegExp} * @memberof MessageMentions */ - static ChannelsPattern = /<#(\d{17,19})>/g; + static ChannelsPattern = FormattingPatterns.Channel; + + /** + * A global regular expression variant of {@link MessageMentions.ChannelsPattern}. + * @type {RegExp} + * @memberof MessageMentions + * @private + */ + static GlobalChannelsPattern = new RegExp(this.ChannelsPattern.source, 'g'); constructor(message, users, roles, everyone, crosspostedChannels, repliedUser) { /** @@ -186,10 +199,12 @@ class MessageMentions { if (this._channels) return this._channels; this._channels = new Collection(); let matches; - while ((matches = this.constructor.ChannelsPattern.exec(this._content)) !== null) { - const chan = this.client.channels.cache.get(matches[1]); - if (chan) this._channels.set(chan.id, chan); + + while ((matches = this.constructor.GlobalChannelsPattern.exec(this._content)) !== null) { + const channel = this.client.channels.cache.get(matches.groups.id); + if (channel) this._channels.set(channel.id, channel); } + return this._channels; } diff --git a/packages/discord.js/src/util/DataResolver.js b/packages/discord.js/src/util/DataResolver.js index 80bff6721..bfc0f6afc 100644 --- a/packages/discord.js/src/util/DataResolver.js +++ b/packages/discord.js/src/util/DataResolver.js @@ -33,7 +33,7 @@ class DataResolver extends null { * @returns {string} */ static resolveCode(data, regex) { - return data.matchAll(regex).next().value?.[1] ?? data; + return regex.exec(data)?.[1] ?? data; } /** diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 1c4aed37b..210d00c77 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -121,6 +121,7 @@ import { APIAttachment, APIChannel, ThreadAutoArchiveDuration, + FormattingPatterns, } from 'discord-api-types/v10'; import { ChildProcess } from 'node:child_process'; import { EventEmitter } from 'node:events'; @@ -1873,10 +1874,11 @@ export class MessageMentions { public crosspostedChannels: Collection; public toJSON(): unknown; - public static ChannelsPattern: RegExp; + public static ChannelsPattern: typeof FormattingPatterns.Channel; + private static GlobalChannelsPattern: RegExp; public static EveryonePattern: RegExp; - public static RolesPattern: RegExp; - public static UsersPattern: RegExp; + public static RolesPattern: typeof FormattingPatterns.Role; + public static UsersPattern: typeof FormattingPatterns.User; } export class MessagePayload {