如何修复 TypeError:无法读取 undefined

时间:2021-04-15 18:06:55

标签: javascript

这是我的部分问题

const {Discord,Client, Attachment, MessageAttachment, DiscordAPIError} = require('discord.js');

const client = new Discord.Client();

const token = 'NzIzNDgxNjczNDA0MjUyMjU5.XuyQ0A.eDXJl2ZdBzQRcIZJrQRHR9qO6sE';

const PREFIX = '/'; 

const fs = require('fs');

client.command = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));

for(const file of commandFiles){
  const command = require(`./commands/${file}`);

  client.command.set(command.name, command);
}

client.on('ready', ()=>{
    console.log('this bot is online!')
    client.user.setActivity('Music in another wolrd',{type:'LISTENING'}).catch(console.error);

})

和我的终端显示这个

const client = new Discord.Client();
                           ^

TypeError: Cannot read property 'Client' of undefined
    at Object.<anonymous> (C:\Users\User\Desktop\discordbot\index.js:3:28)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47    

我已经安装了 npm 但是还是不行我该怎么办 '''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''' ''''''

2 个答案:

答案 0 :(得分:0)

您必须像 const Discord = require('discord.js');

一样导入它

因为您已经在解构它,所以您可以使用 like const client = new Client();

const Discord = require('discord.js');
const fs = require('fs');

const client = new Discord.Client();
const token = 'NzIzNDgxNjczNDA0MjUyMjU5.XuyQ0A.eDXJl2ZdBzQRcIZJrQRHR9qO6sE';
const PREFIX = '/'; 

client.command = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));

for(const file of commandFiles){
  const command = require(`./commands/${file}`);

  client.command.set(command.name, command);
}

client.on('ready', ()=>{
    console.log('this bot is online!')
    client.user.setActivity('Music in another wolrd',{type:'LISTENING'}).catch(console.error);

})

答案 1 :(得分:0)

Discord.js 不会导出 discord 类,因此您无法从 Discord.js 中销毁它