开发者问题收集

TypeError:无法读取 discord.js 中未定义的属性“id”

2022-05-21
227

因此,我尝试将所有事件放在单独的文件中。诸如“ready”和“messageCreate”之类的事件运行良好,但我搞不清楚这一点。有人能帮我吗?

   const { MessageEmbed } = require("discord.js")
    
    module.exports = {
        name: "guildMemberAdd",
        run: async (member) => {
            const welcome = new MessageEmbed()
            .setDescription(`
        <:nezuko_peek:974066160108732517> — New friend just showed up: <@${member.user.id}>
        
        Welcome fellow adventurer in **Cozy corner**! What brings you in these sides? <:girl_love:973968823449436190> Here, go to <#972618593294512128> to introduce yourself and talk with us in <#971800237507248158>!
        `)
            .setImage("https://64.media.tumblr.com/01a9f72f062feaafa60cdbf80f9ba729/tumblr_inline_orgoyznIM51sd5e91_500.gif")
            .setColor("#F7DF79")
            .setFooter({ text: "Thank you for joining and enjoy your stay!" })
        
            const channel = member.guild.channels.cache.get("971800237507248158")
        
        channel.send({ content: `<@&974343947105206382>`, embeds: [welcome] })
        }
    }

这个名为“guildMemberAdd”的事件应该可以正常工作,并在成员加入服务器时自行执行。这是我的事件处理程序:

const { getFiles } = require("../util/functions")

module.exports = (bot, reload) => {
    const {client} = bot

    let events = getFiles("./events/", ".js")

    if (events.length === 0){
        console.log("No events to load")
    }

    events.forEach((f, i) => {
        if (reload)
        delete require.cache[require.resolve(`../events/${f}`)]
        const event = require(`../events/${f}`)
        client.events.set(event.name, event)

        if (!reload)
        console.log (`${i + 1}. ${f} loaded`)

    })

    if (!reload)
    initEvents(bot)
}

function triggerEventHandler(bot, event, ...args){
    const {client} = bot
    
    try {
        if (client.events.has(event))
        client.events.get(event).run(bot, ...args)
        else
        throw new Error(`Event ${event} does not exist`)
    }
    catch(err){
        console.error(err)
    }
}

function initEvents(bot) {
    const {client} = bot
    
    client.on("ready", () => {
        triggerEventHandler(bot, "ready")
    })

    client.on("messageCreate", (message) => {
        triggerEventHandler(bot, "messageCreate", message)
    })

    client.on("guildMemberAdd", (member) => {
        triggerEventHandler(bot, "guildMemberAdd", member)
    })
}

但由于某种原因,它不起作用,我收到错误: TypeError:无法读取未定义的属性“id”

1个回答

调用命令时,您将传递两个参数 bot...args ,而在导出的命令中,它仅需要 ...args 。因此,您有两个选择:要么更改传递的参数的顺序,使其如下所示: client.events.get(event).run(...args, bot) ,要么删除 bot 参数,因为您不会在命令中使用它。事件处理程序中的最终代码可能如下所示:

const { getFiles } = require("../util/functions");

module.exports = (bot, reload) => {
  const { client } = bot;

  let events = getFiles("./events/", ".js");

  if (events.length === 0) {
    console.log("No events to load");
  }

  events.forEach((f, i) => {
    if (reload) delete require.cache[require.resolve(`../events/${f}`)];
    const event = require(`../events/${f}`);
    client.events.set(event.name, event);

    if (!reload) console.log(`${i + 1}. ${f} loaded`);
  });

  if (!reload) initEvents(bot);
};

function triggerEventHandler(bot, event, ...args) {
  const { client } = bot;

  try {
    if (client.events.has(event)) client.events.get(event).run(...args);
    else throw new Error(`Event ${event} does not exist`);
  } catch (err) {
    console.error(err);
  }
}

function initEvents(bot) {
  const { client } = bot;

  client.on("ready", () => {
    triggerEventHandler(bot, "ready");
  });

  client.on("messageCreate", (message) => {
    triggerEventHandler(bot, "messageCreate", message);
  });

  client.on("guildMemberAdd", (member) => {
    triggerEventHandler(bot, "guildMemberAdd", member);
  });
}

Caladan
2022-05-21