繁体   English   中英

TypeError:无法读取未定义的属性“公会”我正在编写欢迎代码,但我不断收到该错误

[英]TypeError: Cannot read property 'guild' of undefined I'm writing a welcome code but I keep getting that error

const db = require('quick.db')
const Discord = require('discord.js')

module.exports = (client) => {
  
  
    client.on('guildMemberAdd', (member, message) => {
     
        let welcomechannel = db.get(`welcome_${message.guild.id}`)
        let welcomemsg = db.get(`Welcomemsg_${message.guild.id}`)
  
       if(welcomechannel = null || undefined) return
   
        if(welcomemsg = null || undefined) welcomemsg = `Hey <@${member.id}>! Make sure to read the rules and have a great time!`

        let embed = new Discord.MessageEmbed()
        .setColor('GOLD')
        .setDescription(welcomemsg)


        welcomechannel.send(embed)
 

    })
  }

guildMemberAdd事件仅使用member参数发出。 您必须从member object 访问guild属性。

let welcomechannel = db.get(`welcome_${member.guild.id}`)
let welcomemsg = db.get(`Welcomemsg_${member.guild.id}`)

您的代码中还有几个问题。 =是 JS 中的赋值运算符。 您想使用===== (严格相等)进行比较。 而且您检查多个值的条件不正确。

if (!welcomechannel === null || welcomechannel === undefined) {
  return
}

或更好

if(!welcomechannel) {
  return
}

同样,您可以使用以下方法修复welcomemsg条件。

if(!welcomemsg) {
  welcomemsg = `Hey <@${member.id}>! Make sure to read the rules and have a great time!`
}

或更好

const DEFAULT_MESSAGE = `Hey <@${member.id}>! Make sure to read the rules and have a great time!`
const welcomemsg = db.get(`Welcomemsg_${guildId}`) || DEFAULT_MESSAGE

这是固定代码。

client.on('guildMemberAdd', (member) => {
  const { id: guildId } = member.guild
  const welcomechannel = db.get(`welcome_${guildId}`)
  const DEFAULT_MESSAGE = `Hey <@${member.id}>! Make sure to read the rules and have a great time!`
  const welcomemsg = db.get(`Welcomemsg_${guildId}`) || DEFAULT_MESSAGE

  if (!welcomechannel) {
    return
  }

  // rest of the code
})

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM