Discord bot setRole命令只能随机运行

时间:2020-01-25 21:40:53

标签: node.js bots discord discord.js

我最近用简单的算法制作了一个机器人。该机器人应该在某个频道上欢迎您,然后您必须告诉他您的名字级别(在特定游戏中)和您的团队(team1 team2或team3)。当您这样做时,他应该以您的昵称和级别为您命名,并在您的团队之后设置您的不和谐角色,最后,在他这样做之后,他要您在pm上获取您的个人资料的屏幕快照,然后将其发送到另一个频道(这不是重要的部分)。

命名部分和屏幕截图功能正常运行,并且按预期运行,尽管出于某些未知原因,setRole函数看似随机运行。它有时赋予成员正确的角色,有时却没有。这是我的算法:

if (message.channel.id == welcomeid){
  var words = message.content.split(' ');
  var name = words[0];
  var number = words[1];
  var team = words[2];
  if (typeof words[0] === "undefined" || typeof words[1] === "undefined" || typeof words[2] === "undefined"){
    message.reply("error checker")
    return;
  }
  team = team.toLowerCase();

    if (name.includes('team1') == 1 || name.includes('team2') == 1 || name.includes('team3') == 1 || name.includes('team4') == 1   ){
      message.reply("Another error");
      return;
    }
    else if (name.length < 3){
      message.reply("error for name length");
      return;
    }
    else if (isNaN(number)){
      message.reply("error for checking if level is a number");
      return;
    }
    else if (number > 40 || number < 1 ) {
      message.reply("error for checking if level is above one and below 40");
      return;
    }
    else {
      if (team.includes("team1")){ //those 4 ifs are the problematic part
        message.member.setNickname(name+" | "+number)
        .then(console.log)
        .catch(console.error);
      message.member.addRole(team1id)
        .then(console.log)
        .catch(console.error);
      screenShot();
    }
    else if (team.includes("team2")){
      message.member.setNickname(name+" | "+number)
      .then(console.log)
      .catch(console.error);
      message.member.addRole(team2id)
       .then(console.log)
       .catch(console.error);
      screenShot();
    }
    else if (team.includes("team3")){
      message.member.setNickname(name+" | "+number)
      .then(console.log)
      .catch(console.error);
      message.member.addRole(team3id)
        .then(console.log)
        .catch(console.error);
      screenShot();
    }
    else if (team.includes("team4")){
      message.member.setNickname(name+" | "+number)
      .then(console.log)
      .catch(console.error);
      message.member.addRole(team4id)
        .then(console.log)
        .catch(console.error);
      screenShot();
    }
    else {
      message.reply("team error");
      return;
    }
  }
}

当然,我不需要机器人在需要的时候就扮演角色,并且我看不到任何与“ addrole”功能相抵触的代码。

1 个答案:

答案 0 :(得分:1)

您的代码中没有可见的错误,我唯一要摆脱的是不必要的else块。因为如果任何一个参数输入不正确,则将函数保留为return。检查您的漫游器角色是否高于所有参与者的角色。如果没有,他将无法更改昵称或放弃这个角色。

  if (message.channel.id == welcomeid){
    var words = message.content.split(' ');
    var name = words[0];
    var number = words[1];
    var team = words[2];
    if (typeof words[0] === "undefined" || typeof words[1] === "undefined" || typeof words[2] === "undefined"){
        return message.reply("error checker")
    }

    team = team.toLowerCase();

    if (name.includes('team1') == 1 || name.includes('team2') == 1 || name.includes('team3') == 1 || name.includes('team4') == 1   ){
      message.reply("Another error");
      return;
    } else if (name.length < 3){
      message.reply("error for name length");
      return;
    } else if (isNaN(number)){
      message.reply("error for checking if level is a number");
      return;
    } else if (number > 40 || number < 1 ) {
      message.reply("error for checking if level is above one and below 40");
      return;
    }

  if (team.includes("team1")){
        message.member.setNickname(name+" | "+number)
        .then(console.log)
        .catch(console.error);
      message.member.addRole(team1id)
        .then(console.log)
        .catch(console.error);
      screenShot();
    } else if (team.includes("team2")){
      message.member.setNickname(name+" | "+number)
          .then(console.log)
          .catch(console.error);
      message.member.addRole(team2id)
       .then(console.log)
       .catch(console.error);
      screenShot();
    } else if (team.includes("team3")){
      message.member.setNickname(name+" | "+number)
      .then(console.log)
      .catch(console.error);
      message.member.addRole(team3id)
        .then(console.log)
        .catch(console.error);
      screenShot();
    } else if (team.includes("team4")){
      message.member.setNickname(name+" | "+number)
      .then(console.log)
      .catch(console.error);
      message.member.addRole(team4id)
        .then(console.log)
        .catch(console.error);
      screenShot();
    } else {
      message.reply("team error");
      return;
    }
}