简体   繁体   中英

TypeError: Cannot read properties of undefined (reading 'set') Discord.js

I'm making a Discord bot, but there was an error and i can't find a solution. I hope someone can find it: This is the Code:

EventHandler

async function loadEvents(client) {
  const { loadFiles } = require("../Functions/fileLoader");
  const ascii = require("ascii-table");
  const table = new ascii().setHeading("Event", "Status");

  await client.events.clear();

  const Files = await loadFiles("Events");

  Files.forEach((file) => {
    const event = require(file);

    const execute = (...args) => event.execute(...args, client);
    client.event.set(event.name, execute);

    if (event.rest) {
      if (event.once) client.rest.once(event.name, execute);
      else client.rest.on(event.name, execute);
    } else {
      if (event.once) client.once(event.name, execute);
      else client.on(event.name, execute);
    }

    table.addRow(event.name, "🟩");
  });

  return console.log(table.toString(), "\nLoaded Events.");
}

module.exports = { loadEvents };

fileLoader

const { glob } = require("glob");
const { promisify } = require("util");
const proGlob = promisify(glob);

async function loadFiles(dirName) {
  const Files = await proGlob(
    `${process.cwd().replace(/\\/g, "/")}/${dirName}/**/*.js`
  );
  Files.forEach((file) => delete require.cache[require.resolve(file)]);
  return Files;
}

module.exports = { loadFiles };

Can anyone find the error?

This is the Error

A quick search in the docs revealed, client.event is not a valid property, and so you are trying to call the set function on something that is not defined (in your case client.event .

But what are you trying to achieve with this line?

client.event.set(event.name, execute);

在此处输入图像描述

https://discord.js.org/#/docs/main/stable/class/Client

Typo prob. your constant is client:events: await client.events.clear()

but you set for client.event: client.event.set(event.name, execute);

you have to use either client.event or client.events and its must be declare in index.js client.event(s) = new Collection();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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