首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Discord.js-commando我该如何检查提到的用户是否有角色,例如静音角色?

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

Stack Overflow用户
提问于 2021-03-01 09:07:58
回答 1查看 39关注 0票数 0

因此,我一直在尝试弄清楚如何在尝试添加之前检查提到的用户是否具有静音角色,如果他们有,就说他们已经是静音角色,但我无法弄清楚。这是我的静音命令代码,任何帮助都是非常感谢的。

代码语言:javascript
复制
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);
      });
  }
}; 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-01 09:41:28

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

代码语言:javascript
复制
member = message.mentions.first();
if (member._roles.includes('<role ID>') {
  //code
}

显然,将<role ID>替换为静音角色的角色id。

这之所以有效,是因为members对象有一个_roles数组,其中包含它们所拥有的角色的所有ID。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66415471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档