👷 Work in progress

This commit is contained in:
2024-08-02 15:13:21 +02:00
parent 95dc716ede
commit f463824261
14 changed files with 443 additions and 35 deletions

View File

@@ -0,0 +1,5 @@
from .components import ReactiveButton
from .utils import ReactiveValue
from .view import ReactiveView
__all__ = ["ReactiveButton", "ReactiveView", "ReactiveValue"]

View File

@@ -0,0 +1,53 @@
from typing import Any
import discord
from .utils import MaybeReactiveValue, ReactiveValue, is_reactive
class Reactive:
"""A class that can be used with reactive values."""
def __init__(self):
super().__init__()
self.reactives: dict[str, ReactiveValue[Any]] = {}
self.super_kwargs: dict[str, Any] = {}
def add_reactive(self, key: str, value: MaybeReactiveValue[Any]) -> None:
"""Add a reactive value to the view."""
if is_reactive(value):
self.reactives[key] = value
if value.default:
setattr(self, key, value.default)
else:
setattr(self, key, value)
async def refresh(self) -> None:
"""Refresh the reactive values."""
for key, value in self.reactives.items():
setattr(self, key, await value())
class ReactiveButton(discord.ui.Button, Reactive): # pyright: ignore[reportUnsafeMultipleInheritance,reportMissingTypeArgument]
"""A button that can be used with reactive values."""
def __init__(
self,
*,
style: MaybeReactiveValue[discord.ButtonStyle] = discord.ButtonStyle.secondary,
label: MaybeReactiveValue[str | None] = None,
disabled: MaybeReactiveValue[bool] = False,
custom_id: str | None = None,
url: MaybeReactiveValue[str | None] = None,
emoji: MaybeReactiveValue[str | discord.Emoji | discord.PartialEmoji | None] = None,
sku_id: int | None = None,
row: MaybeReactiveValue[int | None] = None,
):
discord.ui.Button.__init__(self)
Reactive.__init__(self)
self.add_reactive("style", style)
self.add_reactive("label", label)
self.add_reactive("disabled", disabled)
self.add_reactive("url", url)
self.add_reactive("emoji", emoji)
self.add_reactive("row", row)

View File

@@ -0,0 +1,3 @@
from .reactivity import MaybeReactiveValue, ReactiveValue, is_reactive
__all__ = ["ReactiveValue", "MaybeReactiveValue", "is_reactive"]

View File

@@ -0,0 +1,44 @@
from collections.abc import Awaitable, Callable
from inspect import isawaitable
from typing import TypeGuard, TypeVar
T = TypeVar("T")
class Unset:
"""A class to represent an unset value."""
def __bool__(self):
return False
UNSET = Unset()
class ReactiveValue[T]:
"""A value that can be a constant, a callable, or an async callable."""
def __init__(self, func: Callable[[], T] | Callable[[], Awaitable[T]], default: T | Unset = UNSET):
"""Create a new reactive value."""
self._func: Callable[[], T] | Callable[[], Awaitable[T]] = func
self.default = default
async def __call__(self) -> T:
"""Call the function and return the value.
:raises TypeError: If the value is not callable
"""
if callable(self._func):
ret = self._func()
if isawaitable(ret):
return await ret
return ret
raise TypeError("ReactiveValue must be a callable")
MaybeReactiveValue = T | ReactiveValue[T]
def is_reactive(value: MaybeReactiveValue[T]) -> TypeGuard[ReactiveValue[T]]:
"""Check if a value is a reactive value."""
return isinstance(value, ReactiveValue)

View File

@@ -0,0 +1,57 @@
from typing import Self, override
import discord
from .components import ReactiveButton
class ReactiveView(discord.ui.View):
"""A view that can be used with reactive components."""
def __init__(
self,
*,
timeout: float | None = 180.0,
disable_on_timeout: bool = False,
):
super().__init__(timeout=timeout, disable_on_timeout=disable_on_timeout)
self._reactives: list[ReactiveButton] = []
@override
def add_item(self, item: discord.ui.Item[Self]) -> None:
if isinstance(item, ReactiveButton):
self._reactives.append(item)
super().add_item(item)
async def _get_embed(self) -> discord.Embed | None:
"""Get the discord embed to be displayed in the message."""
return None
async def _get_embeds(self) -> list[discord.Embed]:
"""Get the discord embeds to be displayed in the message."""
return []
async def _get_content(self) -> str | None:
"""Get the content to be displayed in the message."""
return None
async def update(self) -> None:
"""Update the view with new components.
:raises ValueError: If the view has no message (not yet sent?), can't update
"""
for reactive in self._reactives:
await reactive.refresh()
if not self.message:
raise ValueError("View has no message (not yet sent?), can't refresh")
if embeds := await self._get_embeds():
await self.message.edit(content=await self._get_content(), embeds=embeds, view=self)
else:
await self.message.edit(content=await self._get_content(), view=self)
async def send(self, ctx: discord.ApplicationContext | discord.Interaction) -> None:
"""Send the view to a context."""
if embeds := await self._get_embeds():
await ctx.respond(content=await self._get_content(), embeds=embeds, view=self)
else:
await ctx.respond(content=await self._get_content(), view=self)