【问题标题】:How do I fix DiscordAPIError: Unknown Message?如何修复 DiscordAPIError:未知消息?
【发布时间】:2021-09-20 14:36:21
【问题描述】:

我制作了一个删除图像通道中的文本消息的机器人,但是当我使用它时,它会记录此错误。

client.on("message", async message => {
    if (message.author.bot) return;
    const users = ['853248522563485717'];
    if (message.channel.id === '841260660960395287') {
        if (users.includes(message.author.id)) return;
        if (message.attachments.size !== 0) return;
        message.member.send("You can't send text in 'images' channel")
         await message.delete();
    } else return;
});

(这与其他相同主题的问题不同) 我该如何解决?

【问题讨论】:

  • 你确定你没有运行同一个机器人的多个外壳?这样它可能会尝试删除已删除的消息
  • 我很确定这是问题所在,您知道如何解决吗?
  • 关闭另一个正在运行的shell?应该不难;-;

标签: javascript node.js discord.js


【解决方案1】:

最可能的问题是您正在运行 Discord 机器人的多个实例。检查您是否打开了多个命令行,如果是,请检查它们是否正在运行机器人。除此之外,如果用户发送垃圾邮件,Discord API 往往会混淆要删除哪些消息,使其尝试删除同一条消息超过 1 次。您可以尝试添加一个 if 语句来检查消息是否未定义。

client.on("message", async (message) => {
  if (!message) return; // ADDED THIS LINE OF CODE
  if (message.author.bot) return;
  const users = ["853248522563485717"];
  if (message.channel.id === "841260660960395287") {
    if (users.includes(message.author.id)) return;
    if (message.attachments.size !== 0) return;
    message.member.send("You can't send text in 'images' channel");
    await message.delete();
  } else {
    return;
  }
});

【讨论】:

  • 这现在给了我一个新错误 - (node:15092) MaxListenersExceededWarning: 检测到可能的 EventEmitter 内存泄漏。 11 个消息监听器添加到 [Client]。使用emitter.setMaxListeners() 增加限制(使用node --trace-warnings ... 显示警告的创建位置)(节点:15092)UnhandledPromiseRejectionWarning:DiscordAPIError:未知消息
  • @Dawid Szarek,他的问题是他正在使用多个 on message 事件。
  • 啊,是这样的
【解决方案2】:

根据您上面的错误,您似乎对您希望机器人执行的每个命令或功能都使用了message event listener。最大值为 11(相同类型)。这不是正确的做法。

什么是事件监听器?

//the 'client.on' function is called every time a 'message' is sent

client.on("message", async (message) => {
  //do code
});

如何解决这个问题:

不要为每个命令使用 1 个事件侦听器,而是为整个客户端使用 1 个。你可以通过多种方式解决这个问题,command handlers 或类似的,但我会保持简单。

这是您应该在index.jsmain.js 文件中看到的基本设置:

// require the discord.js module
const Discord = require('discord.js');

// create a new Discord client
const client = new Discord.Client();

// on message, run this
client.on('message', (message) => {
    if (!message) return;
    console.log(`message sent in ${message.channel.name}. message content: ${message.content}`);
    //this event will fire every time a message is sent, for example.
});

// login to Discord with your app's token
client.login('your-token-goes-here');

如果你想用你的机器人做多个功能,你会怎么做? (例如删除图像中的文本仅通道将消息记录到控制台)

命令处理。这是唯一的方法。我在上面链接了一个官方 Discord.js 指南,通读它,看看你能做什么。我强烈建议您尝试使用完全不同的机器人和干净的索引文件,因为这比尝试在已经有问题的代码之间适应您不理解的代码更容易。

【讨论】:

    猜你喜欢
    • 2018-11-05
    • 1970-01-01
    • 2022-01-21
    • 2021-12-26
    • 2020-09-24
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    相关资源
    最近更新 更多