Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

144
Views
I'm trying to program my first discord bot but its not responding to the command (javascript)

I've been following a video on how to code commands for beginners. I tried this code but it's not responding to the command I checked the permissions no issues there as well. I copied his code all the same and did not also work.

const { Client, Intents } = require('discord.js');

const client = new Client( );

client.once('ready' , () => {
    console.log('bot is online');
    
const token = ' Token ';

});


const PREFIX = '!';


client.on('message ' , message=>{

    let args = message.content.substring(PREFIX.length).split(" ");

    switch(args[0]){
        case 'ping':
            message.reply('pong!');
        break;

    }
})






client.login ( token );
7 months ago · Juan Pablo Isaza
3 answers
Answer question

0

It should be

client.on('message', //rest of code

instead of

client.on('message ', //rest of code
7 months ago · Juan Pablo Isaza Report

0

The first thing I would do is to adjust the permissions in your client's declaration.

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

then I would try changing the event listener, the last version deprecated some old events

try

client.on("messageCreate", (message) => {

  let args = message.content.substring(PREFIX.length).split(" ");

  switch(args[0]){
    case 'ping':
      message.reply('pong!');
    break;

  }
});
7 months ago · Juan Pablo Isaza Report

0

So, I checked your code right now and since you're only running a few commands and nothing crazily advanced, I'd suggest just using an if statement to check the content of the message and IF It meets the condition to return something, here's the code that I tested and It works, It's a lot simpler and easily modifiable. I'd also suggest adjusting the Intents like Armandas Astrauskas said, and you shouldn't declare stuff inside client methods, like the ready one where you declared the token in, AND if you're using discord.js v13 you'd have to use messageCreate.

const {Client,Intents } = require('discord.js');

const client = new Client();
const token = ' Token ';


client.on('ready', () => {
  console.log('bot is online');
});


const PREFIX = '!';


client.on('message', message => {

  if (message.content == `${PREFIX}ping`) {
    message.reply("pong!")
  }

})

client.login(token);
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs