【发布时间】:2019-04-12 10:25:23
【问题描述】:
我对 Node.js 还很陌生,目前正在开发一个 Discord 机器人。我试图让我的机器人根据用户传递的参数分配服务器角色。我知道我可以找到特定角色的名称,然后根据他们的响应为他们分配该角色。
但是:我的管理员几乎每周都会创建和删除角色,我不能每周都编辑我的代码来更新要分配的可用角色列表,所以我决定制作一行代码这将提取用户传递的参数,然后在 将 args 作为参数传递时找到角色名称。
我的代码如下所示:
if (command === 'role'){
let roleMember = message.member;
var mentionedRole = args[0];
let testRole = mentionedRole.toString();
// Debug message to check if it reads my input
message.channel.send(`TRYING! YOUR ARGS WERE: ${testRole}`);
// Define the role
let finalRole = message.guild.roles.find(r => r.name === testRole);
// Check if it reads the role successfully
message.channel.send(`I READ: ${finalRole}`);
// Add the role to the command author
roleMember.addRole(top).catch(console.error);
}
问题是当它返回它为finalRole 读取的内容时,它给了我this error。当机器人响应时,它将角色读取为空。我已经为此苦苦挣扎了好几个星期了,但我已经束手无策了。有谁知道我该如何解决这个问题?编辑:这是我的意思的一个例子:
!role top“Top”是这里的角色名称。
【问题讨论】:
标签: javascript node.js discord.js