on_delete_message 中的 Discord py 错误 -> 未知消息
Posted
技术标签:
【中文标题】on_delete_message 中的 Discord py 错误 -> 未知消息【英文标题】:Discord py Error in on_delete_message -> Unknown Message 【发布时间】:2021-06-14 17:45:35 【问题描述】:我正在为我的不和谐频道制作机器人,但 on_delete_message 函数出现错误。
奇怪的是我在不同的函数中得到了基本相同的东西:on_reaction_clear,它似乎很好。
问题是在 react.users(): 行中的 async for user 中引发的,并且仅在 on_reaction_delete 函数中,而不是在上面的函数中
@client.event
async def on_reaction_clear(message, reactions):
global con
cur = con.cursor()
for reaction in message.reactions:
async for user in reaction.users():
cur.execute('SELECT count FROM reactions WHERE emoji = "0" AND name = "1" AND channel = "2"'.format(str(reaction.emoji), str(user.id), str(reaction.channel_id)))
count = cur.fetchone()
if (count[0] > 0):
cur.execute('UPDATE reactions SET count = count - 1 WHERE emoji = "0" AND name = "1" AND channel = "2"'.format(str(reaction.emoji), str(user.id), str(reaction.channel_id)))
else:
cur.execute('DELETE FROM reactions WHERE emoji = "0" AND name = "1" AND channel = "2"'.format(str(reaction.emoji), str(user.id), str(reaction.channel_id)))
@client.event
async def on_message_delete(message):
global con
cur = con.cursor()
for reaction in message.reactions:
async for user in reaction.users():
cur.execute('SELECT count FROM reactions WHERE emoji = "0" AND name = "1" AND channel = "2"'.format(str(reaction.emoji), str(user.id), str(reaction.channel_id)))
count = cur.fetchone()
if (count[0] > 0):
cur.execute('UPDATE reactions SET count = count - 1 WHERE emoji = "0" AND name = "1" AND channel = "2"'.format(str(reaction.emoji), str(user.id), str(reaction.channel_id)))
else:
cur.execute('DELETE FROM reactions WHERE emoji = "0" AND name = "1" AND channel = "2"'.format(str(reaction.emoji), str(user.id), str(reaction.channel_id)))
print("done")
错误信息:
Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "asd" is not found
Ignoring exception in on_message_delete
Traceback (most recent call last):
File "C:\Users\seb82\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "c:\Users\seb82\source\reaction-counter-bot\reaction-counter-bot.py", line 145, in on_message_delete
async for user in reaction.users():
File "C:\Users\seb82\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\iterators.py", line 86, in __anext__
msg = await self.next()
File "C:\Users\seb82\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\iterators.py", line 139, in next
await self.fill_users()
File "C:\Users\seb82\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\iterators.py", line 154, in fill_users
data = await self.getter(self.channel_id, self.message.id, self.emoji, retrieve, after=after)
File "C:\Users\seb82\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\http.py", line 243, in request
raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10008): Unknown Message
希望你们能帮帮我:)
【问题讨论】:
由于消息被删除,您无法获取反应,可悲的是无法修复 【参考方案1】:tl;博士:
解决方案:使用on_reaction_add
并将所有反应保存在您的数据库中,当消息被删除时使用该数据库来执行您之前尝试执行的操作。
嘿,正如 Lukasz 之前解释的那样,您无法从已删除的消息中获取反应。
async
和 await
关键字表示您的机器人尝试从不和谐服务器获取新信息。
就像“嘿,不和谐,请告诉我对此消息的反应”和不和谐的回答:“这不存在,对不起兄弟,这是一个 404 交换”;)
这适用于反应,因为在调用函数时会将已删除的反应发送给您 -> 你不需要单独提出这个要求。
我的建议如下:
由于您已经涉及 sql,因此请使用 on_reaction_add
侦听器将每个反应(以及您需要的信息)保存在“映射”到其消息 ID 的表中。
如果消息被删除 (on_message_delete
),只需在表中搜索与该消息相关的所有条目并执行您之前尝试执行的操作。
附:请注意代码中的“原始字符串魔术”sql 语句。 sql注入是一回事,你知道的:)
【讨论】:
以上是关于on_delete_message 中的 Discord py 错误 -> 未知消息的主要内容,如果未能解决你的问题,请参考以下文章