discord.js v12静音命令

时间:2020-10-05 14:02:52

标签: javascript node.js discord discord.js

我在discord.js v12中做了一个静音命令。每当我运行命令时,都会出现错误。我对编码有点陌生,因此我无法理解运行命令时收到错误的原因。这是我的代码:

module.exports = {
    name: "mute",
    description: "Mutesd a member for a given time",

    async run (client, message, args) {

    const muteRoleId = message.guild.roles.cache.get('762199219074367508')
    let muteRole;

    const member = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
    if (!member) 
      return message.channel.send('Please mention a user or provide a valid user ID');
    if (member === message.member)
      return message.channel.send('You cannot mute yourself');
    if (member === message.guild.me) return message.channel.send(message, 0, 'You cannot mute me');
    if (member.roles.highest.position >= message.member.roles.highest.position)
      return message.channel.send('You cannot mute someone with an equal or higher role');
    if (!args[1])
      return message.channel.send('Please enter a length of time of 14 days or less (1s/m/h/d)');
    let time = ms(args[1]);
    if (!time || time > 1209600000) // Cap at 14 days, larger than 24.8 days causes integer overflow
      return message.channel.send('Please enter a length of time of 14 days or less (1s/m/h/d)');

    let reason = args.slice(2).join(' ');
    if (!reason) reason = '`None Provided`';
    if (reason.length > 1024) reason = reason.slice(0, 1021) + '...';

    if (member.roles.cache.has(muteRoleId))
      return message.channel.send('Provided member is already muted');

    // Mute member
    try {
      await member.roles.add(muteRole);
    } catch (err) {
      console.log(err)
      return message.channel.send('Please check the role hierarchy', err.message);
    }
    const muteEmbed = new MessageEmbed()
      .setTitle('Mute Member')
      .setDescription(`${member} has now been muted for **${ms(time, { long: true })}**.`)
      .addField('Moderator', message.member, true)
      .addField('Member', member, true)
      .addField('Time', `\`${ms(time)}\``, true)
      .addField('Reason', reason)
      .setFooter(message.member.displayName,  message.author.displayAvatarURL({ dynamic: true }))
      .setTimestamp()
      .setColor(message.guild.me.displayHexColor);
    message.channel.send(muteEmbed);

    // Unmute member
    member.timeout = message.client.setTimeout(async () => {
      try {
        await member.roles.remove(muteRole);
        const unmuteEmbed = new MessageEmbed()
          .setTitle('Unmute Member')
          .setDescription(`${member} has been unmuted.`)
          .setTimestamp()
          .setColor(message.guild.me.displayHexColor);
        message.channel.send(unmuteEmbed);
      } catch (err) {
        console.log(err)
        return message.channel.send('Please check the role hierarchy', err.message);
      }
    }, time);
  }
};

如前所述,每当我运行此命令时,都会出现以下错误:

TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes.
    at GuildMemberRoleManager.add (/home/runner/Utki-the-bot/node_modules/discord.js/src/managers/GuildMemberRoleManager.js:93:15)
    at Object.run (/home/runner/Utki-the-bot/commands/mute.js:37:26)
    at Client.<anonymous> (/home/runner/Utki-the-bot/index.js:71:42)
    at Client.emit (events.js:315:20)
    at Client.EventEmitter.emit (domain.js:483:12)
    at MessageCreateAction.handle (/home/runner/Utki-the-bot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/home/runner/Utki-the-bot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/runner/Utki-the-bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/home/runner/Utki-the-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/home/runner/Utki-the-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10) {
  [Symbol(code)]: 'INVALID_TYPE'
}

我无法解决该错误。您能帮我解决这个错误吗?预先感谢。

2 个答案:

答案 0 :(得分:0)

这是一个非常简单的错误和修复。该错误表明所提供的角色对方法member.roles.add(muteRole);无效。因此,这告诉我们muteRole不是有效的Role对象,在查看您的代码时,我们看到这是正确的。

在代码的开头,您将拥有以下内容:

const muteRoleId = message.guild.roles.cache.get('762199219074367508')
let muteRole;

您定义了两个变量,您设置了muteRoleId的值,但没有设置muteRole的值。然后,无需更改这些值,您就拥有member.roles.add(muteRole);,但是muteRole将解析为undefined,因为它从未设置。

要解决此问题,只需将member.roles.add(muteRole);更改为member.roles.add(muteRoleId);,它就应该可以正常工作。

答案 1 :(得分:0)

TypeError [INVALID_TYPE]:提供的角色不是角色、雪花或数组或角色或雪花的集合。将其更改为此 let MutedRole = message.guild.roles.cache.find(role => role.name === 'Muted');