简体   繁体   English

我的不和谐机器人代码正在运行,但没有响应我的命令

[英]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.我知道 node.js 并想为不和谐编写自己的机器人。

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. GUILDS意图不足以接收消息。 You will also need GUILD_MESSAGES intent for messages:您还需要GUILD_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但是您还需要在https://discord.com/developers 的机器人设置中启用所有意图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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