简体   繁体   中英

My discord bot code is working but is not responding to my commands

I am a junior in programming. I know node.js and want to write my own bot for discord.

My code which is written below doesn't work. Can you help me with this?

const { Client, Intents } = require('discord.js');
const config = require('./config.json');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

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

client.on('message', message=>{
  if(!message.content.startsWith(config.prefix) || message.author.bot) return;

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

  if(command === 'ping'){
      message.channel.send('pong!');
  }
})

client.login(config.token);

GUILDS intent is not enough to receive messages. You will also need GUILD_MESSAGES intent for messages:

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

I didn't saw your error message yet but I assume it's because of your intents, so really easy solution is:

const client = new Client({
    intents: 32767
});

But you also need to have all intents enabled in your bot settings at https://discord.com/developers

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