Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.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
Discord.js-commando我如何检查所提到的用户是否有角色,例如静音角色?_Discord.js - Fatal编程技术网

Discord.js-commando我如何检查所提到的用户是否有角色,例如静音角色?

Discord.js-commando我如何检查所提到的用户是否有角色,例如静音角色?,discord.js,Discord.js,因此,我一直在试图找出如何在尝试添加之前检查所提到的用户是否拥有静音角色,如果他们拥有,则表示他们已经静音,我无法找出答案。这是我的静音命令代码,非常感谢您的帮助 const { Command } = require('discord.js-commando'); const { MessageEmbed } = require('discord.js'); module.exports = class MuteCommand extends Command { constructor(

因此,我一直在试图找出如何在尝试添加之前检查所提到的用户是否拥有静音角色,如果他们拥有,则表示他们已经静音,我无法找出答案。这是我的静音命令代码,非常感谢您的帮助

const { Command } = require('discord.js-commando');
const { MessageEmbed } = require('discord.js');

module.exports = class MuteCommand extends Command {
  constructor(client) {
    super(client, {
      name: 'mute',
      aliases: ['mute-user'],
      memberName: 'mute',
      group: 'guild',
      description:
        'Mutes a tagged user (if you have already created a Muted role)',
      guildOnly: true,
      userPermissions: ['MANAGE_ROLES'],
      clientPermissions: ['MANAGE_ROLES'],
      args: [
        {
          key: 'userToMute',
          prompt: 'Please mention the member that you want to mute them.',
          type: 'member'
        },
        {
          key: 'reason',
          prompt: 'Why do you want to mute this user?',
          type: 'string',
          default: message =>
            `${message.author.tag} Requested, no reason was given`
        }
      ]
    });
  }

  run(message, { userToMute, reason }) {
    const mutedRole = message.guild.roles.cache.find(
      role => role.name === 'Muted'
    );
    if (!mutedRole)
      return message.channel.send(
        ':x: No "Muted" role found, create one and try again.'
      );
    const user = userToMute;
    if (!user)
      return message.channel.send(':x: Please try again with a valid user.');
    user.roles
      .add(mutedRole)
      .then(() => {
        const muteEmbed = new MessageEmbed()
          .addField('Muted:', user)
          .addField('Reason', reason)
          .setColor('#420626');
        message.channel.send(muteEmbed);
      })
      .catch(err => {
        message.reply(
          ':x: Something went wrong when trying to mute this user.'
        );
        return console.error(err);
      });
  }
}; 

要查看提到的用户是否具有角色,您可以执行以下操作:

member = message.mentions.first();
if (member._roles.includes('<role ID>') {
  //code
}
member=message.indications.first();
if(成员角色包括(“”){
//代码
}
显然,将
替换为静音角色的角色id


这是因为
成员
对象有一个
\u roles
数组,该数组包含他们所拥有的角色的所有id。

如果我用mutedRole.id替换,它是否仍然有效并获得名为Muted的角色的id?mutedRole在我的代码中定义是的,可以!好的,谢谢您的帮助!