diff options
| author | Rapptz <[email protected]> | 2019-03-02 06:14:10 -0500 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2019-03-02 06:14:10 -0500 |
| commit | c96046536603c1014c823c9f6185bdb29b96165a (patch) | |
| tree | 3abcf8a01a548ebc75d72a3b9c448895e820178e | |
| parent | Properly clean-up file objects when necessary instead of instantly. (diff) | |
| download | discord.py-c96046536603c1014c823c9f6185bdb29b96165a.tar.xz discord.py-c96046536603c1014c823c9f6185bdb29b96165a.zip | |
Mock a ConnectionState object to fix wait=True errors in webhooks.
Fixes #1898
| -rw-r--r-- | discord/webhook.py | 43 |
1 files changed, 37 insertions, 6 deletions
diff --git a/discord/webhook.py b/discord/webhook.py index c4ba6bf4..7e130f19 100644 --- a/discord/webhook.py +++ b/discord/webhook.py @@ -100,10 +100,6 @@ class WebhookAdapter: """ raise NotImplementedError() - def store_user(self, data): - # mocks a ConnectionState for appropriate use for Message - return BaseUser(state=self.webhook._state, data=data) - async def _wrap_coroutine_and_cleanup(self, coro, cleanup): try: return await coro @@ -301,6 +297,41 @@ class RequestsWebhookAdapter(WebhookAdapter): from .message import Message return Message(data=response, state=self.webhook._state, channel=self.webhook.channel) +class _FriendlyHttpAttributeErrorHelper: + __slots__ = () + + def __getattr__(self, attr): + raise AttributeError('PartialWebhookState does not support http methods.') + +class _PartialWebhookState: + __slots__ = ('loop',) + + def __init__(self, adapter): + # Fetch the loop from the adapter if it's there + try: + self.loop = adapter.loop + except AttributeError: + self.loop = None + + def _get_guild(self, guild_id): + return None + + def store_user(self, data): + return BaseUser(state=self, data=data) + + @property + def is_bot(self): + return True + + @property + def http(self): + # Some data classes assign state.http and that should be kosher + # however, using it should result in a late-binding error. + return _FriendlyHttpAttributeErrorHelper() + + def __getattr__(self, attr): + raise AttributeError('PartialWebhookState does not support {0:!r}.'.format(attr)) + class Webhook: """Represents a Discord webhook. @@ -371,7 +402,7 @@ class Webhook: self.name = data.get('name') self.avatar = data.get('avatar') self.token = data['token'] - self._state = state + self._state = state or _PartialWebhookState(adapter) self._adapter = adapter self._adapter._prepare(self) @@ -453,7 +484,7 @@ class Webhook: If this is a partial webhook, then this will always return ``None``. """ - return self._state and self._state._get_guild(self.guild_id) + return self._state._get_guild(self.guild_id) @property def channel(self): |