英文:
Discord.js: Check if message is deleted
问题
这是我的机器人获取消息的方式:
await channel.messages.fetch(snowflake)
以下是我观察到的行为:
- 如果消息存在于频道中,并且调用了这个方法,它会返回一个带有雪花ID的
Message
对象。 - 如果消息在机器人运行时被删除,并且调用了这个方法,它仍然会返回一个带有雪花ID的
Message
对象,很可能是缓存中的同一个对象。 - 如果消息在机器人关闭后被删除,并且在机器人重新启动后调用了这个方法,它会引发一个带有代码
10008
的DiscordAPIError
。 - 如果消息在机器人运行时被删除,然后重新启动机器人,并且调用了这个方法,它也会引发之前相同的
DiscordAPIError
。
不幸的是,加粗的情况使我很难检查消息是否被删除。它不会引发相同的DiscordAPIError
,因此代码会顺利执行,直到我对Message
对象执行.delete
或.edit
等操作,这将引发DiscordAPIError
。
使用try...catch
捕获.delete
、.edit
等函数的问题在于,我必须对每一个这些函数都这样做,这不是我想要的。 我想要在执行这些操作之前通过检查消息是否被删除来停止代码的执行,可以使用try...catch
或if
语句。
不,.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 code10008
. - 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...catch
ing 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 })
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论