Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

85
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda