Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

javascript - 如何检查某人是否有特定角色(How to check if someone has a certain role)

Ok, I know that there are similar questions out there but no matter what I try I get the same result.

(好的,我知道那里也有类似的问题,但是不管我尝试什么,我都会得到相同的结果。)

I have been trying for 2 days now to figure out how to check if I had the "OverBot-Admin" role for a ban command.

(我已经尝试了2天,以弄清楚如何检查我是否具有ban命令的“ OverBot-Admin”角色。)

But whenever I run the command it spams the chat!

(但是,每当我运行命令时,它就会向聊天室发送垃圾邮件!)

Here is my code:

(这是我的代码:)

      if (command === "ban") {
    if(message.member.roles.has(role => role.name === "OverBot-Admin")) {
      let reason = args.slice(1).join(" ");
      if (!reason) reason = "No reason provided";
      const user = message.mentions.users.first();

      if (user) {
        const member = message.guild.member(user);

        if (member) {
          member
            .ban({ reason: "They were being bad." })
            .then(() => {
              message.reply(
                ":white_check_mark: Successfully banned " +
                  message.user.id +
                  "for " +
                  reason
              );

              const embed = new Discord.RichEmbed()
                .setTitle("Ban")
                .addField("Member:", message.user.id)
                .addField("Reason:", reason)
                .setTimestamp()
                .setFooter(
                  "Created by OverThrow, OverBot SE",
                  "https://cdn.discordapp.com/avatars/649431468254429224/0b63291c1e7342c2320ca5c3cce806ca.png?size=2048"
                );
            })
            .catch(err => {
              message.reply(":x: I was unable to ban that member!");

              console.error(err);
            });
        } else {
          message.reply(":x: That user isn't a member to this guild!");
        }
      } else {
        message.reply(":x: You didn't mention a member to ban!");
      }
    }
  } else {
    message.reply(":x: I couldn't ban this user, please make sure you have the OverBot-Admin role!")
  }
  ask by OverThrow translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

message.member.roles.has() requires a role ID as an argument, whereas here, you are passing in a function.

(message.member.roles.has()需要角色ID作为参数,而在这里,您要传入一个函数。)

The method I think you are looking for is Collection.exists (although this method is deprecated, if anyone can figure out a different way to go about this, please let me know).

(我认为您正在寻找的方法是Collection.exists (尽管不建议使用此方法,但是如果有人可以找到其他解决方法,请告诉我)。)

Looking at the message.reply in the then function after banning, I see a message.user.id .

(望着message.replythen取缔后的功能,我看到一个message.user.id 。)

However, message.user does not seem to be a valid property of the message object according to the docs.

(但是,根据文档, message.user似乎不是消息对象的有效属性。)

I assume that you would be referring to the user who was just banned, in which case you can reuse the user or member variable you previously defined.

(我假设您是指刚刚被禁止的用户,在这种情况下,您可以重用先前定义的usermember变量。)

If you have further problems, feel free to comment back.

(如果您还有其他问题,请随时发表评论。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...