使用配置文件时的前缀问题

时间:2018-05-14 04:21:16

标签: javascript node.js discord.js

我试图从(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

,我会收到回复

2 个答案:

答案 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.');
}