Discord.js V14 - client.on message create not firing Discord.js V14 - 客户端监听消息创建事件未触发

3

我正在尝试使用Discord.js的V14版本,有很多新的东西!但是整个意图(intents)的概念,我不太理解。我的messageCreate事件没有触发,从其他帖子中得知这可能与意图有关。我已经尝试过修改意图,但似乎没有什么作用。下面是我的代码:

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});
client.on("messageCreate", (message) => {
  console.log("new message...")
  console.log(message.message)
  console.log(message.content)
if (message.mentions.users.first() === client) {
  message.reply("Hey!.")
} 
});
client.login(process.env.token);

Thanks!!

2个回答

3

搞定了!您需要在开发者门户中启用消息内容意图:

图片

此外,您需要将messageContent意图置于顶部:

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [ 
  GatewayIntentBits.DirectMessages,
  GatewayIntentBits.Guilds,
  GatewayIntentBits.GuildBans,
  GatewayIntentBits.GuildMessages,
  GatewayIntentBits.MessageContent,] });
client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});
client.on("messageCreate", (message) => {
if (message.content === "<@1023320497469005938>") {
  message.reply("Hey!")
} 
});
client.login(process.env.token);

关于从消息中获取ping的问题,虽然不是我最初提出的问题,但是我需要解决。只需使用以下代码即可: if (message.content) === "<@BotID>" {CODE HERE}


1
你需要使用 GuildMessages 意图。
注意:在提及机器人的消息中,您不需要使用 MessageContent 意图,因为机器人可以接收到提及它们的消息内容而无需该意图。
const client = new Client({ 
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages
  ]
});
// ...
client.on("messageCreate", (message) => {
  // ...
  if (message.mentions.users.has(client.user.id)) { // this could be modified to make it have to *start* with or have only a ping
    message.reply("Hey!.")
  } 
});
// ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接