I am building a Discord bot, more specific, a rolecolor command. I made a version of this command, that worked. Only problem was that it was extremly inefficient, it took like 5 minutes for it to respond. This was because there where a lot of 'if' statements and other things the bot had to check before executing anything. The file had 129K lines, and my whole editting program was lagging. I now have a new plan, that is probably much more efficient:
The bot checks if a member has any roles that start with "SRC -". SRC means server role color, just a name every role has that is dedicated to being cosmetic. All my colorrole names start with "SRC - name" If it detects any, delete them. Await this process, and after that, add the new color. I have like 205 roles for colors. I CAN just do:
message.guild.members.cache
.get(user.id)
.roles.remove(roleone);
message.guild.members.cache
.get(user.id)
.roles.remove(roletwo);
This works, but then again, inefficient. Discord isn't that fast with deleting and adding roles. When I ran a test, it didn't give me any errors. Despite this, I thought something was going wrong because my roles wheren't changing. When I was running a debug, and checked again, the roles where finally updated. It just takes a while before updating. I would like to have this more efficient. Here are some code samples:
Role adding, after the role removal:
if (args[0] === "1") {
message.guild.members.cache
.get(user.id)
.roles.add(roleone);
message.channel.send(errmsg);
console.log(logmsg);
else if (args[0] === "2") //etc
So my question is, does someone know how to detect if the member has any roles that start with name
, so that only those roles can be deleted?
CodePudding user response:
.remove()
takes either a RoleResolvable
, Array of RoleResolvable
s, or a Collection
of RoleResolvable
s. You can filter the roles and pass it in
const member = message.guild.members.resolve(user.id)
const roles = member.roles.cache.filter(r => r.name.startsWith(`SRC -`))
await member.roles.remove(roles) // remove all roles from the member that start with "SRC -"