Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
My bot is responding to all prefixes and I don't know why

I am using the latest node.js and discord.js versions and I am coding on VSC. My bot started responding to all prefixes and I don't know why. The code I have currently:

const Discord = require('discord.js');
const client = new Discord.Client();
 
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)
}

const PREFIX = '$';
 
 
client.on('ready', () => {
    console.log('updated');
})

client.on('message', message => {
 
    const args = message.content.substring(PREFIX.length).split(" ");

    switch (args[0]) {
        case 'ping':
            message.channel.send('pong!');
            break;
    }
});
 
        client.login(token);

However, with this code in mind, the bot still responds to any prefix. In the image below, I sent the ping command with various random letters and symbols in all at the same time. The bot responded pong to all of them.

Image of the bot not working

not sure what is wrong as I am still new to discord.js but I cannot figure it out.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Maybe you can check if it's a command before.

client.on('message', message => {
  
  const isCommand = message.content.substring(0,PREFIX.length) === PREFIX
  if(isCommand){
    const args = message.content.substring(PREFIX.length).split(" ");
    switch (args[0]) {
        case 'ping':
            message.channel.send('pong!');
            break;
    }
  }
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda