【问题标题】:Cannot read property 'first' of undefined无法读取未定义的属性“第一个”
【发布时间】:2021-07-09 21:32:45
【问题描述】:

我正在尝试为我的不和谐机器人发出静音命令,但出现错误:

Cannot read property 'first' of undefined

const BaseCommand = require('../../utils/structures/BaseCommand');
const Discord = require('discord.js');
const MuteroleCommand = require('./MuteroleCommand');
const { muterole } = require('./MuteroleCommand.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: 'In order for the command to work, a muterole must be provided. this can be done by "=muterole <role ID>"\nYou cannot mute yourself.\nYou cannot mute me.\nYou cannot mute members with a role higher than yours\nYou cannot mute members that have already been muted'}
   )
    .setFooter(client.user.tag, client.user.displayAvatarURL());
    
    const mentionedMember = message.mentions.member.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 (typeof muterole === undefined) return message.channel.send(Embedhelp);
   if (mentionedMember.user.id == client.user.id) return message.channel.send(Embedhelp);
   if (mentionedMember.roles.cache.has(muterole.id)) 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.id).catch(err => console.log(err).then(message.channel.send('There was an error while muting the member')))

  } 
}

我不确定为什么会出现这个错误,我也不确定这段代码是否还有更多错误,我非常想知道问题是什么。

【问题讨论】:

标签: javascript node.js discord.js bots


【解决方案1】:

根据Discord js Documentation,它是 message.mentions.members.first() 而不是 message.mentions.member.first()

【讨论】:

    【解决方案2】:

    问题出在这一行:

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

    您的问题似乎是 message.mentions.member 未定义

    MessageMentions 类不包含名为 member 的字段,但它确实有一个名为 members 的字段,我认为这就是您要查找的内容。

    member 替换为members,你应该很高兴! :)

    【讨论】:

    • 我做了@kess 告诉我的事情,但现在我得到一个不同的错误cannot read property 'id' of undefined
    • 是的,我们不是运行时执行者,请提供更多信息,例如触发错误的行
    猜你喜欢
    • 2020-12-17
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2020-10-30
    • 2019-10-09
    • 1970-01-01
    相关资源
    最近更新 更多