#How to give/remove role based on a stat

13 messages · Page 1 of 1 (latest)

lapis shell
#

Anyone have a better way of doing this ?? Is not seems "safe" for me, is 2 day that I try to do a dynamic thing but I always get stuck

lethal robinBOT
#
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
brisk forge
#

this might be a bit of overdoing it, but personally i'd turn that roles array into a Collection, with uses being the key and id being the value

lapis shell
#

One of my attempts for make it dynamic```js
const firstRole = roles[roles.length-1];

if (!user.roles.cache.has(roles[0].id) && uses > roles[0].uses) {
const rolesId = roles.map(r => r.id);
user.roles.cache.forEach(e => {
if (rolesId.includes(e.id)) user.roles.remove(e.id);
});
user.roles.add(roles[0].id);
};

if (uses > roles[0].uses || uses < firstRole.uses) return; //da controlare tutto dinuovo cazzo

const toAdd = roles.filter(r => uses >= r.uses)[0];
console.log(toAdd);
const toRemove = roles[roles.map(r => r.id).indexOf(toAdd.id)+1];
console.log(toRemove);
toRemove && user.roles.cache.has(toRemove.id) ? user.roles.remove(toRemove.id) : null;
return user.roles.add(toAdd.id);

lapis shell
brisk forge
#

then you can either use stuff like Collection.findKey() or for (const [uses, id] of roles). if you sort the collection in descending order then you will get the highest fitting one before other ones

#

discord.js has Collections that extend native js Maps, by adding Array methods

lapis shell
#

Yea but I don't think it will be much difference from an array, I'm wrong?

brisk forge
#

with array you need to track/find the index to access the uses and id

#

with collection made the way i proposed you can do things like roles.get(currentUses)

#

and get a role id if it the currentUses happen to have a role (or nothing otherwise)

lapis shell
#

Let me try

lapis shell
#

Ok now I have the collection looking like this js { 10 => { id: '1153437898813743176', uses: 10, coins: 1000 }, 25 => { id: '1153438042032443473', uses: 25, coins: 2000 }, 40 => { id: '1153438271527997530', uses: 40, coins: 3000 }, 50 => { id: '1153438459558637729', uses: 50, coins: 4000 }, 60 => { id: '1153438952003469352', uses: 60, coins: 5000 } } and I get it with ```js
client.inviteTrackerRoles.get(uses);