【问题标题】:Discord bot not responding Node.js V16.5 [duplicate]Discord bot没有响应Node.js V16.5 [重复]
【发布时间】:2021-12-26 00:28:18
【问题描述】:

我尝试了所有教程,但我的机器人只是不响应我在 Discord 聊天中的命令

Index.js

const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');


const rest = new REST({ version: '9' }).setToken('[bot token]');

(async () => {
  try {
    console.log('Started refreshing application (/) commands.');

    await rest.put(
      Routes.applicationGuildCommands([clinent id], [guild id]),
      { body: commands },
    );

    console.log('Successfully reloaded application (/) commands.');
  } catch (error) {
    console.error(error);
  }
})();

bot.js

const { DiscordAPIError } = require('@discordjs/rest');
const { Client, Intents } = require('discord.js');
const Discord = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const token = 'token';
const prefix = '!';

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
  console.log(``);
});

client.on('messageCreate', (message) => {
    if(message.content.toLowerCase().includes('hey bot') || message.content.toLowerCase().includes('general kenobi')){
        message.channel.send('Hello there!');
    }
});

client.on('messageCreate', (message) => {
    if(message.content.toLowerCase().includes('fudge') || message.content.toLowerCase().includes('pudding')){
        message.channel.send('Such language is prohibited!');
    }
});

client.login(token);

当我输入任何命令时,Discord 机器人只会保持沉默。我在package.json 中添加了bot.js 作为main。

我在控制台中没有收到任何错误。

【问题讨论】:

  • 你不应该有 2 个messageCreate 事件,你所有的逻辑都应该在一个内处理

标签: javascript node.js discord discord.js bots


【解决方案1】:

我找到了原因。显然,我必须从不和谐的开发人员部分检查我的机器人所需的所有权限。没有教程显示。在那之后,我放了更多的意图:Intents.FLAGS.GUILDS、Intents.FLAGS.GUILD_MEMBERS、Intents.FLAGS.GUILD_MESSAGES

enter image description here

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

如果我没记错的话,这些意图只涉及公会的操作,而不是消息。您需要GUILD_MESSAGES,甚至可能还需要DIRECT_MESSAGES 意图。

https://discord.com/developers/docs/topics/gateway#list-of-intents

【讨论】:

  • 是的。这是解决方案的一部分。我必须从开发人员部分的机器人设置中检查权限。没有它们,我会在提出多个意图时出错。谢谢!
猜你喜欢
  • 2021-12-16
  • 2021-04-15
  • 2022-01-04
  • 2021-11-11
  • 2019-04-11
  • 1970-01-01
  • 2023-04-10
  • 2023-01-23
相关资源
最近更新 更多