File: action_row.py

package info (click to toggle)
python-discord 2.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,476 kB
  • sloc: python: 49,910; javascript: 363; makefile: 154
file content (592 lines) | stat: -rw-r--r-- 22,691 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
"""
The MIT License (MIT)

Copyright (c) 2015-present Rapptz

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations

from typing import (
    TYPE_CHECKING,
    Any,
    Callable,
    ClassVar,
    Coroutine,
    Dict,
    Generator,
    List,
    Literal,
    Optional,
    Sequence,
    Type,
    TypeVar,
    Union,
    overload,
)

from .item import Item, ContainedItemCallbackType as ItemCallbackType
from .button import Button, button as _button
from .select import select as _select, Select, UserSelect, RoleSelect, ChannelSelect, MentionableSelect
from ..components import ActionRow as ActionRowComponent
from ..enums import ButtonStyle, ComponentType, ChannelType
from ..partial_emoji import PartialEmoji
from ..utils import MISSING, get as _utils_get

if TYPE_CHECKING:
    from typing_extensions import Self

    from .view import LayoutView
    from .select import (
        BaseSelectT,
        ValidDefaultValues,
        MentionableSelectT,
        ChannelSelectT,
        RoleSelectT,
        UserSelectT,
        SelectT,
    )
    from ..emoji import Emoji
    from ..components import SelectOption
    from ..interactions import Interaction
    from .container import Container

    SelectCallbackDecorator = Callable[[ItemCallbackType['S', BaseSelectT]], BaseSelectT]

S = TypeVar('S', bound=Union['ActionRow', 'Container', 'LayoutView'], covariant=True)
V = TypeVar('V', bound='LayoutView', covariant=True)

__all__ = ('ActionRow',)


class _ActionRowCallback:
    __slots__ = ('row', 'callback', 'item')

    def __init__(self, callback: ItemCallbackType[S, Any], row: ActionRow, item: Item[Any]) -> None:
        self.callback: ItemCallbackType[Any, Any] = callback
        self.row: ActionRow = row
        self.item: Item[Any] = item

    def __call__(self, interaction: Interaction) -> Coroutine[Any, Any, Any]:
        return self.callback(self.row, interaction, self.item)


class ActionRow(Item[V]):
    r"""Represents a UI action row.

    This is a top-level layout component that can only be used on :class:`LayoutView`
    and can contain :class:`Button`\s and :class:`Select`\s in it.

    Action rows can only have 5 children. This can be inherited.

    .. versionadded:: 2.6

    Examples
    --------

    .. code-block:: python3

        import discord
        from discord import ui

        # you can subclass it and add components with the decorators
        class MyActionRow(ui.ActionRow):
            @ui.button(label='Click Me!')
            async def click_me(self, interaction: discord.Interaction, button: discord.ui.Button):
                await interaction.response.send_message('You clicked me!')

        # or use it directly on LayoutView
        class MyView(ui.LayoutView):
            row = ui.ActionRow()
            # or you can use your subclass:
            # row = MyActionRow()

            # you can add items with row.button and row.select
            @row.button(label='A button!')
            async def row_button(self, interaction: discord.Interaction, button: discord.ui.Button):
                await interaction.response.send_message('You clicked a button!')

    Parameters
    ----------
    \*children: :class:`Item`
        The initial children of this action row.
    id: Optional[:class:`int`]
        The ID of this component. This must be unique across the view.
    """

    __action_row_children_items__: ClassVar[List[ItemCallbackType[Self, Any]]] = []
    __discord_ui_action_row__: ClassVar[bool] = True
    __item_repr_attributes__ = ('id',)

    def __init__(
        self,
        *children: Item[V],
        id: Optional[int] = None,
    ) -> None:
        super().__init__()
        self._children: List[Item[V]] = self._init_children()
        self._children.extend(children)
        self._weight: int = sum(i.width for i in self._children)

        if self._weight > 5:
            raise ValueError('maximum number of children exceeded')

        self.id = id

    def __init_subclass__(cls) -> None:
        super().__init_subclass__()

        children: Dict[str, ItemCallbackType[Self, Any]] = {}
        for base in reversed(cls.__mro__):
            for name, member in base.__dict__.items():
                if hasattr(member, '__discord_ui_model_type__'):
                    children[name] = member

        if len(children) > 5:
            raise TypeError('ActionRow cannot have more than 5 children')

        cls.__action_row_children_items__ = list(children.values())

    def __repr__(self) -> str:
        return f'<{self.__class__.__name__} children={len(self._children)}>'

    def _init_children(self) -> List[Item[Any]]:
        children = []

        for func in self.__action_row_children_items__:
            item: Item = func.__discord_ui_model_type__(**func.__discord_ui_model_kwargs__)
            item.callback = _ActionRowCallback(func, self, item)  # type: ignore
            item._parent = getattr(func, '__discord_ui_parent__', self)
            setattr(self, func.__name__, item)
            children.append(item)
        return children

    def _update_view(self, view) -> None:
        self._view = view
        for child in self._children:
            child._view = view

    def _has_children(self):
        return True

    def _is_v2(self) -> bool:
        # although it is not really a v2 component the only usecase here is for
        # LayoutView which basically represents the top-level payload of components
        # and ActionRow is only allowed there anyways.
        # If the user tries to add any V2 component to a View instead of LayoutView
        # it should error anyways.
        return True

    @property
    def width(self):
        return 5

    @property
    def type(self) -> Literal[ComponentType.action_row]:
        return ComponentType.action_row

    @property
    def children(self) -> List[Item[V]]:
        """List[:class:`Item`]: The list of children attached to this action row."""
        return self._children.copy()

    def walk_children(self) -> Generator[Item[V], Any, None]:
        """An iterator that recursively walks through all the children of this action row
        and its children, if applicable.

        Yields
        ------
        :class:`Item`
            An item in the action row.
        """

        for child in self.children:
            yield child

    def content_length(self) -> int:
        """:class:`int`: Returns the total length of all text content in this action row."""
        from .text_display import TextDisplay

        return sum(len(item.content) for item in self._children if isinstance(item, TextDisplay))

    def add_item(self, item: Item[Any]) -> Self:
        """Adds an item to this action row.

        This function returns the class instance to allow for fluent-style
        chaining.

        Parameters
        ----------
        item: :class:`Item`
            The item to add to the action row.

        Raises
        ------
        TypeError
            An :class:`Item` was not passed.
        ValueError
            Maximum number of children has been exceeded (5)
            or (40) for the entire view.
        """

        if (self._weight + item.width) > 5:
            raise ValueError('maximum number of children exceeded')

        if len(self._children) >= 5:
            raise ValueError('maximum number of children exceeded')

        if not isinstance(item, Item):
            raise TypeError(f'expected Item not {item.__class__.__name__}')

        if self._view:
            self._view._add_count(1)

        item._update_view(self.view)
        item._parent = self
        self._weight += 1
        self._children.append(item)

        return self

    def remove_item(self, item: Item[Any]) -> Self:
        """Removes an item from the action row.

        This function returns the class instance to allow for fluent-style
        chaining.

        Parameters
        ----------
        item: :class:`Item`
            The item to remove from the action row.
        """

        try:
            self._children.remove(item)
        except ValueError:
            pass
        else:
            if self._view:
                self._view._add_count(-1)
            self._weight -= 1

        return self

    def find_item(self, id: int, /) -> Optional[Item[V]]:
        """Gets an item with :attr:`Item.id` set as ``id``, or ``None`` if
        not found.

        .. warning::

            This is **not the same** as ``custom_id``.

        Parameters
        ----------
        id: :class:`int`
            The ID of the component.

        Returns
        -------
        Optional[:class:`Item`]
            The item found, or ``None``.
        """
        return _utils_get(self.walk_children(), id=id)

    def clear_items(self) -> Self:
        """Removes all items from the action row.

        This function returns the class instance to allow for fluent-style
        chaining.
        """
        if self._view:
            self._view._add_count(-len(self._children))
        self._children.clear()
        self._weight = 0
        return self

    def to_component_dict(self) -> Dict[str, Any]:
        components = []
        for component in self.children:
            components.append(component.to_component_dict())

        base = {
            'type': self.type.value,
            'components': components,
        }
        if self.id is not None:
            base['id'] = self.id
        return base

    def button(
        self,
        *,
        label: Optional[str] = None,
        custom_id: Optional[str] = None,
        disabled: bool = False,
        style: ButtonStyle = ButtonStyle.secondary,
        emoji: Optional[Union[str, Emoji, PartialEmoji]] = None,
        id: Optional[int] = None,
    ) -> Callable[[ItemCallbackType[S, Button[V]]], Button[V]]:
        """A decorator that attaches a button to the action row.

        The function being decorated should have three parameters, ``self`` representing
        the :class:`discord.ui.ActionRow`, the :class:`discord.Interaction` you receive and
        the :class:`discord.ui.Button` being pressed.
        .. note::

            Buttons with a URL or a SKU cannot be created with this function.
            Consider creating a :class:`Button` manually and adding it via
            :meth:`ActionRow.add_item` instead. This is beacuse these buttons
            cannot have a callback associated with them since Discord does not
            do any processing with them.

        Parameters
        ----------
        label: Optional[:class:`str`]
            The label of the button, if any.
            Can only be up to 80 characters.
        custom_id: Optional[:class:`str`]
            The ID of the button that gets received during an interaction.
            It is recommended to not set this parameters to prevent conflicts.
            Can only be up to 100 characters.
        style: :class:`.ButtonStyle`
            The style of the button. Defaults to :attr:`.ButtonStyle.grey`.
        disabled: :class:`bool`
            Whether the button is disabled or not. Defaults to ``False``.
        emoji: Optional[Union[:class:`str`, :class:`.Emoji`, :class:`.PartialEmoji`]]
            The emoji of the button. This can be in string form or a :class:`.PartialEmoji`
            or a full :class:`.Emoji`.
        id: Optional[:class:`int`]
            The ID of the component. This must be unique across the view.

            .. versionadded:: 2.6
        """

        def decorator(func: ItemCallbackType[S, Button[V]]) -> ItemCallbackType[S, Button[V]]:
            ret = _button(
                label=label,
                custom_id=custom_id,
                disabled=disabled,
                style=style,
                emoji=emoji,
                row=None,
                id=id,
            )(func)
            ret.__discord_ui_parent__ = self  # type: ignore
            return ret  # type: ignore

        return decorator  # type: ignore

    @overload
    def select(
        self,
        *,
        cls: Type[SelectT] = Select[Any],
        options: List[SelectOption] = MISSING,
        channel_types: List[ChannelType] = ...,
        placeholder: Optional[str] = ...,
        custom_id: str = ...,
        min_values: int = ...,
        max_values: int = ...,
        disabled: bool = ...,
        id: Optional[int] = ...,
    ) -> SelectCallbackDecorator[S, SelectT]:
        ...

    @overload
    def select(
        self,
        *,
        cls: Type[UserSelectT] = UserSelect[Any],
        options: List[SelectOption] = MISSING,
        channel_types: List[ChannelType] = ...,
        placeholder: Optional[str] = ...,
        custom_id: str = ...,
        min_values: int = ...,
        max_values: int = ...,
        disabled: bool = ...,
        default_values: Sequence[ValidDefaultValues] = ...,
        id: Optional[int] = ...,
    ) -> SelectCallbackDecorator[S, UserSelectT]:
        ...

    @overload
    def select(
        self,
        *,
        cls: Type[RoleSelectT] = RoleSelect[Any],
        options: List[SelectOption] = MISSING,
        channel_types: List[ChannelType] = ...,
        placeholder: Optional[str] = ...,
        custom_id: str = ...,
        min_values: int = ...,
        max_values: int = ...,
        disabled: bool = ...,
        default_values: Sequence[ValidDefaultValues] = ...,
        id: Optional[int] = ...,
    ) -> SelectCallbackDecorator[S, RoleSelectT]:
        ...

    @overload
    def select(
        self,
        *,
        cls: Type[ChannelSelectT] = ChannelSelect[Any],
        options: List[SelectOption] = MISSING,
        channel_types: List[ChannelType] = ...,
        placeholder: Optional[str] = ...,
        custom_id: str = ...,
        min_values: int = ...,
        max_values: int = ...,
        disabled: bool = ...,
        default_values: Sequence[ValidDefaultValues] = ...,
        id: Optional[int] = ...,
    ) -> SelectCallbackDecorator[S, ChannelSelectT]:
        ...

    @overload
    def select(
        self,
        *,
        cls: Type[MentionableSelectT] = MentionableSelect[Any],
        options: List[SelectOption] = MISSING,
        channel_types: List[ChannelType] = MISSING,
        placeholder: Optional[str] = ...,
        custom_id: str = ...,
        min_values: int = ...,
        max_values: int = ...,
        disabled: bool = ...,
        default_values: Sequence[ValidDefaultValues] = ...,
        id: Optional[int] = ...,
    ) -> SelectCallbackDecorator[S, MentionableSelectT]:
        ...

    def select(
        self,
        *,
        cls: Type[BaseSelectT] = Select[Any],
        options: List[SelectOption] = MISSING,
        channel_types: List[ChannelType] = MISSING,
        placeholder: Optional[str] = None,
        custom_id: str = MISSING,
        min_values: int = 1,
        max_values: int = 1,
        disabled: bool = False,
        default_values: Sequence[ValidDefaultValues] = MISSING,
        id: Optional[int] = None,
    ) -> SelectCallbackDecorator[S, BaseSelectT]:
        """A decorator that attaches a select menu to the action row.

        The function being decorated should have three parameters, ``self`` representing
        the :class:`discord.ui.ActionRow`, the :class:`discord.Interaction` you receive and
        the chosen select class.

        To obtain the selected values inside the callback, you can use the ``values`` attribute of the chosen class in the callback. The list of values
        will depend on the type of select menu used. View the table below for more information.

        +----------------------------------------+-----------------------------------------------------------------------------------------------------------------+
        | Select Type                            | Resolved Values                                                                                                 |
        +========================================+=================================================================================================================+
        | :class:`discord.ui.Select`             | List[:class:`str`]                                                                                              |
        +----------------------------------------+-----------------------------------------------------------------------------------------------------------------+
        | :class:`discord.ui.UserSelect`         | List[Union[:class:`discord.Member`, :class:`discord.User`]]                                                     |
        +----------------------------------------+-----------------------------------------------------------------------------------------------------------------+
        | :class:`discord.ui.RoleSelect`         | List[:class:`discord.Role`]                                                                                     |
        +----------------------------------------+-----------------------------------------------------------------------------------------------------------------+
        | :class:`discord.ui.MentionableSelect`  | List[Union[:class:`discord.Role`, :class:`discord.Member`, :class:`discord.User`]]                              |
        +----------------------------------------+-----------------------------------------------------------------------------------------------------------------+
        | :class:`discord.ui.ChannelSelect`      | List[Union[:class:`~discord.app_commands.AppCommandChannel`, :class:`~discord.app_commands.AppCommandThread`]]  |
        +----------------------------------------+-----------------------------------------------------------------------------------------------------------------+

        Example
        ---------
        .. code-block:: python3

            class MyView(discord.ui.LayoutView):
                action_row = discord.ui.ActionRow()

                @action_row.select(cls=ChannelSelect, channel_types=[discord.ChannelType.text])
                async def select_channels(self, interaction: discord.Interaction, select: ChannelSelect):
                    return await interaction.response.send_message(f'You selected {select.values[0].mention}')

        Parameters
        ------------
        cls: Union[Type[:class:`discord.ui.Select`], Type[:class:`discord.ui.UserSelect`], Type[:class:`discord.ui.RoleSelect`], \
            Type[:class:`discord.ui.MentionableSelect`], Type[:class:`discord.ui.ChannelSelect`]]
            The class to use for the select menu. Defaults to :class:`discord.ui.Select`. You can use other
            select types to display different select menus to the user. See the table above for the different
            values you can get from each select type. Subclasses work as well, however the callback in the subclass will
            get overridden.
        placeholder: Optional[:class:`str`]
            The placeholder text that is shown if nothing is selected, if any.
            Can only be up to 150 characters.
        custom_id: :class:`str`
            The ID of the select menu that gets received during an interaction.
            It is recommended not to set this parameter to prevent conflicts.
            Can only be up to 100 characters.
        min_values: :class:`int`
            The minimum number of items that must be chosen for this select menu.
            Defaults to 1 and must be between 0 and 25.
        max_values: :class:`int`
            The maximum number of items that must be chosen for this select menu.
            Defaults to 1 and must be between 1 and 25.
        options: List[:class:`discord.SelectOption`]
            A list of options that can be selected in this menu. This can only be used with
            :class:`Select` instances.
            Can only contain up to 25 items.
        channel_types: List[:class:`~discord.ChannelType`]
            The types of channels to show in the select menu. Defaults to all channels. This can only be used
            with :class:`ChannelSelect` instances.
        disabled: :class:`bool`
            Whether the select is disabled or not. Defaults to ``False``.
        default_values: Sequence[:class:`~discord.abc.Snowflake`]
            A list of objects representing the default values for the select menu. This cannot be used with regular :class:`Select` instances.
            If ``cls`` is :class:`MentionableSelect` and :class:`.Object` is passed, then the type must be specified in the constructor.
            Number of items must be in range of ``min_values`` and ``max_values``.
        id: Optional[:class:`int`]
            The ID of the component. This must be unique across the view.

            .. versionadded:: 2.6
        """

        def decorator(func: ItemCallbackType[S, BaseSelectT]) -> ItemCallbackType[S, BaseSelectT]:
            r = _select(  # type: ignore
                cls=cls,  # type: ignore
                placeholder=placeholder,
                custom_id=custom_id,
                min_values=min_values,
                max_values=max_values,
                options=options,
                channel_types=channel_types,
                disabled=disabled,
                default_values=default_values,
                id=id,
            )(func)
            r.__discord_ui_parent__ = self
            return r

        return decorator  # type: ignore

    @classmethod
    def from_component(cls, component: ActionRowComponent) -> ActionRow:
        from .view import _component_to_item

        self = cls(id=component.id)
        for cmp in component.children:
            self.add_item(_component_to_item(cmp, self))
        return self