I am new to JavaScript having syntax problems please help me here is my code

const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = '-';

const fs = require('fs');

client.commands = new Discord.Collection();

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

}

client.once('ready',() => {
    console.log('Walker is Online!');
});

client.on('message', message=>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'ping'){
        client.commands.get('ping').execute(message, args);
    } else if (command == 'youtube'){
        message.channel.send('https://www.youtube.com/codelyon');
    } else if (command == 'Strawberry'){
        message.channel.send('Lite Blue Butterfly');
    } else if (command == ('Blue Butterfly')
        message.command.send('Sugie StrawBerry');
    }

}

client.login('NzgwNTA1ODM1NDk0MTc4ODE2.X7wEtA.fy9ssTMv4P7LJFZ26MXGFyNrSGs');

You’re going to have to be more specific. I don’t see any syntax errors off hand. The only thing is that I’d want to use a switch statement instead of that if/else chain, but that’s not an error.

I guess I just noticed this:

const command = require('./commands/${file}');

Those look like regular single quotes to me instead of back ticks, like you’d want for a template literal.

2 Likes

Hello Kevin, thanks so very much for responding to my question. I am very sorry that I am just now able to get back to this issue. I have been dealing with trying to avoid this COVID Virus. Please accept my apology.

Kevin, what I am attempting to do is to create a Discord Bot for trading on the stock market. I am a retired 60 yr old IT guy with Type1 Diabetes and Prostate Cancer. I am not a developer but I’ve always attempted to code throughout my over 40 yr IT career. So I’m new to all of this, Anyway It appears to me that not all of my code is executing.

I followed the Youtube Video “Codelyon Code your own Discord Bot Basics (2020)”
which is a series of three videos on YouTube.

Kevin, im using a Mac running Big Sur 11.0.1
I downloaded and installed Node.js Discord.js
then I ran the
NPM INIT to create my json package in the default folder
then I used the Discord Interface to create my Client Id and Token
I was able to set the permissions and get the bot to come online.
So I have been attempting to get my code to run on but it only seems to recognize the first two commands in my if else statements
the bot will respond to the -ping command and respond with a pong
the -youtube command will execute
but when I enter the -Strawberry Command I get no response
as well as the -Blue Butterfly command I get no response as well

I’m using Visual Studio Code to edit my JavaScript Code
I renamed the default index.js to main.js as directed in the video

but when I compile the code I get no errors. So it appears to be something in my
if else statements
so your suggestion about using the Switch approch is appealing but im not sure how to do that

Kevin this is my second bot that I have created My Discord Servers name is Narrow Path Walkers
and the first bot has a name of NPWStockBot but it has a error in the code as well that has something to do with the $Path variable not showing a path to the appropriate folder so it says it can’t find a file folder
So I created TraderBotBob to see if I could simplify the code so that I could control the bot and communicate with it.
My plan is to use the TD Ameritrade API to access real time data from the market to automate my proprietary trading system

Kevin, I’m going to send you the original code for the NPWStockBot and then my attempts to simplify the code with TraderBotBob

I will check the reverse ticks in the code now.

Thank you so much for your time I hope this is the type of detail that you need to help me. I would like to just engage you throughout this entire process if at all possible so that I don’t have to explain what I am attempting to do.

Ok this is the Original Code
NPWStockBot main.js file

const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = '-';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('/Users/ufreewoody/desktop/MyDiscordBot/commands').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
    const command = require('/Users/ufreewoody/desktop/MyDiscordBot/commands/${file}');

    client.commands.set(command.name.command);

}

client.once('ready',() => {
    console.log('Walter is Online!');
});

client.on('message', message=>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'ping'){
        client.commands.get('ping').execute(message, args);
    } else if (command == 'youtube'){
        client.commands.get('youtube').execute(message, args);
    } else if (command == 'Strawberry'){
        message.channel.send('Im fluttering for your Love Strawberry Light Blue Butterfly');
    } else if (command == ('Light Blue Butterfly')){
        message.command.send('Sugie StrawBerry');
    }

});

client.login('NzgwNTA1ODM1NDk0MTc4ODE2.X7wEtA.fy9ssTMv4P7LJFZ26MXGFyNrSGs');

Now here is my attempt to simplify the code.

const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = '-';

client.once('ready',() => {
    console.log('TraderBotBob is Online!');
});

client.on('message', message=>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'ping'){
        message.channel.send('pong');
    } else if (command == 'youtube'){
        message.channel.send('https://www.youtube.com/codelyon');
    } else if (command === 'Strawberry'){
        message.channel.send('Im fluttering for your Love Sugie Strawberry');
    } else if (command === 'Light Blue Butterfly'){
        message.command.send('Sugie StrawBerry');
    }

});

client.login('Nzg5MTYxNTgyODE0NDI5MTg1.X9uB_w.W0QeWvFiD-8t63JpsclfozoBTGg');

I don’t really have time to go through the whole tutorial and try everything out. Specific questions would be better.

So I have been attempting to get my code to run on but it only seems to recognize the first two commands in my if else statements

If you are talking about this here …

    const command = args.shift().toLowerCase();

    if (command === 'ping'){
        message.channel.send('pong');
    } else if (command == 'youtube'){
        message.channel.send('https://www.youtube.com/codelyon');
    } else if (command === 'Strawberry'){
        message.channel.send('Im fluttering for your Love Sugie Strawberry');
    } else if (command === 'Light Blue Butterfly'){
        message.command.send('Sugie StrawBerry');
    }

… the issue that jumps out at me is that you are getting command and then immediately converting it to lowercase. But then in your if/else chain your 3rd and 4th checks are checking for strings that have capital letters in them - there is no argument you can pass that will catch on those - you have to check against strings that have all lowercase.

so your suggestion about using the Switch approch is appealing but im not sure how to do that

This would be a switch solution for the same thing:

const command = args.shift().toLowerCase();

switch (command) {
  case 'ping':
    message.channel.send('pong');
    break;
  case 'youtube':
    message.channel.send('https://www.youtube.com/codelyon');
    break;
  case 'strawberry':
    message.channel.send('Im fluttering for your Love Sugie Strawberry');
    break;
  case 'light blue butterfly':
    message.command.send('Sugie StrawBerry');
    break;
  default:
    // if you want to do something if the command isn't found
}

Another approach would be to create a dictionary of responses:

const RESPONSES = {
  ping: 'pong',
  youtube: 'https://www.youtube.com/codelyon',
  strawberry: 'Im fluttering for your Love Sugie Strawberry',
  'light blue butterfly': 'Sugie StrawBerry',
};

const command = args.shift().toLowerCase();
const response = RESPONSES[command];

if (response) {
  message.channel.send(response);
}

I like that because you could put your dictionary somewhere else, maybe even in a different file and unclutter your code.


If you are having trouble with JS, this forum is part of Free Code Camp which has a JS curriculum that is free and self-paced.

1 Like

Kevin, thank you so much.
Now that the holiday is over I will apply the code changes and I really like the code examples that you sent thats just what I need.

Thanks for your quick and thorough response.
Woody

Ok Kevin, I changed everything to lowercase and it works fine now.
Thank you so much