Dark Mistress
Dark Mistress

Reputation: 13

Prefix issue when using config file

I was trying to learn discord.js from (https://discordjs.guide) and I am struck at this issue.

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"
}

The issue is that it is not recognizing the prefix at all

If I type !ping, there is no reply and I do get reply if I type !test

Upvotes: 1

Views: 126

Answers (2)

AdityaParab
AdityaParab

Reputation: 7100

You need to use backtick instead of single quote. Backtick can be found under esc key and on left side of 1

if (message.content === `${prefix}ping`) {
  // send back "Pong." to the channel the message was sent in
  message.channel.send('Pong.');
}

Upvotes: 0

Marcos Casagrande
Marcos Casagrande

Reputation: 40404

You're using single quotes instead of back-tick, which are needed for template literals. So you're checking against: ${prefix}ping instead of !ping

It should be:

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`);

Upvotes: 1

Related Questions