当我尝试加载我的JavaScript脚本时,会出现此错误(也是针对不和谐的机器人)
ReferenceError: Cannot access 'Discord' before initialization
at Object.<anonymous> (C:\Users\33614\Desktop\spambot.js\index.js:1:16)
[90m at Module._compile (internal/modules/cjs/loader.js:1156:30)[39m
[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)[39m
[90m at Module.load (internal/modules/cjs/loader.js:1000:32)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:899:14)[39m
[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)[39m
[90m at internal/main/run_main_module.js:18:47[39m
也是我的代码
const client = Discord.client();
const Discord = require("discord.js");
var prefix = '!'
var token = 'NzM1NjI4OTEzMzY3MDU2NDA0.XxnRyQ.X2H-FP1DSRq97DoyRtYnEpqmI5s'
client.on("ready", function() {
console.log("ready")
})
client.on("message", message => {
})
client.login(token);
答案 0 :(得分:2)
您的前两行看起来应该切换一下-先“导入”,然后使用。
const Discord = require("discord.js");
const client = Discord.Client();
根据文档:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('Pong!');
}
});
client.login('token');