Skip to content

Commit

Permalink
Add id_chk to item inits
Browse files Browse the repository at this point in the history
  • Loading branch information
hypergonial committed Oct 31, 2023
1 parent a16e494 commit a310bf7
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 9 deletions.
11 changes: 11 additions & 0 deletions miru/abc/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,12 @@ def __init__(
position: t.Optional[int] = None,
width: int = 1,
disabled: bool = False,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
) -> None:
super().__init__(custom_id=custom_id, row=row, position=position, width=width)
self._handler: t.Optional[View] = None
self._disabled: bool = disabled
self._id_chk = id_chk or (lambda c: c == self.custom_id)

@property
def view(self) -> View:
Expand All @@ -172,6 +174,15 @@ def disabled(self, value: bool) -> None:
raise TypeError("Expected type 'bool' for property 'disabled'.")
self._disabled = value

@property
def id_chk(self) -> t.Callable[[str], bool]:
"""
A callable that determines if the button was \"pressed\".\\
It receives the custom_id to check as a parameter, and should return a boolean.\\
Defaults to checking if the item's custom_id equals the provided custom_id.
"""
return self._id_chk

@abstractmethod
def _build(self, action_row: hikari.api.MessageActionRowBuilder) -> None:
"""
Expand Down
9 changes: 8 additions & 1 deletion miru/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class Button(ViewItem):
The row the button should be in, leave as None for auto-placement.
position : Optional[int], optional
The position the button should be in within a row, leave as None for auto-placement.
id_chk : Optional[Callable[[str], bool]], optional
A callable that determines if the button was \"pressed\", by default None\\
It receives the custom_id to check as a parameter, and should return a boolean.\\
If None, the default check is used, which checks if the button's custom_id equals the provided custom_id.
Raises
------
Expand All @@ -57,8 +61,9 @@ def __init__(
emoji: t.Union[hikari.Emoji, str, None] = None,
row: t.Optional[int] = None,
position: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
) -> None:
super().__init__(custom_id=custom_id, row=row, position=position, disabled=disabled)
super().__init__(custom_id=custom_id, row=row, position=position, disabled=disabled, id_chk=id_chk)
self._emoji: t.Optional[hikari.Emoji] = hikari.Emoji.parse(emoji) if isinstance(emoji, str) else emoji
self.label = label
self.url = self._url = url
Expand Down Expand Up @@ -173,6 +178,7 @@ def button(
emoji: t.Optional[t.Union[str, hikari.Emoji]] = None,
row: t.Optional[int] = None,
disabled: bool = False,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
) -> t.Callable[[t.Callable[[ViewT, Button, ViewContext], t.Any]], Button]:
"""A decorator to transform a coroutine function into a Discord UI Button's callback.
This must be inside a subclass of View.
Expand Down Expand Up @@ -209,6 +215,7 @@ def decorator(func: t.Callable[..., t.Any]) -> t.Any:
row=row,
disabled=disabled,
url=None,
id_chk=id_chk,
)

return DecoratedItem(item, func)
Expand Down
38 changes: 31 additions & 7 deletions miru/ext/nav/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ def __init__(
position: t.Optional[int] = None,
disabled: bool = False,
width: int = 1,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
) -> None:
super().__init__(custom_id=custom_id, row=row, width=width, position=position, disabled=disabled)
super().__init__(custom_id=custom_id, row=row, width=width, position=position, disabled=disabled, id_chk=id_chk)
self._handler: t.Optional[NavigatorView] = None

async def before_page_change(self) -> None:
Expand Down Expand Up @@ -101,8 +102,11 @@ def __init__(
emoji: t.Union[hikari.Emoji, str, None] = chr(9654),
row: t.Optional[int] = None,
position: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
):
super().__init__(style=style, label=label, custom_id=custom_id, emoji=emoji, row=row, position=position)
super().__init__(
style=style, label=label, custom_id=custom_id, emoji=emoji, row=row, position=position, id_chk=id_chk
)

async def callback(self, context: ViewContext) -> None:
self.view.current_page += 1
Expand All @@ -129,8 +133,11 @@ def __init__(
emoji: t.Union[hikari.Emoji, str, None] = chr(9664),
row: t.Optional[int] = None,
position: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
):
super().__init__(style=style, label=label, custom_id=custom_id, emoji=emoji, row=row, position=position)
super().__init__(
style=style, label=label, custom_id=custom_id, emoji=emoji, row=row, position=position, id_chk=id_chk
)

async def callback(self, context: ViewContext) -> None:
self.view.current_page -= 1
Expand All @@ -157,8 +164,11 @@ def __init__(
emoji: t.Union[hikari.Emoji, str, None] = chr(9194),
row: t.Optional[int] = None,
position: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
):
super().__init__(style=style, label=label, custom_id=custom_id, emoji=emoji, row=row, position=position)
super().__init__(
style=style, label=label, custom_id=custom_id, emoji=emoji, row=row, position=position, id_chk=id_chk
)

async def callback(self, context: ViewContext) -> None:
self.view.current_page = 0
Expand All @@ -185,8 +195,11 @@ def __init__(
emoji: t.Union[hikari.Emoji, str, None] = chr(9193),
row: t.Optional[int] = None,
position: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
):
super().__init__(style=style, label=label, custom_id=custom_id, emoji=emoji, row=row, position=position)
super().__init__(
style=style, label=label, custom_id=custom_id, emoji=emoji, row=row, position=position, id_chk=id_chk
)

async def callback(self, context: ViewContext) -> None:
self.view.current_page = len(self.view.pages) - 1
Expand All @@ -213,10 +226,18 @@ def __init__(
disabled: bool = False,
row: t.Optional[int] = None,
position: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
):
# Either label or emoji is required, so we pass a placeholder
super().__init__(
style=style, label="0/0", custom_id=custom_id, emoji=emoji, disabled=disabled, row=row, position=position
style=style,
label="0/0",
custom_id=custom_id,
emoji=emoji,
disabled=disabled,
row=row,
position=position,
id_chk=id_chk,
)

async def before_page_change(self) -> None:
Expand Down Expand Up @@ -259,8 +280,11 @@ def __init__(
emoji: t.Union[hikari.Emoji, str, None] = chr(9209),
row: t.Optional[int] = None,
position: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
):
super().__init__(style=style, label=label, custom_id=custom_id, emoji=emoji, row=row, position=position)
super().__init__(
style=style, label=label, custom_id=custom_id, emoji=emoji, row=row, position=position, id_chk=id_chk
)

async def callback(self, context: ViewContext) -> None:
if not self.view.message and not self.view._inter:
Expand Down
7 changes: 6 additions & 1 deletion miru/select/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class SelectBase(ViewItem, abc.ABC):
A boolean determining if the select menu should be disabled or not, by default False
row : Optional[int], optional
The row the select menu should be in, leave as None for auto-placement.
id_chk : Optional[Callable[[str], bool]], optional
A callable that determines if the select was interacted with, by default None\\
It receives the custom_id to check as a parameter, and should return a boolean.\\
If None, the default check is used, which checks if the select's custom_id equals the provided custom_id.
Raises
------
Expand All @@ -44,8 +48,9 @@ def __init__(
max_values: int = 1,
disabled: bool = False,
row: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
) -> None:
super().__init__(custom_id=custom_id, row=row, position=0, width=5, disabled=disabled)
super().__init__(custom_id=custom_id, row=row, position=0, width=5, disabled=disabled, id_chk=id_chk)
self.placeholder = placeholder
self.min_values = min_values
self.max_values = max_values
Expand Down
8 changes: 8 additions & 0 deletions miru/select/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class ChannelSelect(SelectBase):
A boolean determining if the select menu should be disabled or not, by default False
row : Optional[int], optional
The row the select menu should be in, leave as None for auto-placement.
id_chk : Optional[Callable[[str], bool]], optional
A callable that determines if the select was interacted with, by default None\\
It receives the custom_id to check as a parameter, and should return a boolean.\\
If None, the default check is used, which checks if the select's custom_id equals the provided custom_id.
"""

def __init__(
Expand All @@ -49,6 +53,7 @@ def __init__(
max_values: int = 1,
disabled: bool = False,
row: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
) -> None:
super().__init__(
custom_id=custom_id,
Expand All @@ -57,6 +62,7 @@ def __init__(
max_values=max_values,
disabled=disabled,
row=row,
id_chk=id_chk,
)
self.channel_types = channel_types
self._values: t.Sequence[hikari.InteractionChannel] = []
Expand Down Expand Up @@ -123,6 +129,7 @@ def channel_select(
max_values: int = 1,
disabled: bool = False,
row: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
) -> t.Callable[[t.Callable[[ViewT, ChannelSelect, ViewContext], t.Any]], ChannelSelect]:
"""
A decorator to transform a function into a Discord UI ChannelSelectMenu's callback. This must be inside a subclass of View.
Expand All @@ -140,6 +147,7 @@ def decorator(func: t.Callable[..., t.Any]) -> t.Any:
max_values=max_values,
disabled=disabled,
row=row,
id_chk=id_chk,
)
return DecoratedItem(item, func)

Expand Down
8 changes: 8 additions & 0 deletions miru/select/mentionable.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class MentionableSelect(SelectBase):
A boolean determining if the select menu should be disabled or not, by default False
row : Optional[int], optional
The row the select menu should be in, leave as None for auto-placement.
id_chk : Optional[Callable[[str], bool]], optional
A callable that determines if the select was interacted with, by default None\\
It receives the custom_id to check as a parameter, and should return a boolean.\\
If None, the default check is used, which checks if the select's custom_id equals the provided custom_id.
"""

def __init__(
Expand All @@ -46,6 +50,7 @@ def __init__(
max_values: int = 1,
disabled: bool = False,
row: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
) -> None:
super().__init__(
custom_id=custom_id,
Expand All @@ -54,6 +59,7 @@ def __init__(
max_values=max_values,
disabled=disabled,
row=row,
id_chk=id_chk,
)
self._values = hikari.ResolvedOptionData(
attachments={}, channels={}, messages={}, members={}, roles={}, users={}
Expand Down Expand Up @@ -115,6 +121,7 @@ def mentionable_select(
max_values: int = 1,
disabled: bool = False,
row: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
) -> t.Callable[[t.Callable[[ViewT, MentionableSelect, ViewContext], t.Any]], MentionableSelect]:
"""
A decorator to transform a function into a Discord UI MentionableSelectMenu's callback.
Expand All @@ -132,6 +139,7 @@ def decorator(func: t.Callable[..., t.Any]) -> t.Any:
max_values=max_values,
disabled=disabled,
row=row,
id_chk=id_chk,
)
return DecoratedItem(item, func)

Expand Down
8 changes: 8 additions & 0 deletions miru/select/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class RoleSelect(SelectBase):
A boolean determining if the select menu should be disabled or not, by default False
row : Optional[int], optional
The row the select menu should be in, leave as None for auto-placement.
id_chk : Optional[Callable[[str], bool]], optional
A callable that determines if the select was interacted with, by default None\\
It receives the custom_id to check as a parameter, and should return a boolean.\\
If None, the default check is used, which checks if the select's custom_id equals the provided custom_id.
"""

def __init__(
Expand All @@ -46,6 +50,7 @@ def __init__(
max_values: int = 1,
disabled: bool = False,
row: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
) -> None:
super().__init__(
custom_id=custom_id,
Expand All @@ -54,6 +59,7 @@ def __init__(
max_values=max_values,
disabled=disabled,
row=row,
id_chk=id_chk,
)
self._values: t.Sequence[hikari.Role] = []

Expand Down Expand Up @@ -107,6 +113,7 @@ def role_select(
max_values: int = 1,
disabled: bool = False,
row: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
) -> t.Callable[[t.Callable[[ViewT, RoleSelect, ViewContext], t.Any]], RoleSelect]:
"""
A decorator to transform a function into a Discord UI RoleSelectMenu's callback. This must be inside a subclass of View.
Expand All @@ -123,6 +130,7 @@ def decorator(func: t.Callable[..., t.Any]) -> t.Any:
max_values=max_values,
disabled=disabled,
row=row,
id_chk=id_chk,
)
return DecoratedItem(item, func)

Expand Down
8 changes: 8 additions & 0 deletions miru/select/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ class TextSelect(SelectBase):
A boolean determining if the select menu should be disabled or not, by default False
row : Optional[int], optional
The row the select menu should be in, leave as None for auto-placement.
id_chk : Optional[Callable[[str], bool]], optional
A callable that determines if the select was interacted with, by default None\\
It receives the custom_id to check as a parameter, and should return a boolean.\\
If None, the default check is used, which checks if the select's custom_id equals the provided custom_id.
Raises
------
Expand All @@ -100,6 +104,7 @@ def __init__(
max_values: int = 1,
disabled: bool = False,
row: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
) -> None:
super().__init__(
custom_id=custom_id,
Expand All @@ -108,6 +113,7 @@ def __init__(
max_values=max_values,
disabled=disabled,
row=row,
id_chk=id_chk,
)
self._values: t.Sequence[str] = []
self.options = options
Expand Down Expand Up @@ -203,6 +209,7 @@ def text_select(
max_values: int = 1,
disabled: bool = False,
row: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
) -> t.Callable[[t.Callable[[ViewT, TextSelect, ViewContext], t.Any]], TextSelect]:
"""
A decorator to transform a function into a Discord UI TextSelectMenu's callback. This must be inside a subclass of View.
Expand All @@ -220,6 +227,7 @@ def decorator(func: t.Callable[..., t.Any]) -> t.Any:
max_values=max_values,
disabled=disabled,
row=row,
id_chk=id_chk,
)
return DecoratedItem(item, func)

Expand Down
8 changes: 8 additions & 0 deletions miru/select/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class UserSelect(SelectBase):
A boolean determining if the select menu should be disabled or not, by default False
row : Optional[int], optional
The row the select menu should be in, leave as None for auto-placement.
id_chk : Optional[Callable[[str], bool]], optional
A callable that determines if the select was interacted with, by default None\\
It receives the custom_id to check as a parameter, and should return a boolean.\\
If None, the default check is used, which checks if the select's custom_id equals the provided custom_id.
"""

def __init__(
Expand All @@ -46,6 +50,7 @@ def __init__(
max_values: int = 1,
disabled: bool = False,
row: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
) -> None:
super().__init__(
custom_id=custom_id,
Expand All @@ -54,6 +59,7 @@ def __init__(
max_values=max_values,
disabled=disabled,
row=row,
id_chk=id_chk,
)
self._values: t.Sequence[hikari.User] = []

Expand Down Expand Up @@ -115,6 +121,7 @@ def user_select(
max_values: int = 1,
disabled: bool = False,
row: t.Optional[int] = None,
id_chk: t.Optional[t.Callable[[str], bool]] = None,
) -> t.Callable[[t.Callable[[ViewT, UserSelect, ViewContext], t.Any]], UserSelect]:
"""
A decorator to transform a function into a Discord UI UserSelectMenu's callback. This must be inside a subclass of View.
Expand All @@ -131,6 +138,7 @@ def decorator(func: t.Callable[..., t.Any]) -> t.Any:
max_values=max_values,
disabled=disabled,
row=row,
id_chk=id_chk,
)
return DecoratedItem(item, func)

Expand Down

0 comments on commit a310bf7

Please sign in to comment.