Browse Source

Fix webhook typings and use PartialMessageable instead of Object

pull/7442/head
Rapptz 4 years ago
parent
commit
69f578abdc
  1. 30
      discord/webhook/async_.py
  2. 10
      discord/webhook/sync.py

30
discord/webhook/async_.py

@ -43,7 +43,7 @@ from ..user import BaseUser, User
from ..asset import Asset from ..asset import Asset
from ..http import Route from ..http import Route
from ..mixins import Hashable from ..mixins import Hashable
from ..object import Object from ..channel import PartialMessageable
__all__ = ( __all__ = (
'Webhook', 'Webhook',
@ -58,6 +58,7 @@ if TYPE_CHECKING:
from ..file import File from ..file import File
from ..embeds import Embed from ..embeds import Embed
from ..mentions import AllowedMentions from ..mentions import AllowedMentions
from ..state import ConnectionState
from ..types.webhook import ( from ..types.webhook import (
Webhook as WebhookPayload, Webhook as WebhookPayload,
) )
@ -579,10 +580,11 @@ class _FriendlyHttpAttributeErrorHelper:
class _WebhookState: class _WebhookState:
__slots__ = ('_parent', '_webhook') __slots__ = ('_parent', '_webhook')
def __init__(self, webhook, parent): def __init__(self, webhook: Any, parent: Optional[Union[ConnectionState, _WebhookState]]):
self._webhook = webhook self._webhook: Any = webhook
if isinstance(parent, self.__class__): self._parent: Optional[ConnectionState]
if isinstance(parent, _WebhookState):
self._parent = None self._parent = None
else: else:
self._parent = parent self._parent = parent
@ -595,10 +597,12 @@ class _WebhookState:
def store_user(self, data): def store_user(self, data):
if self._parent is not None: if self._parent is not None:
return self._parent.store_user(data) return self._parent.store_user(data)
return BaseUser(state=self, data=data) # state parameter is artificial
return BaseUser(state=self, data=data) # type: ignore
def create_user(self, data): def create_user(self, data):
return BaseUser(state=self, data=data) # state parameter is artificial
return BaseUser(state=self, data=data) # type: ignore
@property @property
def http(self): def http(self):
@ -748,9 +752,9 @@ class BaseWebhook(Hashable):
'_state', '_state',
) )
def __init__(self, data: WebhookPayload, token: Optional[str] = None, state=None): def __init__(self, data: WebhookPayload, token: Optional[str] = None, state: Optional[ConnectionState] = None):
self.auth_token: Optional[str] = token self.auth_token: Optional[str] = token
self._state = state or _WebhookState(self, parent=state) self._state: Union[ConnectionState, _WebhookState] = state or _WebhookState(self, parent=state)
self._update(data) self._update(data)
def _update(self, data: WebhookPayload): def _update(self, data: WebhookPayload):
@ -765,10 +769,8 @@ class BaseWebhook(Hashable):
user = data.get('user') user = data.get('user')
self.user: Optional[Union[BaseUser, User]] = None self.user: Optional[Union[BaseUser, User]] = None
if user is not None: if user is not None:
if self._state is None: # state parameter may be _WebhookState
self.user = BaseUser(state=None, data=user) self.user = User(state=self._state, data=user) # type: ignore
else:
self.user = User(state=self._state, data=user)
source_channel = data.get('source_channel') source_channel = data.get('source_channel')
if source_channel: if source_channel:
@ -1179,7 +1181,9 @@ class Webhook(BaseWebhook):
def _create_message(self, data): def _create_message(self, data):
state = _WebhookState(self, parent=self._state) state = _WebhookState(self, parent=self._state)
channel = self.channel or Object(id=int(data['channel_id'])) # state may be artificial (unlikely at this point...)
channel = self.channel or PartialMessageable(state=self._state, id=int(data['channel_id'])) # type: ignore
# state is artificial
return WebhookMessage(data=data, state=state, channel=channel) # type: ignore return WebhookMessage(data=data, state=state, channel=channel) # type: ignore
@overload @overload

10
discord/webhook/sync.py

@ -43,7 +43,7 @@ from .. import utils
from ..errors import InvalidArgument, HTTPException, Forbidden, NotFound, DiscordServerError from ..errors import InvalidArgument, HTTPException, Forbidden, NotFound, DiscordServerError
from ..message import Message from ..message import Message
from ..http import Route from ..http import Route
from ..object import Object from ..channel import PartialMessageable
from .async_ import BaseWebhook, handle_message_parameters, _WebhookState from .async_ import BaseWebhook, handle_message_parameters, _WebhookState
@ -373,6 +373,8 @@ class SyncWebhookMessage(Message):
.. versionadded:: 2.0 .. versionadded:: 2.0
""" """
_state: _WebhookState
def edit( def edit(
self, self,
content: Optional[str] = MISSING, content: Optional[str] = MISSING,
@ -745,8 +747,10 @@ class SyncWebhook(BaseWebhook):
def _create_message(self, data): def _create_message(self, data):
state = _WebhookState(self, parent=self._state) state = _WebhookState(self, parent=self._state)
channel = self.channel or Object(id=int(data['channel_id'])) # state may be artificial (unlikely at this point...)
return SyncWebhookMessage(data=data, state=state, channel=channel) channel = self.channel or PartialMessageable(state=self._state, id=int(data['channel_id'])) # type: ignore
# state is artificial
return SyncWebhookMessage(data=data, state=state, channel=channel) # type: ignore
@overload @overload
def send( def send(

Loading…
Cancel
Save