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

443
Views
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 answers
Answer question

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 Report
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!