Browse Source

Add support for passing client to Webhook.from_url and Webhook.partial

pull/10109/head
Rapptz 2 years ago
committed by dolfies
parent
commit
956697080d
  1. 61
      discord/webhook/async_.py

61
discord/webhook/async_.py

@ -62,6 +62,7 @@ if TYPE_CHECKING:
from types import TracebackType from types import TracebackType
from ..embeds import Embed from ..embeds import Embed
from ..client import Client
from ..mentions import AllowedMentions from ..mentions import AllowedMentions
from ..message import Attachment from ..message import Attachment
from ..state import ConnectionState from ..state import ConnectionState
@ -949,7 +950,15 @@ class Webhook(BaseWebhook):
return f'https://discord.com/api/webhooks/{self.id}/{self.token}' return f'https://discord.com/api/webhooks/{self.id}/{self.token}'
@classmethod @classmethod
def partial(cls, id: int, token: str, *, session: aiohttp.ClientSession, bot_token: Optional[str] = None) -> Self: def partial(
cls,
id: int,
token: str,
*,
session: aiohttp.ClientSession = MISSING,
client: Client = MISSING,
bot_token: Optional[str] = None,
) -> Self:
"""Creates a partial :class:`Webhook`. """Creates a partial :class:`Webhook`.
Parameters Parameters
@ -964,12 +973,23 @@ class Webhook(BaseWebhook):
will not close it. will not close it.
.. versionadded:: 2.0 .. versionadded:: 2.0
client: :class:`Client`
The client to initialise this webhook with. This allows it to
attach the client's internal state. If ``session`` is not given
while this is given then the client's internal session will be used.
.. versionadded:: 2.2
bot_token: Optional[:class:`str`] bot_token: Optional[:class:`str`]
The bot authentication token for authenticated requests The bot authentication token for authenticated requests
involving the webhook. involving the webhook.
.. versionadded:: 2.0 .. versionadded:: 2.0
Raises
-------
TypeError
Neither ``session`` nor ``client`` were given.
Returns Returns
-------- --------
:class:`Webhook` :class:`Webhook`
@ -982,10 +1002,26 @@ class Webhook(BaseWebhook):
'token': token, 'token': token,
} }
return cls(data, session, token=bot_token) state = None
if client is not MISSING:
state = client._connection
if session is MISSING:
session = client.http._HTTPClient__session # type: ignore
if session is MISSING:
raise TypeError('session or client must be given')
return cls(data, session, token=bot_token, state=state)
@classmethod @classmethod
def from_url(cls, url: str, *, session: aiohttp.ClientSession, bot_token: Optional[str] = None) -> Self: def from_url(
cls,
url: str,
*,
session: aiohttp.ClientSession = MISSING,
client: Client = MISSING,
bot_token: Optional[str] = None,
) -> Self:
"""Creates a partial :class:`Webhook` from a webhook URL. """Creates a partial :class:`Webhook` from a webhook URL.
.. versionchanged:: 2.0 .. versionchanged:: 2.0
@ -1002,6 +1038,12 @@ class Webhook(BaseWebhook):
will not close it. will not close it.
.. versionadded:: 2.0 .. versionadded:: 2.0
client: :class:`Client`
The client to initialise this webhook with. This allows it to
attach the client's internal state. If ``session`` is not given
while this is given then the client's internal session will be used.
.. versionadded:: 2.2
bot_token: Optional[:class:`str`] bot_token: Optional[:class:`str`]
The bot authentication token for authenticated requests The bot authentication token for authenticated requests
involving the webhook. involving the webhook.
@ -1012,6 +1054,8 @@ class Webhook(BaseWebhook):
------- -------
ValueError ValueError
The URL is invalid. The URL is invalid.
TypeError
Neither ``session`` nor ``client`` were given.
Returns Returns
-------- --------
@ -1023,9 +1067,18 @@ class Webhook(BaseWebhook):
if m is None: if m is None:
raise ValueError('Invalid webhook URL given') raise ValueError('Invalid webhook URL given')
state = None
if client is not MISSING:
state = client._connection
if session is MISSING:
session = client.http._HTTPClient__session # type: ignore
if session is MISSING:
raise TypeError('session or client must be given')
data: Dict[str, Any] = m.groupdict() data: Dict[str, Any] = m.groupdict()
data['type'] = 1 data['type'] = 1
return cls(data, session, token=bot_token) # type: ignore return cls(data, session, token=bot_token, state=state) # type: ignore # Casting dict[str, Any] to WebhookPayload
@classmethod @classmethod
def _as_follower(cls, data, *, channel, user) -> Self: def _as_follower(cls, data, *, channel, user) -> Self:

Loading…
Cancel
Save