javascriptdiscorddiscord.jsbots

My Discord bot is not responding to commands


So I'm following a YouTube toturial from 2021 on how to code a discord bot, beginner course. And im stuck on this one part of the tutorial that i can't get to work. I can't Get the bot to respond to my commands on Discord. If you know how to fix this i would appreciate the help, thx!

const Discord = require('discord.js');

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

const client = new Discord.Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
  ]
})

const prefix = '!';

client.once('ready', () => {
    console.log('IamBot is online!');
});

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'){
        message.channel.send('pong!');
    } else if (command == 'youtube'){
        message.channel.send('https://www.youtube.com/channel/UCPORwSx6_1e00INnAdrkDHg/videos');
    }
});

client.login('My Token Is Here');

Solution

  • You need to add this to your intents.

    GatewayIntentBits.MessageContent
    

    It should look like this:

    const client = new Discord.Client({
      intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent,
      ]
    })
    

    Also make sure to enable Message Content intent on the bot's Discord page. intent

    EDIT:

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

    Should be

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

    As seen in the docs