Javascript Getting ReferenceError:(令牌)未定义
最近,我一直在开发一个叫做音乐机器人的机器人。并且,它显示了以下错误消息:Javascript Getting ReferenceError:(令牌)未定义,javascript,discord,discord.js,Javascript,Discord,Discord.js,最近,我一直在开发一个叫做音乐机器人的机器人。并且,它显示了以下错误消息: C:\Users\yuhao\Desktop\discord-bot-master\index.js:51 client.login(token); ^ ReferenceError: token is not defined at Object.<anonymous> (C:\Users\yuhao\Desktop\discord-bot-master\index
C:\Users\yuhao\Desktop\discord-bot-master\index.js:51
client.login(token);
^
ReferenceError: token is not defined
at Object.<anonymous> (C:\Users\yuhao\Desktop\discord-bot-master\index.js:51:14)
at Module._compile (internal/modules/cjs/loader.js:1144:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1164:10)
at Module.load (internal/modules/cjs/loader.js:993:32)
at Function.Module._load (internal/modules/cjs/loader.js:892:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
C:\Users\yuhao\Desktop\discord-bot-master>
这是config.json:
{
"prefix": "!",
"token": "token"
}
就这些。请在回复部分帮助我,谢谢 像这样使用
const fs = require('fs')
const Discord = require('discord.js');
const Client = require('./client/Client');
const config = require('./config.json');
const client = new Client();
client.commands = new Discord.Collection();
const queue = new Map();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
console.log(client.commands);
client.once('ready', () => {
console.log('Ready!');
});
client.once('reconnecting', () => {
console.log('Reconnecting!');
});
client.once('disconnect', () => {
console.log('Disconnect!');
});
client.on('message', async message => {
const args = message.content.slice(config.prefix.length).split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName);
if (message.author.bot) return;
if (!message.content.startsWith(config.prefix)) return;
try {
command.execute(message);
} catch (error) {
console.error(error);
message.reply('There was an error trying to execute that command!');
}
});
client.login(config.token);
您的问题是尚未填充令牌或前缀。您需要像这样解析JSON,以便填充相关对象 JS: TS(使用一个类,这样您就不必担心键入错误的值,因为您将获得自动完成):
将来,Java和JavaScript是两个完全不同的东西。如果您指的是JavaScript,请不要使用Java标记。请不要只转储代码,描述您更改了什么以及更改的原因。嗨,client.login(config.token);,需要配置吗?这是将json文件导入项目的正确方法。您不能按名称导入属性,因此您将配置文件作为
config
导入,然后使用他的属性,如config.property
const fs = require('fs')
const Discord = require('discord.js');
const Client = require('./client/Client');
const config = require('./config.json');
const client = new Client();
client.commands = new Discord.Collection();
const queue = new Map();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
console.log(client.commands);
client.once('ready', () => {
console.log('Ready!');
});
client.once('reconnecting', () => {
console.log('Reconnecting!');
});
client.once('disconnect', () => {
console.log('Disconnect!');
});
client.on('message', async message => {
const args = message.content.slice(config.prefix.length).split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName);
if (message.author.bot) return;
if (!message.content.startsWith(config.prefix)) return;
try {
command.execute(message);
} catch (error) {
console.error(error);
message.reply('There was an error trying to execute that command!');
}
});
client.login(config.token);
const fs = require('fs')
...
const config = JSON.parse(fs.readFileSync(filename, "UTF-8");
client.login(config.token);
export default class Settings {
public token: String;
public prefix: String;
}
...
const config: Settings = JSON.parse(fs.readFileSync(filename, "UTF-8");
client.login(config.token);