【问题标题】:My discord bot code is working but is not responding to my commands [duplicate]我的不和谐机器人代码正在运行,但没有响应我的命令 [重复]
【发布时间】:2021-12-03 02:53:03
【问题描述】:

我是编程的大三学生。我知道 node.js 并想为不和谐编写自己的机器人。

下面写的我的代码不起作用。 你能帮我解决这个问题吗?

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

【问题讨论】:

  • 对不起我的英语顺便说一句
  • 能否请您添加一个尝试运行机器人时遇到的错误?

标签: javascript node.js discord discord.js


【解决方案1】:

GUILDSintent 不足以接收消息。您还需要GUILD_MESSAGES 消息意图:

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

【讨论】:

    【解决方案2】:

    我还没有看到您的错误消息,但我认为这是因为您的意图,所以非常简单的解决方案是:

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

    但您还需要在https://discord.com/developers 的机器人设置中启用所有意图

    【讨论】:

    • 你不应该鼓励人们添加每一个意图。这是不好的做法,也是对功能的浪费。你应该只添加你真正需要的意图。
    • @MrMythical,我同意,但对我来说,OP 似乎需要 all 意图,所以我给了他们更简单的方法。 (不过你的回答也很好)
    • 他们只是回复消息...
    猜你喜欢
    • 2021-11-21
    • 1970-01-01
    • 2021-08-18
    • 2021-06-17
    • 2022-12-15
    • 2022-10-24
    • 2019-04-13
    • 2022-11-15
    相关资源
    最近更新 更多