2025-03-09 11:23:13 +01:00
|
|
|
# Copyright (c) Paillat-dev
|
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
|
2024-08-03 15:14:44 +02:00
|
|
|
from typing import Self
|
2024-08-02 15:13:21 +02:00
|
|
|
|
|
|
|
|
import discord
|
2024-08-03 15:59:36 +02:00
|
|
|
from typing_extensions import override
|
2024-08-02 15:13:21 +02:00
|
|
|
|
2024-08-03 15:59:36 +02:00
|
|
|
from .components import Reactive
|
2024-08-02 15:13:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
2025-03-09 11:23:13 +01:00
|
|
|
) -> None:
|
|
|
|
|
super().__init__(timeout=timeout, disable_on_timeout=disable_on_timeout) # pyright: ignore[reportUnknownMemberType]
|
2024-08-03 15:59:36 +02:00
|
|
|
self._reactives: list[Reactive] = []
|
2025-03-29 16:24:37 +01:00
|
|
|
self.ctx: discord.ApplicationContext | discord.Interaction | None = None
|
2024-08-02 15:13:21 +02:00
|
|
|
|
2024-08-03 15:59:36 +02:00
|
|
|
@override
|
2024-08-02 15:13:21 +02:00
|
|
|
def add_item(self, item: discord.ui.Item[Self]) -> None:
|
2024-08-03 15:59:36 +02:00
|
|
|
if isinstance(item, Reactive):
|
2024-08-02 15:13:21 +02:00
|
|
|
self._reactives.append(item)
|
2025-03-09 11:23:13 +01:00
|
|
|
super().add_item(item) # pyright: ignore [reportUnknownMemberType]
|
2024-08-02 15:13:21 +02:00
|
|
|
|
|
|
|
|
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."""
|
2024-08-03 15:27:07 +02:00
|
|
|
if embed := await self._get_embed():
|
|
|
|
|
return [embed]
|
2024-08-02 15:13:21 +02:00
|
|
|
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()
|
2025-03-29 16:24:37 +01:00
|
|
|
editable = self.ctx or self.message
|
|
|
|
|
if not editable:
|
|
|
|
|
raise ValueError("View has no editable (not yet sent?), can't refresh")
|
2024-08-02 15:13:21 +02:00
|
|
|
if embeds := await self._get_embeds():
|
2025-03-29 16:32:35 +01:00
|
|
|
await editable.edit(content=await self._get_content(), embeds=embeds, view=self) # pyright: ignore[reportUnknownMemberType]
|
2024-08-02 15:13:21 +02:00
|
|
|
else:
|
2025-03-29 16:32:35 +01:00
|
|
|
await editable.edit(content=await self._get_content(), view=self) # pyright: ignore[reportUnknownMemberType]
|
2024-08-02 15:13:21 +02:00
|
|
|
|
2025-03-29 16:24:37 +01:00
|
|
|
async def send(self, ctx: discord.ApplicationContext | discord.Interaction, ephemeral: bool = False) -> None:
|
2024-08-02 15:13:21 +02:00
|
|
|
"""Send the view to a context."""
|
2025-03-29 16:24:37 +01:00
|
|
|
self.ctx = ctx
|
2024-08-02 15:13:21 +02:00
|
|
|
if embeds := await self._get_embeds():
|
2025-03-29 16:24:37 +01:00
|
|
|
await ctx.respond(content=await self._get_content(), embeds=embeds, view=self, ephemeral=ephemeral) # pyright: ignore [reportUnknownMemberType]
|
2024-08-02 15:13:21 +02:00
|
|
|
else:
|
2025-03-29 16:24:37 +01:00
|
|
|
await ctx.respond(content=await self._get_content(), view=self, ephemeral=ephemeral) # pyright: ignore [reportUnknownMemberType]
|