事实证明它对我也不起作用,我认为它有效
因为我之前没有测试过,只使用了历史或者我自己作为用户。
一种解决方法是使用 channel.history 代替它返回的消息
支持 .content 和类似的。
可能与此类似
@bot.event
async def on_message(message):
async for message in message.channel.history(limit=1):
print(message.content)
我也尝试(糟糕地)让 discord.py 总是从历史中获取它,这似乎对我有用。 (适用于 v1.x)
From cbbd51bf17bb19ea4835365e08266d06a27c1459 Mon Sep 17 00:00:00 2001
From: Example User <user@example.com>
Date: Fri, 7 May 2021 13:37:50 +0300
Subject: [PATCH] add workaround for on_message on self bots
Recently, now the messages that self bots get in events are broken.
In the case of on_message, .content and .embeds don't work.
However, this isn't the case if the message is retrieved with
channel.history()
This workarounds the issue for on_message by retrieving it from history
always
NOTE: I made the parse call async, didnt seem to break anything (yet)
---
discord/gateway.py | 6 +++++-
discord/state.py | 9 ++++++++-
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/discord/gateway.py b/discord/gateway.py
index 210a8822..019693db 100644
--- a/discord/gateway.py
+++ b/discord/gateway.py
@@ -30,6 +30,7 @@ import concurrent.futures
import json
import logging
import struct
+import inspect
import sys
import time
import threading
@@ -506,7 +507,10 @@ class DiscordWebSocket:
except KeyError:
log.debug('Unknown event %s.', event)
else:
- func(data)
+ if inspect.iscoroutinefunction(func):
+ await func(data)
+ else:
+ func(data)
# remove the dispatched listeners
removed = []
diff --git a/discord/state.py b/discord/state.py
index da1212c1..7c713e39 100644
--- a/discord/state.py
+++ b/discord/state.py
@@ -485,9 +485,16 @@ class ConnectionState:
def parse_resumed(self, data):
self.dispatch('resumed')
- def parse_message_create(self, data):
+ async def parse_message_create(self, data):
channel, _ = self._get_guild_channel(data)
message = Message(channel=channel, data=data, state=self)
+ # This is a workaround for messages not working on self bots
+ async for temp_msg in message.channel.history(limit=1):
+ if temp_msg.id != message.id:
+ log.warning("retrieved on_message from history is not correct one")
+ break
+
+ message = temp_msg
self.dispatch('message', message)
if self._messages is not None:
self._messages.append(message)
--
2.30.2
编辑:现在看别人的回答