我试图从(https://discordjs.guide)学习discord.js,我对此问题感到震惊。
Index.js
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
client.on('ready', () => {
console.log('Ready!');
});
client.on('message', message => {
if (message.content === '${prefix}ping') {
// send back "Pong." to the channel the message was sent in
message.channel.send('Pong.');
}
if (message.content === '!test') {
// send back "Pong." to the channel the message was sent in
message.channel.send('Test not found');
}
});
client.login(token);
Config.json
{
"prefix": "!",
"token": "Token"
}
问题在于它无法识别所有
的前缀如果我输入!ping
,则无法回复,如果我输入!test
答案 0 :(得分:1)
您使用的是单引号而不是后退,这是template literals所需的。因此,您要检查:${prefix}ping
而不是!ping
应该是:
if (message.content === `${prefix}ping`) {
// send back "Pong." to the channel the message was sent in
message.channel.send('Pong.');
}
const prefix = '!';
console.log('${prefix}ping'); // What you have
console.log(`${prefix}ping`);
答案 1 :(得分:0)
您需要使用backtick
而不是单引号。可以在esc键和1
if (message.content === `${prefix}ping`) {
// send back "Pong." to the channel the message was sent in
message.channel.send('Pong.');
}