0
votes

I have some roles in my discord server. Some is just for colors. I want to my members select the roles by their own with the command .cargo <color>

I have the code

if (command === 'cargo') {
    const colors = message.guild.roles.filter(role => role.name.startsWith('#'));
    if (colors.size < 1) return message.channel.send('Não há cargos nesse servidor');

    const str = args.join(' ');
    const role = colors.find(role => role.name.slice('#'.length).toLowerCase() === str.toLowerCase);

    if(!role) return message.channel.send('Esse cargo não existe!');

    try {
        await message.member.removeRoles(colors);
        await message.member.addRole(role);
        message.channel.send(`Agora você tem o cargo ${role}!`);
    }
    catch (e) {
        message.channel.send(`Falhei :( ${e.message}`);
    }
}

but everytime I type .cargo it's showing an error like the role doesn't exist if(!role) return message.channel.send('Esse cargo não existe!');

Errors that i noticed:

.cargo doesn't show the server roles and return the message that the role doesn't exist

.cargo blue return the message that the role doesn't exist

1
Have you tried logging the variables to see if they contain what you think they do? Also, what is the point of '#'.length? That's just 1.Joseph Webber
On the 5th line, you wrote role.name.slice('#'.length).toLowerCase() === str.toLowerCase, without the parenthesis. Is that a typo in your post or that's your code? If that's your code, it can't find the role because String.toLowerCase is a function, try calling it and see if that worksFederico Grandi
@FedericoGrandi it was not a typo ;-; so it workedMiguel Machado
I'm getting missing permissions even tho the bot have itMiguel Machado
I've added an answer so that you can accept it and close the question. For that other error, if you don't find anything on that error please open a new question, since it's not related to this one.Federico Grandi

1 Answers

0
votes

[Solution from the comments]
On the 5th line, you wrote role.name.slice('#'.length).toLowerCase() === str.toLowerCase, without the parenthesis. If that's your code, it can't find the role because String.toLowerCase is a function, try calling it and see if that works.