discordjs/collection

<Collection>.hasAny() is not working

F0rce opened this issue · 2 comments

F0rce commented

Issue description

As stated in the documentation, Collection has a function called hasAny() which accepts an Array as argument.
But the hasAny() function always returns false eventhough the member has one of the given roles 100 %.

When using has() it works perfectly fine.

Code sample

const roles = [ "roleid1", "roleid2" ];

// Not working logic
if (<message>.member.roles.cache.hasAny(roles)) {
  console.log("Member has any of the roles");
}

// Working Logic (pretty inefficient)
let pass = false;
for (let i = 0; i < roles.length; i++) {
  if (message.member.roles.cache.has(ignore[i])) {
    pass = true;
  }
}
if (pass) {
  console.log("Member has any of the roles");
}

discord.js version

13.3.1

Node.js version

v16.13.0

Operating system

Linux

Priority this issue should have

Medium (should be fixed soon)

Which partials do you have configured?

CHANNEL

Which gateway intents are you subscribing to?

GUILDS, GUILD_MEMBERS, GUILD_PRESENCES, GUILD_MESSAGES, DIRECT_MESSAGES

I have tested this issue on a development release

No response

It takes rest params, change your code to:

- if (<message>.member.roles.cache.hasAny(roles)) {
+ if (<message>.member.roles.cache.hasAny(...roles)) {

And it'll work.

Edit: I just noticed, while in the code it's clear:

collection/src/index.ts

Lines 46 to 48 in 3f19298

public hasAny(...keys: K[]) {
return keys.some((k) => super.has(k));
}

The website does not indicate that it's rest parameters, but instead, that it takes an array as a single parameter:

image

I have no idea if our docsgen generates this information, but we should look into this.

F0rce commented

@kyranet thank you very much for telling me. With the changes to use rest parameters it worked first try.

I think it is needed to update the docs / website because other people could also be running against the problem I had.

Have a great night and thanks for answering that fast.