【问题标题】:Discord.js-commando how would I check if a mentioned user has a role, like a muted role for example?Discord.js-commando 我将如何检查提到的用户是否具有角色,例如静音角色?
【发布时间】:2021-05-30 14:01:50
【问题描述】:

所以我一直试图弄清楚如何在尝试添加之前检查提到的用户是否具有静音角色,如果有,就说他们已经静音,我无法弄清楚。这是我的静音命令代码,不胜感激。

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);
      });
  }
}; 

【问题讨论】:

    标签: discord.js


    【解决方案1】:

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

    member = message.mentions.first();
    if (member._roles.includes('<role ID>') {
      //code
    }
    

    显然,将&lt;role ID&gt; 替换为静音角色的角色ID。

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

    【讨论】:

    • 如果我将 替换为 mutedRole.id 是否仍然可以工作并获得名为 Muted 的角色的 id? mutedRole 在我的代码中定义
    猜你喜欢
    • 2021-11-15
    • 2021-10-25
    • 2020-12-27
    • 2021-01-02
    • 2019-12-11
    • 2020-09-21
    • 2020-12-25
    • 2021-04-06
    • 2021-02-24
    相关资源
    最近更新 更多