简体   繁体   English

类型错误:无法读取未定义的属性“first”

[英]TypeError: Cannot read property 'first' of undefined

client.on('message', (message) => {
 const { member, mentions } = message;

 const tag = `<@${member.id}>`;

 if (message.content.startsWith(`${prefix}ban`)) {
  if (
   member.hasPermission('ADMINISTRATOR') ||
   member.hasPermission('BAN_MEMBERS')
  ) {
   const target = mentions.user.first();
   if (target) {
    const targetMember = message.guild.members.cache.get(target.id);
    targetMember.ban();
    message.channel.send(
     `${tag} Cet.te ancien.ne membre a été bannii avec succès !`
    );
   } else {
    message.channel.send(`${tag} Merci de spécifier le membre à bannir.`);
   }
  } else {
   message.channel.send(
    `${tag} Tu n'as pas la permission d'effectuer cette commande.`
   );
  }
 }
});

I'm not sure what the error in my code is, any help is appreciated!我不确定我的代码中的错误是什么,任何帮助表示赞赏! I would also take any suggestions on how to improve this code in general.我还会就如何改进此代码提出任何建议。

user is not a property of mentions . user不是mentions的属性。 Instead use users :而是使用users

mentions.users.first();

MessageMentions docs MessageMentions文档


Also, as a bit of a suggestion, instead of getting the mention as a User object, then turning it into a GuildMember object, you could just skip straight to the GuildMember object.另外,作为一个建议,您可以直接跳到GuildMember对象,而不是将提及作为User对象,然后将其转换为GuildMember对象。

// instead of:
const target = mentions.users.first();

// use:
const target = mentions.members.first();

mentions.users returns a collection of every user that was mentioned in that message. mentions.users返回该消息中提到的每个用户的集合。 You are taking the first User object (first mention).您正在获取第一个User对象(第一次提及)。

You are then turning that User object into a GuildMember object using the Guild.member() function :然后使用Guild.member()函数将该User对象转换为GuildMember对象:

message.guild.members.cache.get(target.id);

However , message.members returns a collection of every GuildMember that was mentioned, eliminating the need to convert a User object to a GuildMember later in your code, as it would already be a GuildMember .但是message.members返回提到的每个GuildMember的集合,无需稍后在代码GuildMember User对象转换为GuildMember ,因为它已经是GuildMember

It's actually mentions.user s .first() .它实际上是提到.user s .first() See docs .请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (节点:2724)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“first” - (node:2724) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'first' of undefined 渲染错误:“TypeError:无法读取未定义的属性‘first_name’” - Error in render: "TypeError: Cannot read property 'first_name' of undefined" 节点JS | TypeError:无法读取未定义的属性“ first_name” - Node JS | TypeError: Cannot read property 'first_name' of undefined 错误类型错误:无法读取未定义的属性“first_name” - ERROR TypeError: Cannot read property 'first_name' of undefined 先拼接 object 返回 TypeError: Cannot read property of undefined - Splicing first object returns TypeError: Cannot read property of undefined NodeJS :: TypeError:无法读取未定义的属性“first_name” - NodeJS :: TypeError: Cannot read property 'first_name' of undefined 类型错误:无法读取未定义的 discord js 的属性“first” - TypeError: Cannot read property 'first' of undefined discord js jQuery未捕获的TypeError无法读取首页上未定义的属性“ left” - jquery uncaught TypeError cannot read property 'left' of undefined on first page TypeError:无法读取未定义的属性… - TypeError: Cannot read property … of undefined TypeError:无法读取未定义的属性 - TypeError: Cannot read property of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM