我的Discord机器人无法读取私信消息。

huangapple go评论90阅读模式
英文:

My discord Bot is Unable to read DM messages

问题

我知道"必须启用CHANNEL部分",而且已经启用了。但是我的机器人仍然无法识别直接消息。

这是我的代码:

const client = new Client({intents:[...],partials:[Partials.Channel,...]});

client.on("messageCreate", (message) =>{
if (message.channel.type === 'DM') console.log('收到直接消息');
})

机器人的其他部分都正常工作,只有这部分出了问题。

英文:

I know that "the CHANNEL partial must be enabled" and it is. But my bot still can't recognize DM messages.

Here is my code:

const client = new Client({intents:[...],partials:[Partials.Channel,...]});

client.on("messageCreate", (message) =>{
if (message.channel.type === 'DM') console.log('Dm recieved');
})

All the other parts of the bot work except this part.

答案1

得分: 0

请查看事件处理指南。我个人建议使用单独文件类型的事件处理器

示例:

const { Events } = require('discord.js');

client.once(Events.messageCreate, message => {
	console.log(`消息事件触发!消息内容:${message.content}`);
});

我的建议(单独文件类型的事件处理器):

for (const file of eventFiles) {
  const event = require(`./events/${file}`);
  console.log(`| ✅ ${file} 已加载!`);

  if (event.once) {
    client.once(event.name, (...args) => event.execute(...args, commands));
  } else {
    client.on(event.name, (...args) => event.execute(...args, commands));
  }
}
module.exports = {
  name: "messageCreate",
  once: false,
  async execute(message) {
    // 处理消息的代码
  }
}

您还可以尝试记录事件以检查是否被正确触发。

如果上述答案不起作用,您可以尝试以下代码:

if (message.channel.isDMBased())
英文:

Please see the guide on how to handle events. My personal recommendation is to use an individual file type event handler

Example:

const { Events } = require('discord.js');

client.once(Events.messageCreate, message => {
	console.log(`Message event triggered! Message: {message.content}`);
});

My recommendation (individual file type event handler):

for (const file of eventFiles) {
  const event = require(`./events/${file}`);
  console.log(`| ✅ ${file} loaded!`);

  if (event.once) {
    client.once(event.name, (...args) => event.execute(...args, commands));
  } else {
    client.on(event.name, (...args) => event.execute(...args, commands));
  }
}
module.exports = {
  name: "messageCreate",
  once: false,
  async execute(message) {
    // Your code to handle messages
  }
}

You can also try logging in the event to check if it's being triggered correctly.

If the above answers do not work, you can try this:

if (message.channel.isDMBased())

huangapple
  • 本文由 发表于 2023年1月8日 18:49:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75047164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定