Discord.js 错误提供的角色不是角色、雪花或数组或角色或雪花的集合

2022-01-10 00:00:00 discord bots javascript discord.js

我正在为我的机器人创建一个静音命令,如果还没有一个静音角色,它会创建一个静音角色并将其提供给命令中提到的用户,目前我得到的错误是;

I'm making a mute command for my bot where it creates a muted role if there isn't one already and gives it to the user that is mentioned in the command, currently the error im getting is;

[INVALID_TYPE]:提供的角色不是角色、雪花或数组或角色或雪花的集合.

我最好的猜测是,这个错误的发生是因为它没有创建它应该创建的角色,它不能把它给提到的成员,不过我可能完全错了.

My best guess is that this error occurs because it doesn't create the role that it is supposed to create therefor it cannot give it to the mentioned member, i may be completely wrong though.

const BaseCommand = require('../../utils/structures/BaseCommand');
const Discord = require('discord.js');

module.exports = class MuteCommand extends BaseCommand {
  constructor() {
    super('mute', 'moderation', []);
  }

  async run(client, message, args) {
    if(!message.member.hasPermission("MUTE_MEMBERS")) return message.channel.send("You do not have Permission to use this command.");
    if(!message.guild.me.hasPermission("MUTE_MEMBERS")) return message.channel.send("I do not have Permissions to mute members.");
    const Embedhelp = new Discord.MessageEmbed()
    .setTitle('Mute Command')
    .setColor('#6DCE75')
    .setDescription('Use this command to Mute a member so that they cannot chat in text channels nor speak in voice channels')
    .addFields(
      { name: '**Usage:**', value: '=mute (user) (time) (reason)'},
      { name: '**Example:**', value: '=mute @Michael stfu'},
      { name: '**Info**', value: 'You cannot mute yourself.
You cannot mute me.
You cannot mute members with a role higher than yours
You cannot mute members that have already been muted'}
   )
    .setFooter(client.user.tag, client.user.displayAvatarURL());

    let role = 'muted' || 'Muted';
    let newrole = message.guild.roles.cache.find(x => x.name === role);
    if (typeof newrole === undefined) {
      message.guild.roles.create({
        data: {
          name: 'Muted',
          color: '#ff0000',
          permissions: {
              SEND_MESSAGES: false,
              ADD_REACTIONS: false,
              SPEAK: false
          }
        },
        reason: 'to mute people',
      })
      .catch(console.log(err)); {
        message.channel.send('Could not create muted role');
      };
    };
    let muterole = message.guild.roles.cache.find(x => x.name === role);

    const mentionedMember = message.mentions.members.first() || await message.guild.members.fetch(args[0]);
    let reason = args.slice(1).join(" ");
    const banEmbed = new Discord.MessageEmbed()
     .setTitle('You have been Muted in '+message.guild.name)
     .setDescription('Reason for Mute: '+reason)
     .setColor('#6DCE75')
     .setTimestamp()
     .setFooter(client.user.tag, client.user.displayAvatarURL());

   if (!reason) reason = 'No reason provided';
   if (!args[0]) return message.channel.send(Embedhelp);
   if (!mentionedMember) return message.channel.send(Embedhelp);
   if (!mentionedMember.bannable) return message.channel.send(Embedhelp);
   if (mentionedMember.user.id == message.author.id) return message.channel.send(Embedhelp);
   if (mentionedMember.user.id == client.user.id) return message.channel.send(Embedhelp);
   if (mentionedMember.roles.cache.has(muterole)) return message.channel.send(Embedhelp);
   if (message.member.roles.highest.position <= mentionedMember.roles.highest.position) return message.channel.send(Embedhelp);

   await mentionedMember.send(banEmbed).catch(err => console.log(err));
   await mentionedMember.roles
  .add(muterole)
  .then(() => message.channel.send("There was an error while muting the member"))
  .catch((err) => console.log(err));

  } 
}

创建角色的代码行是:

message.guild.roles.create({
        data: {
          name: 'Muted',
          color: '#ff0000',
          permissions: {
              SEND_MESSAGES: false,
              ADD_REACTIONS: false,
              SPEAK: false
          }
        },
        reason: 'to mute people',
      })

推荐答案

您可以使用 async/await 或 .then,但我建议使用 async/await.例如:

You can use either async/await or .then, but i recommend using async/await. For example:


const mentionedMember = message.mentions.members.first() || await 
message.guild.members.fetch(args[0]);

const newMutedRole = await message.guild.roles.create({
        data: {
          name: 'Muted',
          color: '#ff0000',
          permissions: {
              SEND_MESSAGES: false,
              ADD_REACTIONS: false,
              SPEAK: false
          }
        },
        reason: 'to mute people',
      })


mendtionedMember.roles.add(newMutedRole)

相关文章