Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

84
Views
discord.js - bot using old code to carry out commands

I'm pretty new to programming in general, and I have chosen to try out bot making as one of my first projects. However, my code seems to be using my old code and new code simultaneously. Here is what I had for my old code, in a single index.js file.

const Discord = require(discord.js);
const client = new Discord.Client();
require('dotenv').config();

client.on('ready', => {
console.log(`Logged in`)});

client.on("message", msg => {
if(msg.content === "ping") {
msg.reply("pong")
   }
})

client.login(process.env.BOT_TOKEN);

And that's it for my old code. I've recently updated the code to organise everything and include a command handler. This is the new code, in their index.js and ping.js files respectively.

For index.js:

const Discord = require('discord.js');
const fs = require('fs');
const client = new Discord.Client()
require('dotenv').config();

const prefix = '<'

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.on('ready', () => {
    console.log(`Logged in`)
});

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

client.login(process.env.BOT_TOKEN);

For ping.js:

module.exports = {
name: 'ping',
description: "ping command",
execute(message, args){
     message.reply("pong");
    }
}

And that's all the code. Now, the problem arises when I type <ping in the discord chat. Instead of replying with pong once, it does so twice. I have tried everything I could from saving and restarting Visual Studio Code, but to no avail. I do not even know if this is a problem with my code, as there are no error messages whatsoever when this happens. Thank you all who answer.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!