简体   繁体   中英

DiscordBot online but doesn't respond to command (js)

I have no experience with dc bots so I followed a yt tutorial but discord.js he used was an older version (v12) so I fixed the code for my version v14. The Bot goes online but when I type a command it won't respond. There are no errors it just goes online and gives the console message that's it. Maybe it's because of the version but I can't find any answers for this version. (Maybe it's something else as I said I've never done that before) This is my main.js:


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

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

  const prefix ='!';

  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);
  };

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


client.on('message', message =>{
  console.log("1")
    if(!message.content.startsWith(prefix) || message.author.bot) return;
    console.log("2")

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'ping'){
        client.commands.get('ping').execute(message, args);
    }
});

client.login('TOKEN');

The console.log() 1 and 2 are to check if it works but it won't show in the console just the "Bot is online". Then I have a folder /commands with ping.js:

module.exports = {
    name: 'ping',
    description: "this is a ping command!",
    execute(message, args){
        message.channel.send('pong!')
    }
}

You should request the message content intent. See how:

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

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

Note that you may also need to enable it in the Discord Developer Portal: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM