fix: Remove global flag on regular expressions (#8177)

This commit is contained in:
Jiralite
2022-07-03 14:36:53 +01:00
committed by GitHub
parent fa010b5162
commit cdd9214212
5 changed files with 38 additions and 19 deletions

View File

@@ -12,11 +12,12 @@ const Events = require('../util/Events');
*/ */
class GuildTemplate extends Base { 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} * @type {RegExp}
* @memberof GuildTemplate * @memberof GuildTemplate
*/ */
static GuildTemplatesPattern = /discord(?:app)?\.(?:com\/template|new)\/([\w-]{2,255})/gi; static GuildTemplatesPattern = /discord(?:app)?\.(?:com\/template|new)\/(?<code>[\w-]{2,255})/i;
constructor(client, data) { constructor(client, data) {
super(client); super(client);

View File

@@ -13,11 +13,12 @@ const { Error, ErrorCodes } = require('../errors');
*/ */
class Invite extends Base { 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} * @type {RegExp}
* @memberof Invite * @memberof Invite
*/ */
static InvitesPattern = /discord(?:(?:app)?\.com\/invite|\.gg(?:\/invite)?)\/([\w-]{2,255})/gi; static InvitesPattern = /discord(?:(?:app)?\.com\/invite|\.gg(?:\/invite)?)\/(?<code>[\w-]{2,255})/i;
constructor(client, data) { constructor(client, data) {
super(client); super(client);

View File

@@ -1,6 +1,7 @@
'use strict'; 'use strict';
const { Collection } = require('@discordjs/collection'); const { Collection } = require('@discordjs/collection');
const { FormattingPatterns } = require('discord-api-types/v10');
const { flatten } = require('../util/Util'); const { flatten } = require('../util/Util');
/** /**
@@ -8,32 +9,44 @@ const { flatten } = require('../util/Util');
*/ */
class MessageMentions { 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} * @type {RegExp}
* @memberof MessageMentions * @memberof MessageMentions
*/ */
static EveryonePattern = /@(everyone|here)/g; static EveryonePattern = /@(?<mention>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} * @type {RegExp}
* @memberof MessageMentions * @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} * @type {RegExp}
* @memberof MessageMentions * @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} * @type {RegExp}
* @memberof MessageMentions * @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) { constructor(message, users, roles, everyone, crosspostedChannels, repliedUser) {
/** /**
@@ -186,10 +199,12 @@ class MessageMentions {
if (this._channels) return this._channels; if (this._channels) return this._channels;
this._channels = new Collection(); this._channels = new Collection();
let matches; let matches;
while ((matches = this.constructor.ChannelsPattern.exec(this._content)) !== null) {
const chan = this.client.channels.cache.get(matches[1]); while ((matches = this.constructor.GlobalChannelsPattern.exec(this._content)) !== null) {
if (chan) this._channels.set(chan.id, chan); const channel = this.client.channels.cache.get(matches.groups.id);
if (channel) this._channels.set(channel.id, channel);
} }
return this._channels; return this._channels;
} }

View File

@@ -33,7 +33,7 @@ class DataResolver extends null {
* @returns {string} * @returns {string}
*/ */
static resolveCode(data, regex) { static resolveCode(data, regex) {
return data.matchAll(regex).next().value?.[1] ?? data; return regex.exec(data)?.[1] ?? data;
} }
/** /**

View File

@@ -121,6 +121,7 @@ import {
APIAttachment, APIAttachment,
APIChannel, APIChannel,
ThreadAutoArchiveDuration, ThreadAutoArchiveDuration,
FormattingPatterns,
} from 'discord-api-types/v10'; } from 'discord-api-types/v10';
import { ChildProcess } from 'node:child_process'; import { ChildProcess } from 'node:child_process';
import { EventEmitter } from 'node:events'; import { EventEmitter } from 'node:events';
@@ -1873,10 +1874,11 @@ export class MessageMentions {
public crosspostedChannels: Collection<Snowflake, CrosspostedChannel>; public crosspostedChannels: Collection<Snowflake, CrosspostedChannel>;
public toJSON(): unknown; public toJSON(): unknown;
public static ChannelsPattern: RegExp; public static ChannelsPattern: typeof FormattingPatterns.Channel;
private static GlobalChannelsPattern: RegExp;
public static EveryonePattern: RegExp; public static EveryonePattern: RegExp;
public static RolesPattern: RegExp; public static RolesPattern: typeof FormattingPatterns.Role;
public static UsersPattern: RegExp; public static UsersPattern: typeof FormattingPatterns.User;
} }
export class MessagePayload { export class MessagePayload {