Skip to content

Commit 50caa3c

Browse files
DA-344mikeshardmindSoheabowocadojay3332
authored
Add support for components V2
Co-authored-by: Michael H <[email protected]> Co-authored-by: Soheab <[email protected]> Co-authored-by: owocado <[email protected]> Co-authored-by: Jay3332 <[email protected]> Co-authored-by: Danny <[email protected]>
1 parent 6ec2e53 commit 50caa3c

33 files changed

+4214
-298
lines changed

discord/abc.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
)
9797
from .poll import Poll
9898
from .threads import Thread
99-
from .ui.view import View
99+
from .ui.view import BaseView, View, LayoutView
100100
from .types.channel import (
101101
PermissionOverwrite as PermissionOverwritePayload,
102102
Channel as ChannelPayload,
@@ -1386,6 +1386,38 @@ class Messageable:
13861386
async def _get_channel(self) -> MessageableChannel:
13871387
raise NotImplementedError
13881388

1389+
@overload
1390+
async def send(
1391+
self,
1392+
*,
1393+
file: File = ...,
1394+
delete_after: float = ...,
1395+
nonce: Union[str, int] = ...,
1396+
allowed_mentions: AllowedMentions = ...,
1397+
reference: Union[Message, MessageReference, PartialMessage] = ...,
1398+
mention_author: bool = ...,
1399+
view: LayoutView,
1400+
suppress_embeds: bool = ...,
1401+
silent: bool = ...,
1402+
) -> Message:
1403+
...
1404+
1405+
@overload
1406+
async def send(
1407+
self,
1408+
*,
1409+
files: Sequence[File] = ...,
1410+
delete_after: float = ...,
1411+
nonce: Union[str, int] = ...,
1412+
allowed_mentions: AllowedMentions = ...,
1413+
reference: Union[Message, MessageReference, PartialMessage] = ...,
1414+
mention_author: bool = ...,
1415+
view: LayoutView,
1416+
suppress_embeds: bool = ...,
1417+
silent: bool = ...,
1418+
) -> Message:
1419+
...
1420+
13891421
@overload
13901422
async def send(
13911423
self,
@@ -1485,7 +1517,7 @@ async def send(
14851517
allowed_mentions: Optional[AllowedMentions] = None,
14861518
reference: Optional[Union[Message, MessageReference, PartialMessage]] = None,
14871519
mention_author: Optional[bool] = None,
1488-
view: Optional[View] = None,
1520+
view: Optional[BaseView] = None,
14891521
suppress_embeds: bool = False,
14901522
silent: bool = False,
14911523
poll: Optional[Poll] = None,
@@ -1558,7 +1590,7 @@ async def send(
15581590
If set, overrides the :attr:`~discord.AllowedMentions.replied_user` attribute of ``allowed_mentions``.
15591591
15601592
.. versionadded:: 1.6
1561-
view: :class:`discord.ui.View`
1593+
view: Union[:class:`discord.ui.View`, :class:`discord.ui.LayoutView`]
15621594
A Discord UI View to add to the message.
15631595
15641596
.. versionadded:: 2.0
@@ -1656,7 +1688,7 @@ async def send(
16561688
data = await state.http.send_message(channel.id, params=params)
16571689

16581690
ret = state.create_message(channel=channel, data=data)
1659-
if view and not view.is_finished():
1691+
if view and not view.is_finished() and view.is_dispatchable():
16601692
state.store_view(view, ret.id)
16611693

16621694
if poll:

discord/channel.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
from .file import File
101101
from .user import ClientUser, User, BaseUser
102102
from .guild import Guild, GuildChannel as GuildChannelType
103-
from .ui.view import View
103+
from .ui.view import BaseView, View, LayoutView
104104
from .types.channel import (
105105
TextChannel as TextChannelPayload,
106106
NewsChannel as NewsChannelPayload,
@@ -2841,6 +2841,47 @@ async def create_tag(
28412841

28422842
return result
28432843

2844+
@overload
2845+
async def create_thread(
2846+
self,
2847+
*,
2848+
name: str,
2849+
auto_archive_duration: ThreadArchiveDuration = ...,
2850+
slowmode_delay: Optional[int] = ...,
2851+
file: File = ...,
2852+
files: Sequence[File] = ...,
2853+
allowed_mentions: AllowedMentions = ...,
2854+
mention_author: bool = ...,
2855+
applied_tags: Sequence[ForumTag] = ...,
2856+
view: LayoutView,
2857+
suppress_embeds: bool = ...,
2858+
reason: Optional[str] = ...,
2859+
) -> ThreadWithMessage:
2860+
...
2861+
2862+
@overload
2863+
async def create_thread(
2864+
self,
2865+
*,
2866+
name: str,
2867+
auto_archive_duration: ThreadArchiveDuration = ...,
2868+
slowmode_delay: Optional[int] = ...,
2869+
content: Optional[str] = ...,
2870+
tts: bool = ...,
2871+
embed: Embed = ...,
2872+
embeds: Sequence[Embed] = ...,
2873+
file: File = ...,
2874+
files: Sequence[File] = ...,
2875+
stickers: Sequence[Union[GuildSticker, StickerItem]] = ...,
2876+
allowed_mentions: AllowedMentions = ...,
2877+
mention_author: bool = ...,
2878+
applied_tags: Sequence[ForumTag] = ...,
2879+
view: View = ...,
2880+
suppress_embeds: bool = ...,
2881+
reason: Optional[str] = ...,
2882+
) -> ThreadWithMessage:
2883+
...
2884+
28442885
async def create_thread(
28452886
self,
28462887
*,
@@ -2857,7 +2898,7 @@ async def create_thread(
28572898
allowed_mentions: AllowedMentions = MISSING,
28582899
mention_author: bool = MISSING,
28592900
applied_tags: Sequence[ForumTag] = MISSING,
2860-
view: View = MISSING,
2901+
view: BaseView = MISSING,
28612902
suppress_embeds: bool = False,
28622903
reason: Optional[str] = None,
28632904
) -> ThreadWithMessage:
@@ -2907,7 +2948,7 @@ async def create_thread(
29072948
If set, overrides the :attr:`~discord.AllowedMentions.replied_user` attribute of ``allowed_mentions``.
29082949
applied_tags: List[:class:`discord.ForumTag`]
29092950
A list of tags to apply to the thread.
2910-
view: :class:`discord.ui.View`
2951+
view: Union[:class:`discord.ui.View`, :class:`discord.ui.LayoutView`]
29112952
A Discord UI View to add to the message.
29122953
stickers: Sequence[Union[:class:`~discord.GuildSticker`, :class:`~discord.StickerItem`]]
29132954
A list of stickers to upload. Must be a maximum of 3.
@@ -2983,7 +3024,7 @@ async def create_thread(
29833024
data = await state.http.start_thread_in_forum(self.id, params=params, reason=reason)
29843025
thread = Thread(guild=self.guild, state=self._state, data=data)
29853026
message = Message(state=self._state, channel=thread, data=data['message'])
2986-
if view and not view.is_finished():
3027+
if view and not view.is_finished() and view.is_dispatchable():
29873028
self._state.store_view(view, message.id)
29883029

29893030
return ThreadWithMessage(thread=thread, message=message)

discord/client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
from .backoff import ExponentialBackoff
7373
from .webhook import Webhook
7474
from .appinfo import AppInfo
75-
from .ui.view import View
75+
from .ui.view import BaseView
7676
from .ui.dynamic import DynamicItem
7777
from .stage_instance import StageInstance
7878
from .threads import Thread
@@ -3156,7 +3156,7 @@ def remove_dynamic_items(self, *items: Type[DynamicItem[Item[Any]]]) -> None:
31563156

31573157
self._connection.remove_dynamic_items(*items)
31583158

3159-
def add_view(self, view: View, *, message_id: Optional[int] = None) -> None:
3159+
def add_view(self, view: BaseView, *, message_id: Optional[int] = None) -> None:
31603160
"""Registers a :class:`~discord.ui.View` for persistent listening.
31613161
31623162
This method should be used for when a view is comprised of components
@@ -3166,7 +3166,7 @@ def add_view(self, view: View, *, message_id: Optional[int] = None) -> None:
31663166
31673167
Parameters
31683168
------------
3169-
view: :class:`discord.ui.View`
3169+
view: Union[:class:`discord.ui.View`, :class:`discord.ui.LayoutView`]
31703170
The view to register for dispatching.
31713171
message_id: Optional[:class:`int`]
31723172
The message ID that the view is attached to. This is currently used to
@@ -3182,7 +3182,7 @@ def add_view(self, view: View, *, message_id: Optional[int] = None) -> None:
31823182
and all their components have an explicitly provided custom_id.
31833183
"""
31843184

3185-
if not isinstance(view, View):
3185+
if not isinstance(view, BaseView):
31863186
raise TypeError(f'expected an instance of View not {view.__class__.__name__}')
31873187

31883188
if not view.is_persistent():
@@ -3194,8 +3194,8 @@ def add_view(self, view: View, *, message_id: Optional[int] = None) -> None:
31943194
self._connection.store_view(view, message_id)
31953195

31963196
@property
3197-
def persistent_views(self) -> Sequence[View]:
3198-
"""Sequence[:class:`.View`]: A sequence of persistent views added to the client.
3197+
def persistent_views(self) -> Sequence[BaseView]:
3198+
"""Sequence[Union[:class:`.View`, :class:`.LayoutView`]]: A sequence of persistent views added to the client.
31993199
32003200
.. versionadded:: 2.0
32013201
"""

0 commit comments

Comments
 (0)