javascript - 无法从命令中找到角色
问题描述
我在我的不和谐服务器中有一些角色。有些只是为了颜色。我想让我的成员使用命令自己选择角色.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
返回角色不存在的消息
解决方案
[评论中的解决方案]
在第 5 行,你写了role.name.slice('#'.length).toLowerCase() === str.toLowerCase
,没有括号。如果那是您的代码,它找不到角色,因为String.toLowerCase
它是一个函数,请尝试调用它并查看它是否有效。
推荐阅读
- sqlite - 访问 Akavache DatabasePath 值
- node.js - 为什么不是所有值都存储在 mongodb 中?
- c++ - IDsObjectPicker 在最新的 Windows 10 更新上失败
- angular - 文件上传/下载时显示和隐藏 Spinner
- asp.net - 如何绑定包含表单的视图和部分视图,我需要在主视图中访问
- python - 我们如何在本地机器上用 Python 开发基于 google earthengine 的应用程序?
- r - 对于字符串中的每个单词,检查它是否是另一个单词字符串的一部分
- python - openpyxl - 仅日期格式列
- javascript - Jquery 在请求中发送的数组后添加 [ ] 字符
- excel - PageSetup.Zoom 删除分页符并缩小导出