首页 > 解决方案 > 无法从命令中找到角色

问题描述

我在我的不和谐服务器中有一些角色。有些只是为了颜色。我想让我的成员使用命令自己选择角色.cargo <color>

我有代码

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}`);
    }
}

但每次我输入.cargo它都会显示一个错误,比如角色不存在if(!role) return message.channel.send('Esse cargo não existe!');

我注意到的错误:

.cargo不显示服务器角色并返回角色不存在的消息

.cargo blue返回角色不存在的消息

标签: javascriptbotsdiscorddiscord.js

解决方案


[评论中的解决方案]
在第 5 行,你写了role.name.slice('#'.length).toLowerCase() === str.toLowerCase,没有括号。如果那是您的代码,它找不到角色,因为String.toLowerCase它是一个函数,请尝试调用它并查看它是否有效。


推荐阅读