fix(MessageMentions): ignoreRepliedUser option in has() (v13)

#8365 v13.9.2
This commit is contained in:
March 7th 2022-08-01 12:37:46 +07:00
parent 7cf216fabf
commit cde18dc37e
2 changed files with 37 additions and 6 deletions

View File

@ -93,6 +93,13 @@ class MessageMentions {
*/ */
this._channels = null; this._channels = null;
/**
* Cached users for {@link MessageMentions#parsedUsers}
* @type {?Collection<Snowflake, User>}
* @private
*/
this._parsedUsers = null;
/** /**
* Crossposted channel data. * Crossposted channel data.
* @typedef {Object} CrosspostedChannel * @typedef {Object} CrosspostedChannel
@ -168,6 +175,23 @@ class MessageMentions {
return this._channels; return this._channels;
} }
/**
* Any user mentions that were included in the message content
* <info>Order as they appear first in the message content</info>
* @type {Collection<Snowflake, User>}
* @readonly
*/
get parsedUsers() {
if (this._parsedUsers) return this._parsedUsers;
this._parsedUsers = new Collection();
let matches;
while ((matches = this.constructor.USERS_PATTERN.exec(this._content)) !== null) {
const user = this.client.users.cache.get(matches[1]);
if (user) this._parsedUsers.set(user.id, user);
}
return this._parsedUsers;
}
/** /**
* Options used to check for a mention. * Options used to check for a mention.
* @typedef {Object} MessageMentionsHasOptions * @typedef {Object} MessageMentionsHasOptions
@ -187,14 +211,20 @@ class MessageMentions {
*/ */
has(data, { ignoreDirect = false, ignoreRoles = false, ignoreRepliedUser = false, ignoreEveryone = false } = {}) { has(data, { ignoreDirect = false, ignoreRoles = false, ignoreRepliedUser = false, ignoreEveryone = false } = {}) {
const user = this.client.users.resolve(data); const user = this.client.users.resolve(data);
const role = this.guild?.roles.resolve(data); if (!ignoreEveryone && user && this.everyone) return true;
const channel = this.client.channels.resolve(data);
const userWasRepliedTo = user && this.repliedUser?.id === user.id;
if (!ignoreRepliedUser && userWasRepliedTo && this.users.has(user.id)) return true;
if (!ignoreRepliedUser && this.users.has(this.repliedUser?.id) && this.repliedUser?.id === user?.id) return true; if (!ignoreRepliedUser && this.users.has(this.repliedUser?.id) && this.repliedUser?.id === user?.id) return true;
if (!ignoreDirect) { if (!ignoreDirect) {
if (this.users.has(user?.id)) return true; if (user && (!ignoreRepliedUser || this.parsedUsers.has(user.id)) && this.users.has(user.id)) return true;
if (this.roles.has(role?.id)) return true;
if (this.channels.has(channel?.id)) return true; const role = this.guild?.roles.resolve(data);
if (role && this.roles.has(role.id)) return true;
const channel = this.client.channels.resolve(data);
if (channel && this.channels.has(channel.id)) return true;
} }
if (user && !ignoreEveryone && this.everyone) return true; if (user && !ignoreEveryone && this.everyone) return true;
if (!ignoreRoles) { if (!ignoreRoles) {

3
typings/index.d.ts vendored
View File

@ -2103,13 +2103,14 @@ export class MessageMentions {
private _channels: Collection<Snowflake, AnyChannel> | null; private _channels: Collection<Snowflake, AnyChannel> | null;
private readonly _content: string; private readonly _content: string;
private _members: Collection<Snowflake, GuildMember> | null; private _members: Collection<Snowflake, GuildMember> | null;
private _parsedUsers: Collection<Snowflake, User> | null;
public readonly channels: Collection<Snowflake, AnyChannel>; public readonly channels: Collection<Snowflake, AnyChannel>;
public readonly client: Client; public readonly client: Client;
public everyone: boolean; public everyone: boolean;
public readonly guild: Guild; public readonly guild: Guild;
public has(data: UserResolvable | RoleResolvable | ChannelResolvable, options?: MessageMentionsHasOptions): boolean; public has(data: UserResolvable | RoleResolvable | ChannelResolvable, options?: MessageMentionsHasOptions): boolean;
public readonly members: Collection<Snowflake, GuildMember> | null; public readonly members: Collection<Snowflake, GuildMember> | null;
public readonly parsedUsers: Collection<Snowflake, User>;
public repliedUser: User | null; public repliedUser: User | null;
public roles: Collection<Snowflake, Role>; public roles: Collection<Snowflake, Role>;
public users: Collection<Snowflake, User>; public users: Collection<Snowflake, User>;