Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 38 additions & 7 deletions src/structures/MessageMentions.js
Expand Up @@ -93,6 +93,13 @@ class MessageMentions {
*/
this._channels = null;

/**
* Cached users for {@link MessageMentions#parsedUsers}
* @type {?Collection<Snowflake, User>}
* @private
*/
this._parsedUsers = null;

/**
* Crossposted channel data.
* @typedef {Object} CrosspostedChannel
Expand Down Expand Up @@ -168,6 +175,23 @@ class MessageMentions {
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.
* @typedef {Object} MessageMentionsHasOptions
Expand All @@ -187,16 +211,23 @@ class MessageMentions {
*/
has(data, { ignoreDirect = false, ignoreRoles = false, ignoreRepliedUser = false, ignoreEveryone = false } = {}) {
const user = this.client.users.resolve(data);
const role = this.guild?.roles.resolve(data);
const channel = this.client.channels.resolve(data);

if (!ignoreRepliedUser && this.users.has(this.repliedUser?.id) && this.repliedUser?.id === user?.id) return true;
if (!ignoreEveryone && user && this.everyone) return true;

const userWasRepliedTo = user && this.repliedUser?.id === user.id;

if (!ignoreRepliedUser && userWasRepliedTo && this.users.has(user.id)) return true;

if (!ignoreDirect) {
if (this.users.has(user?.id)) return true;
if (this.roles.has(role?.id)) return true;
if (this.channels.has(channel?.id)) return true;
if (user && (!ignoreRepliedUser || this.parsedUsers.has(user.id)) && this.users.has(user.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 (!ignoreRoles) {
const member = this.guild?.members.resolve(data);
if (member) {
Expand Down
2 changes: 2 additions & 0 deletions typings/index.d.ts
Expand Up @@ -1806,13 +1806,15 @@ export class MessageMentions {
private _channels: Collection<Snowflake, AnyChannel> | null;
private readonly _content: string;
private _members: Collection<Snowflake, GuildMember> | null;
private _parsedUsers: Collection<Snowflake, User> | null;

public readonly channels: Collection<Snowflake, AnyChannel>;
public readonly client: Client;
public everyone: boolean;
public readonly guild: Guild;
public has(data: UserResolvable | RoleResolvable | ChannelResolvable, options?: MessageMentionsHasOptions): boolean;
public readonly members: Collection<Snowflake, GuildMember> | null;
public readonly parsedUsers: Collection<Snowflake, User>;
public repliedUser: User | null;
public roles: Collection<Snowflake, Role>;
public users: Collection<Snowflake, User>;
Expand Down