#keyArray

1 messages ยท Page 1 of 1 (latest)

little needle
#

the snippet i provided was for a collection, i called .keys() on right

marsh crag
#

ok so a thread

little needle
#

so you should do that with your collection

#

your collection being oldMember.roles.cache

marsh crag
#

so like this ? ```js
const oldRoles = oldMember.roles.cache().filter(x => !options.excludedroles.includes(x)).filter(x => !newMember.roles.cache().includes(x))

const keyArray = [...oldRoles.keys()]```

#

that should work right ?

little needle
#

x is a role

marsh crag
little needle
#

it was a string before

marsh crag
#

๐Ÿ˜•

little needle
#

don't know what else to say, really

#

oldMember.roles.cache is the collection you want the keys of

marsh crag
little needle
#

so to get the keys before you used oldMember.roles.cache.keyArray() now that's gone

#

so you instead spread the .keys() into an array as i explained

little needle
#

then i don't understand why you are using an key array to begin with?

#

you want to check if the collection has one of multiple roles, of which you have the ids?

little needle
#

okay?

marsh crag
little needle
#

i still don't see what any of that has to do with my questions - or your initial question

#

what is it that you are trying to do here?

marsh crag
#

oh wait i messed up

little needle
#

so, again

const oldRoles = oldMember.roles.cache.keyArray().filter(x => !options.excludedroles.includes(x)).filter(x => !newMember.roles.cache.key().includes(x))

that was your code
now .keyArray() does not exist
instead, spread the cache's oldMember.roles.cache keys iterator oldMember.roles.cache.keys() into an array

quoting myself here:

https://discord.js.org/#/docs/collection/stable/class/Collection?scrollTo=keys returns an iterator, which you can collect into an array
const keyArray = [...myCollection.keys()] - to name one example for a collection instance

marsh crag
#

so It checks when a member gets the role add the member to the emebed then when removed remove it from the embed

marsh crag
#

make it into keys ?

little needle
#

try to read my answer more carefully

marsh crag
#

i am learning atm and i am confused

little needle
#

oldMember.roles.cache.keyArray() <-- this is what i'm telling you how to replace

#

ignore the rest for now

marsh crag
#

hmmmmmm

little needle
#

so do

instead, spread the cache's oldMember.roles.cache keys iterator oldMember.roles.cache.keys() into an array

quoting myself here:

https://discord.js.org/#/docs/collection/stable/class/Collection?scrollTo=keys returns an iterator, which you can collect into an array
const keyArray = [...myCollection.keys()] - to name one example for a collection instance

and then filter, as you did before

marsh crag
#

ok i got it

#
const oldRoles = [...oldMember.roles.cache.keys()].filter(x => ![...newMember.roles.cache.keys()].includes(x))``` this right ?
#

should work

#

Umm....... ?

little needle
#

maybe? i'd personally advise you against using keys here, it complicates the code and your execution time

#

you can #filter the collection directly

#

old.roles.cache.filter(role => !new.roles.cache.filter.has(role.id)) or similar

marsh crag
#

hmm

#

ok thx for the help

#

๐Ÿ™‚

little needle
#

#has is a direct key lookup and much, much faster than a full iteration on every single iteration (which is what you are doing here)

marsh crag
#

ok i see

little needle
#

and Collection#filter directly iterates the map, instead of first iterating it to convert it to an array and then iterating it again

ancient ridgeBOT
#

<:_:862626783890636830> Additional Information: Collections
read more

marsh crag
#

oh

marsh crag
#
rosterembed.addField("**" + role.name.toUpperCase() + "**", role.members.length === 0 ? "> No one has this Role" : ">>> " + role.members.map(member => `<@${member.user.id}> | \`${member.user.tag}\``).join("\n").substr(0, leftnum))``` so if none has the role it should say "No one has this Role" but it doesnt for some reason
ancient ridgeBOT
#

<:_:874569322742308864> Role#members
The cached guild members that have this role

little needle
#

Type: Collection
<Snowflake, GuildMember>

returns a collection, collections don't have a .length

ancient ridgeBOT
#

<:_:874569296821501952> Collection
A Map with additional utility methods. This is used throughout discord.js rather than Arrays for anything that has an ID, for significantly improved performance and ease-of-use.

little needle
#

#size is used for maps

marsh crag
little needle
#

try it

marsh crag
#

it say >>>

little needle
#

yeah, you tell it to do that

marsh crag
little needle
#

what's your new code?

marsh crag
# little needle what's your new code?

rosterembed.addField("**" + role.name.toUpperCase() + "**", role.members.size === 0 ? "> No one has this Role" : ">>> " + role.members.map(member => `<@${member.user.id}> | \`${member.user.tag}\``).join("\n").substr(0, leftnum))

little needle
#

console.log(role.members)

marsh crag