【问题标题】:How to check if a message exists? (discord.js)如何检查消息是否存在? (discord.js)
【发布时间】:2021-04-18 08:06:24
【问题描述】:

我在这里有一个嵌入管理器,我想检查嵌入是否存在。我在发送后删除它,如果其他人在此之前删除它,我会收到 DiscordAPI 错误。我尝试了几件事,但从未得到解决方案。你能帮忙吗?

这是我的代码:

    error(channel, title, content) {
        var message
        var embed = new MessageEmbed() 
            .setColor(COLORS.red)
            .setDescription(content)
        if(title) {
            embed.setTitle(title)
        }
        channel.send(' ', embed).then((m) => {
            message = m
            try {
                setTimeout(function() {
                    if(MESSAGE EXISTS) {
                        m.delete()
                    } else {
                        return;
                    }
                }, 5500);
            } catch(e) {
            }

        })
        return message

    },

已编辑:

    error(channel, title, content) {
        var message
        var embed = new MessageEmbed() 
            .setColor(COLORS.red)
            .setDescription(content)
        if(title) {
            embed.setTitle(title)
        }
        channel.send(' ', embed).then((m) => {
            message = m
            try {
                setTimeout(function() {
                    try {
                        channel.messages.fetch(m.id)
                      .then(m => console.log(m.content)) //it fetched the message - good
                    } catch (error) {
                        console.log(error)
                    }
                }, 5500);
            } catch(e) {
            }

        })
        return message

    },

堆栈跟踪:

DiscordAPIError: Unknown Message
    at RequestHandler.execute (D:\Discord Bot\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
    at processTicksAndRejections (node:internal/process/task_queues:93:5)
    at async RequestHandler.push (D:\Discord Bot\node_modules\discord.js\src\rest\RequestHandler.js:39:14)
    at async MessageManager._fetchId (D:\Discord Bot\node_modules\discord.js\src\managers\MessageManager.js:135:18) {
  method: 'get',
  path: '/channels/797648418104934470/messages/798863571924418562',
  code: 10008,
  httpStatus: 404
}
npm ERR! code 1
npm ERR! path D:\Discord Bot
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node main.js debug

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    API 错误是由于不和谐试图删除不存在的消息(您已经将其删除)引起的。尝试通过在 try catch 语句中获取嵌入的 id 来解决承诺,然后从那里开始 - 这样可以避免 API 错误。

    try {
        channel.messages.fetch(message.id)
      .then(message => console.log(message.content)) //it fetched the message - good
    } catch (error) {
        return; //the message no longer exists and will be ignored
    }
    

    【讨论】:

    • 感谢您的回答。我已经尝试多次获取消息,但我失败了。你能帮忙吗?
    • 当然,我会编辑我的答案以提出修复建议,请稍等
    • 我会试试的。
    • 非常感谢!错误现在被捕获,我的机器人没有崩溃
    • 太棒了!请点击左侧的勾号标记答案为正确
    【解决方案2】:

    不要让它变得比需要的更困难。 .send.fetch.delete 都返回一个承诺。这意味着您可以(并且应该)使用catch 方法来捕获任何错误。您不需要使用 try-catch 块,除非您想使用 async/await 而不是 .then/catch

    检查下面的代码,我已经添加了所有catches。检查当消息不存在时(检查err.httpStatus)或由于任何其他原因无法删除消息时它如何捕获错误。

    channel.send(embed)
      .then((sentMessage) => {
        setTimeout(() => {
          channel.messages
            .fetch(sentMessage.id)
            .then((fetchedMessage) => {
              console.log('Message exists');
    
              fetchedMessage
                .delete()
                .then(() => console.log('Message deleted successfully'))
                .catch((err) => console.log('Could not delete the message', err));
            })
            .catch((err) => {
              if (err.httpStatus === 404) {
                console.log('Message already deleted');
              } else {
                console.log(err);
              }
            });
        }, 5000);
      });
    

    【讨论】:

      【解决方案3】:

      所以我遇到了类似的问题,我认为我的解决方案可能适用于您的问题。

       function msgExists(channelID, messageID){
      
      
      /*get channel*/
      const channel = client.channels.cache.get(channelID); //(get channel)
      
      /*get channel messages*/
      channel.messages.fetch({ limit: 100 }).then(messages => {
          let count = 0;
          let max = (messages.length - 1);
          //Iterate through the messages.
          messages.forEach(message => {                                   
                                          
                                          if (messageID === message.id){
                                              console.log("message found");
                                          }
                                          if (count == (max)){
                                              console.log("message could not be found");
                                          }
                                          
                                          count++;
                                      });
          
      
        });  
      

      }

      【讨论】:

        猜你喜欢
        • 2021-10-28
        • 2018-07-21
        • 2021-09-26
        • 2022-01-24
        • 1970-01-01
        • 2023-01-11
        • 2021-07-01
        • 2020-08-25
        • 2014-06-18
        相关资源
        最近更新 更多