Discord.js:检查消息是否被删除

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

Discord.js: Check if message is deleted

问题

这是我的机器人获取消息的方式:

await channel.messages.fetch(snowflake)

以下是我观察到的行为:

  • 如果消息存在于频道中,并且调用了这个方法,它会返回一个带有雪花ID的Message对象。
  • 如果消息在机器人运行时被删除,并且调用了这个方法,它仍然会返回一个带有雪花ID的Message对象,很可能是缓存中的同一个对象。
  • 如果消息在机器人关闭后被删除,并且在机器人重新启动后调用了这个方法,它会引发一个带有代码10008DiscordAPIError
  • 如果消息在机器人运行时被删除,然后重新启动机器人,并且调用了这个方法,它也会引发之前相同的DiscordAPIError

不幸的是,加粗的情况使我很难检查消息是否被删除。它不会引发相同的DiscordAPIError,因此代码会顺利执行,直到我对Message对象执行.delete.edit等操作,这将引发DiscordAPIError

使用try...catch捕获.delete.edit等函数的问题在于,我必须对每一个这些函数都这样做,这不是我想要的。 我想要在执行这些操作之前通过检查消息是否被删除来停止代码的执行,可以使用try...catchif语句。

不,.deleted已被弃用,不幸的是我不能使用它。

不幸的是,当机器人运行时,.editable在消息被删除时不会更新。

在执行操作之前,如何使用channel.messages.fetch检测消息是否被删除?

英文:

This is how my bot fetches messages:

await channel.messages.fetch(snowflake)

These are the following behaviors I've observed:

  • If the message exists in the channel and this is called, this returns a Message object with the snowflake ID.
  • If the message is deleted while the bot is on and this is called, this still returns a Message object with the snowflake ID, likely the same object from cache.
  • If the message is deleted when the bot is off and this is called after the bot turns on, this raises a DiscordAPIError with code 10008.
  • If the message is deleted when the bot is on, then the bot is restarted, then this is called, this also raises the same DiscordAPIError previously.

The bolded case unfortunately makes it hard for me to check if the message is deleted or not. It doesn't raise the same DiscordAPIError and because of that the code goes along nicely until I do operations on the Message object such as .delete or .edit which will raise the DiscordAPIError.

The problem with try...catching the .delete, .edit, etc. functions is that I would have to do this for every one of those functions, which is not what I want. I want to be able to stop the code before it reaches to those operations by checking if the message is deleted or not, either by try...catch or if.

No, .deleted is deprecated and unfortunately I can't use this.

Unfortunately .editable doesn't update when the message is deleted while the bot is on.

How would I detect if a message is deleted using channel.messages.fetch before performing operations on it?

答案1

得分: 0

你可以在 fetch 中使用 force 选项来跳过缓存检查。

await channel.messages.fetch({ message: snowflake, force: true })
英文:

You can use the force option with fetch to skip the cache check

await channel.messages.fetch({ message: snowflake, force: true })

huangapple
  • 本文由 发表于 2023年8月8日 23:55:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861256.html
匿名

发表评论

匿名网友

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

确定