Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Improve webhook typing. #1193

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/626.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve type annotations for :attr:`Interaction.followup`\'s ``.send`` method.
6 changes: 4 additions & 2 deletions disnake/interactions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
overload,
)

from disnake.webhook.interaction import InteractionFollowupWebhook
elenakrittik marked this conversation as resolved.
Show resolved Hide resolved

from .. import utils
from ..app_commands import OptionChoice
from ..channel import PartialMessageable, _threaded_guild_channel_factory
Expand Down Expand Up @@ -310,14 +312,14 @@ def response(self) -> InteractionResponse:
return InteractionResponse(self)

@utils.cached_slot_property("_cs_followup")
def followup(self) -> Webhook:
def followup(self) -> InteractionFollowupWebhook:
""":class:`Webhook`: Returns the follow up webhook for follow up interactions."""
payload = {
"id": self.application_id,
"type": WebhookType.application.value,
"token": self.token,
}
return Webhook.from_state(data=payload, state=self._state)
return Webhook.from_state(data=payload, state=self._state) # type: ignore

@utils.cached_slot_property("_cs_expires_at")
def expires_at(self) -> datetime:
Expand Down
5 changes: 4 additions & 1 deletion disnake/webhook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
:license: MIT, see LICENSE for more details.

"""
from . import async_, sync

from . import async_, interaction, sync
from .async_ import *
from .interaction import *
from .sync import *

__all__ = []
__all__.extend(async_.__all__)
__all__.extend(interaction.__all__)
__all__.extend(sync.__all__)
76 changes: 76 additions & 0 deletions disnake/webhook/interaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# SPDX-License-Identifier: MIT

from __future__ import annotations

from collections.abc import Sequence
from typing import List, Optional

from disnake.abc import Snowflake
from disnake.embeds import Embed
from disnake.file import File
from disnake.flags import MessageFlags
from disnake.mentions import AllowedMentions
from disnake.ui.action_row import Components, MessageUIComponent
from disnake.ui.view import View
from disnake.utils import MISSING
from disnake.webhook.async_ import Webhook, WebhookMessage
elenakrittik marked this conversation as resolved.
Show resolved Hide resolved

__all__ = ("InteractionFollowupWebhook",)


class InteractionFollowupWebhook(Webhook):
"""A 1:1 copy of :class:`Webhook` meant for :attr:`Interaction.followup`\\'s annotations."""

async def send(
self,
content: Optional[str] = MISSING,
*,
tts: bool = MISSING,
ephemeral: bool = MISSING,
suppress_embeds: bool = MISSING,
flags: MessageFlags = MISSING,
file: File = MISSING,
files: List[File] = MISSING,
embed: Embed = MISSING,
embeds: List[Embed] = MISSING,
allowed_mentions: AllowedMentions = MISSING,
view: View = MISSING,
components: Components[MessageUIComponent] = MISSING,
thread: Snowflake = MISSING,
thread_name: str = MISSING,
applied_tags: Sequence[Snowflake] = MISSING,
delete_after: float = MISSING,
) -> WebhookMessage:
"""|coro|

Sends a message using the webhook.

This is the same as :meth:`Webhook.send` but with type hints changed. Namely,
the return type is :class:`WebhookMessage` instead of :class:`Message` because
``wait=True`` for interaction webhooks, and ``username`` and ``avatar_url`` are
not supported.

Returns
-------
:class:`WebhookMessage`
The message that was sent.
"""
return await super().send(
content=content,
tts=tts,
ephemeral=ephemeral,
embeds=embeds,
embed=embed,
file=file,
files=files,
view=view,
components=components,
allowed_mentions=allowed_mentions,
thread=thread,
thread_name=thread_name,
applied_tags=applied_tags,
delete_after=delete_after,
suppress_embeds=suppress_embeds,
flags=flags,
wait=True,
)
Loading