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

456
Visualizações
How do i make my discord bot respond to a message with prefix?

What I'm trying to do is set up a discord autoresponse bot that responds if someone says match prefix + respondobject like "!ping". I don't know why it doesn't come up with any response in the dispute. I've attached a picture of what it does. I can't figure out why it's not showing up with any responses in discord.

const Discord = require('discord.js');
const client = new Discord.Client({intents: ["GUILDS", "GUILD_MESSAGES"]});
const prefix = '!'

client.on('ready', () => {
let botStatus = [
  'up!h or up!help',
  `${client.users.cache.size} citizens!`,
  `${client.guilds.cache.size} servers!`
]

setInterval(function(){
  let status = botStatus[Math.floor(Math.random() * botStatus.length)]
  client.user.setActivity(status, {type: 'WATCHING'})
}, 15000);
  console.log(client.user.username);
});

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

  const responseObject = {
  "ping": `🏓 Latency is ${msg.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms`
};

  if (responseObject[message.content]) {
    message.channel.send('Loading data...').then (async (msg) =>{
    msg.delete()
    message.channel.send(responseObject[message.content]);
  }).catch(err => console.log(err.message))
  }
});

client.login(process.env.token);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your main issue is that you're trying to check message.content against 'ping', but require a prefix. Your message.content will always have a prefix, so you could try if (responseObject[message.content.slice(prefix.length)]).

Other alternatives would be to add the prefix to the object ("!ping": "Latency is...")

Or, create a variable that tracks the command used.

let cmd = message.content.toLowerCase().slice(prefix.length).split(/\s+/gm)[0]
// then use it to check the object
if (responseObject[cmd]) {}
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