Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于args将角色分配给成员_Javascript_Node.js_Discord.js - Fatal编程技术网

Javascript 基于args将角色分配给成员

Javascript 基于args将角色分配给成员,javascript,node.js,discord.js,Javascript,Node.js,Discord.js,我是Node.js的新手,目前正在开发一个Discord机器人。我试图让我的机器人根据用户传递的参数分配服务器角色。我知道我可以找到特定角色的名称,然后根据他们的响应为他们分配该角色但是:我的管理员几乎每周都会创建和删除角色,我不能每周编辑代码来更新要分配的可用角色列表,因此我决定编写一行代码,提取用户传递的参数,然后在传递参数时查找角色名我的代码是这样的: if (command === 'role'){ let roleMember = message.member; v

我是Node.js的新手,目前正在开发一个Discord机器人。我试图让我的机器人根据用户传递的参数分配服务器角色。我知道我可以找到特定角色的名称,然后根据他们的响应为他们分配该角色

但是:我的管理员几乎每周都会创建和删除角色,我不能每周编辑代码来更新要分配的可用角色列表,因此我决定编写一行代码,提取用户传递的参数,然后在传递参数时查找角色名
我的代码是这样的:

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
读取的内容时,它给了我。当bot响应时,它将角色读取为null。这件事我已经绞尽脑汁好几个星期了,我已经一筹莫展了。有人知道我如何解决这个问题吗?编辑:以下是我的意思的一个例子:
!role top
“top”是此处的角色名称。

我认为问题在于您将角色名称与
role.toString()进行比较。使用
Role.toString()
返回一个提及,而
Role.name
只是角色的名称。
我会这样写:

if (command === 'role') {
  let roleMember = message.member;
  // I'm getting the Role object directly from the message, so that I don't need to parse it later
  var mentionedRole = message.mentions.roles.first();

  // If there was no mentioned Role, or the are any kinds of problems, exit
  if (!mentionedRole) return message.channel.send(`I'm unable to use that role: ${mentionedRole}`);

  // Add the role to the command author
  roleMember.addRole(mentionedRole).then(message.channel.send("Role successfully added.")).catch(console.error);
}
编辑:如果您只想使用角色的名称,而不想提及它,您可以这样做(假设
args[0]
是参数):


问题是
提到了.roles.first()
需要该人提及该角色,从而使用该角色ping每个人。我如何才能使它不需要提及,并且可以将参数作为参数传递。很高兴听到:)
if (command === 'role') {
  if (!args[0]) return message.reply("Please add the role name.");

  let roleMember = message.member;
  // I'm using the first argument to find the role: now it's not case-sensitive, if you want it to be case-sensitive just remove the .toLowerCase()
  var mentionedRole = message.guild.roles.find(r => r.name.toLowerCase() == args[0].toLowerCase());

  // If there was no mentioned Role, or the are any kinds of problems, exit
  if (!mentionedRole) return message.channel.send(`I'm unable to use that role: ${args[0]}`);

  // Add the role to the command author
  roleMember.addRole(mentionedRole).then(message.channel.send("Role successfully added.")).catch(console.error);
}