我是Node.js初学者。 问题在哪里,如何解决?'
当我启动命令时,在控制台中我看到错误:
if(!hp[message.author.id]){
^
ReferenceError: message is not defined
at Object.<anonymous> (C:\Users\natan11\Desktop\kurczak 1\index.js:36:8)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
index.js
const Discord = require('discord.js');
const client = new Discord.Client();
const token = 'my token';
const fs = require("fs");
client.command = new Discord.Collection();
let hp = require("./hp.json");
fs.readdir("./commands/", (err, files) => {
if(err) console.log(err);
let jsfile = files.filter(f => f.split(".").pop() === "js");
if(jsfile.length <= 0){
console.log("nie mogę znaleźć komendy.");
return;
}
jsfile.forEach((f, i) =>{
let props = require(`./commands/${f}`);
console.log(`${f} loaded`)
client.commands.set(props.help.name, props);
});
});
client.on('ready', async () => {
console.log(`${client.user.username} is online on ${bot.guild.size} servers`);
client.user.setActivity("tutorials on TSC", {type: "WATCHING"});
});
if(!hp[message.author.id]){
//this is problem
hp[message.author.id] = {
hp: 0
};
}
let hpAmt = Math.floor(Math.random() * 1) + 1;
let basepAmt = Math.floor(Math.random() * 1) + 1;
console.log(`%{hpAmt} ; ${basepAmt} ; `);
if(hpAmt === basepAmt){
hp[message.author.id] = {
hp: hp[message.author.id].hp + hpAmt
};
fs.writeFile("./hp.json", JSON.stringify(hp), (err) => {
if (err) console.log(err)
});
}
const PREFIX = '*';
client.on('message', message=>{
let args = message.content.substring(PREFIX.length).split(" ");
switch(args[0]){
case 'start':
message.channel.send('*gra_się_rozpoczeła!')
let VoiceChannel = message.guild.channels.find(channel => channel.name === "polE");
if(VoiceChannel !=null)
{
console.log(VoiceChannel.name + " kanał został znaleziony " + " channel.")
VoiceChannel.join()
}
break;
case 'stop':
message.channel.send('*gra_się_zkończyła!')
message.reply('przstraszył się przegranej uciekł i zgubił za sobą całą stawke')
if(message.guild.voiceConnection)
{
message.guild.voiceConnection.disconnect();
}
break;
case 'ping':
message.channel.send('pong!')
break;
case 'gra':
message.channel.send('gre rozpoczyna się komendą *start w tedy pojawiją się kurniki które wysyłają co 10s kure. kury da się levelować za pomocą pisania nestępujacych komend egg1,egg2,egg3... gra się kończy kto pierwszy zniszczy kurnik rywala miłej zabawy bok! ;-)')
break;
case 'baza1':
message.reply('zostałeś pseniesiony do bazy1')
var role = message.guild.roles.find(role => role.name === "baza2");
message.member.removeRole(role);
var role = message.guild.roles.find(role => role.name === "Baza1");
message.member.addRole(role);
break;
case 'baza2':
message.reply('zostałeś pseniesiony do bazy2')
var role = message.guild.roles.find(role => role.name === "Baza1");
message.member.removeRole(role);
var role = message.guild.roles.find(role => role.name === "baza2");
message.member.addRole(role);
break;
case 'koniec_baz':
message.reply('już nie przynalerzysz do żadnych baz')
var role = message.guild.roles.find(role => role.name === "baza2");
message.member.removeRole(role);
var role = message.guild.roles.find(role => role.name === "Baza1");
message.member.removeRole(role);
break;
case 'egg5':
message.channel.send('*twój_kurczak_nabił_2lvl')
break;
case 'twój_kurczak_nabił_2lvl':
var role = message.guild.roles.find(role => role.name === "1lvl");
message.member.removeRole(role);
var role = message.guild.roles.find(role => role.name === "2lvl");
message.member.addRole(role);
break;
case 'gra_się_rozpoczeła!':
var role = message.guild.roles.find(role => role.name === "1lvl");
message.member.addRole(role);
var role = message.guild.roles.find(role => role.name === "2lvl");
message.member.removeRole(role);
break;
case 'gra_się_zkończyła!':
var role = message.guild.roles.find(role => role.name === "1lvl");
message.member.addRole(role);
var role = message.guild.roles.find(role => role.name === "2lvl");
message.member.removeRole(role);
break;
}
})
client.on('message', msg=>{
if(msg.content === "*test"){
msg.reply('bot działa wszystko jest ok');
}
})
client.login(token);
我正在寻求解决的快速帮助,所以我会有所帮助,并为您提供discord.js
的版本
Version: 11.4.2 ^
这取决于填充,不用担心
xd doggo food
xd doggo food
xd doggo food
xd doggo food
xd doggo food
xd doggo food
xd doggo food
答案 0 :(得分:0)
似乎此行未正确读取文件:
let hp = require("./hp.json");
检查并确保它在您指定的位置。 require语句应该对其进行解析。
答案 1 :(得分:0)
请替换这段代码:
if(!hp[message.author.id]){
hp[message.author.id] = {
hp: 0
};
}
let hpAmt = Math.floor(Math.random() * 1) + 1;
let basepAmt = Math.floor(Math.random() * 1) + 1;
console.log(`%{hpAmt} ; ${basepAmt} ; `);
if(hpAmt === basepAmt){
hp[message.author.id] = {
hp: hp[message.author.id].hp + hpAmt
};
fs.writeFile("./hp.json", JSON.stringify(hp), (err) => {
if (err) console.log(err)
});
}
使用:
client.on("message", async message => {
if(!hp[message.author.id]){
// Not a problem anymore :)
hp[message.author.id] = {
hp: 0
};
}
let hpAmt = Math.floor(Math.random() * 1) + 1;
let basepAmt = Math.floor(Math.random() * 1) + 1;
console.log(`%{hpAmt} ; ${basepAmt} ; `);
if(hpAmt === basepAmt){
hp[message.author.id] = {
hp: hp[message.author.id].hp + hpAmt
};
fs.writeFile("./hp.json", JSON.stringify(hp), (err) => {
if (err) console.log(err)
});
}
});