|
|
|
@@ -29,6 +29,7 @@ config-free: no host config import; everything is passed at construction.
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
import logging
|
|
|
|
|
from typing import (
|
|
|
|
|
Any,
|
|
|
|
|
Dict,
|
|
|
|
@@ -69,6 +70,8 @@ DEFAULT_EMOJIS = {
|
|
|
|
|
"cache": "\U0001f5c2\ufe0f", # 🗂️
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
PageT_co = TypeVar("PageT_co", bound=Page, covariant=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -161,6 +164,10 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
|
|
|
|
|
super().__init__(timeout=timeout)
|
|
|
|
|
if not pages:
|
|
|
|
|
raise ValueError("ButtonPaginator requires at least one page")
|
|
|
|
|
if per_page < 1:
|
|
|
|
|
# per_page <= 0 would ZeroDivisionError (==0) or yield a negative max_pages
|
|
|
|
|
# (<0) at the divmod below; fail loud like the other construction guards
|
|
|
|
|
raise ValueError("per_page must be >= 1")
|
|
|
|
|
self.author_id: Optional[int] = author_id
|
|
|
|
|
self.delete_message_after: bool = delete_message_after
|
|
|
|
|
self.mentions_allowed = mentions_allowed or discord.AllowedMentions.all()
|
|
|
|
@@ -176,7 +183,9 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
|
|
|
|
|
|
|
|
|
|
total_pages, left_over = divmod(len(self.pages), self.per_page)
|
|
|
|
|
self.max_pages: int = total_pages + (1 if left_over else 0)
|
|
|
|
|
if cache is not None and len(cache) < self.max_pages:
|
|
|
|
|
# a falsy cache (None or []) disables the cache button (see the render path's
|
|
|
|
|
# `if self.cache:`); only a non-empty cache must have one entry per page
|
|
|
|
|
if cache and len(cache) < self.max_pages:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"cache has {len(cache)} entries but there are {self.max_pages} pages; "
|
|
|
|
|
"cache needs one entry per page"
|
|
|
|
@@ -200,7 +209,7 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
|
|
|
|
|
|
|
|
|
|
async def interaction_check(self, interaction: Interaction) -> bool:
|
|
|
|
|
"""restrict interaction to author_id when set"""
|
|
|
|
|
if not self.author_id or self.author_id == interaction.user.id:
|
|
|
|
|
if self.author_id is None or self.author_id == interaction.user.id:
|
|
|
|
|
return True
|
|
|
|
|
await interaction.response.send_message("You cannot interact with this menu.", ephemeral=True)
|
|
|
|
|
return False
|
|
|
|
@@ -259,6 +268,14 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
|
|
|
|
|
if isinstance(value, discord.Attachment):
|
|
|
|
|
value = await value.to_file()
|
|
|
|
|
self._page_kwargs["files"].append(value)
|
|
|
|
|
elif key in ("embed", "embeds", "file", "files"):
|
|
|
|
|
# a wrong-typed embed/file would otherwise fall to the catch-all and
|
|
|
|
|
# be forwarded verbatim, colliding with the base embeds=[]/files=[]
|
|
|
|
|
# and raising an opaque TypeError from inside discord.py — reject it
|
|
|
|
|
# here with a clear, paginator-side message
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"page key {key!r} has unexpected type {type(value).__name__}"
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
self._page_kwargs[key] = value
|
|
|
|
|
elif isinstance(formatted_page, str):
|
|
|
|
@@ -280,27 +297,38 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
|
|
|
|
|
|
|
|
|
|
return self._page_kwargs
|
|
|
|
|
|
|
|
|
|
def update_buttons(self) -> None:
|
|
|
|
|
"""rebuild the action row for the current page state"""
|
|
|
|
|
def update_buttons(self, nav: bool = True) -> None:
|
|
|
|
|
"""rebuild the action row for the current page state
|
|
|
|
|
|
|
|
|
|
nav=False rebuilds with only the page's custom buttons and no navigation
|
|
|
|
|
items (prev/jump/cache/next) — a single-page result that still carries
|
|
|
|
|
custom buttons keeps them (and their live callbacks) without a nav row.
|
|
|
|
|
the nav row is also suppressed for a single page (max_pages < 2) even when
|
|
|
|
|
nav is left at its default, so a re-render (update_page) never resurrects it.
|
|
|
|
|
"""
|
|
|
|
|
self.clear_items()
|
|
|
|
|
|
|
|
|
|
self.previous_page.emoji = self.emojis["previous"]
|
|
|
|
|
self.previous_page.disabled = self.current_page <= 0
|
|
|
|
|
self.add_item(self.previous_page)
|
|
|
|
|
nav = nav and self.max_pages >= 2
|
|
|
|
|
|
|
|
|
|
self.jump_button.label = self.page_text.format(self.current_page + 1, self.max_pages)
|
|
|
|
|
self.add_item(self.jump_button)
|
|
|
|
|
if nav:
|
|
|
|
|
self.previous_page.emoji = self.emojis["previous"]
|
|
|
|
|
self.previous_page.disabled = self.current_page <= 0
|
|
|
|
|
self.add_item(self.previous_page)
|
|
|
|
|
|
|
|
|
|
self.jump_button.label = self.page_text.format(self.current_page + 1, self.max_pages)
|
|
|
|
|
self.add_item(self.jump_button)
|
|
|
|
|
|
|
|
|
|
for button in self.current_page_buttons:
|
|
|
|
|
self.add_item(button)
|
|
|
|
|
|
|
|
|
|
if self.cache:
|
|
|
|
|
self.cache_button.emoji = self.emojis["cache"]
|
|
|
|
|
self.add_item(self.cache_button)
|
|
|
|
|
if nav:
|
|
|
|
|
if self.cache:
|
|
|
|
|
self.cache_button.emoji = self.emojis["cache"]
|
|
|
|
|
self.add_item(self.cache_button)
|
|
|
|
|
|
|
|
|
|
self.next_page.emoji = self.emojis["next"]
|
|
|
|
|
self.next_page.disabled = self.current_page >= self.max_pages - 1
|
|
|
|
|
self.add_item(self.next_page)
|
|
|
|
|
self.next_page.emoji = self.emojis["next"]
|
|
|
|
|
self.next_page.disabled = self.current_page >= self.max_pages - 1
|
|
|
|
|
self.add_item(self.next_page)
|
|
|
|
|
|
|
|
|
|
async def _build_render_kwargs(self) -> Dict[str, Any]:
|
|
|
|
|
"""build the edit-ready kwargs for the current page (buttons + attachments)"""
|
|
|
|
@@ -367,9 +395,13 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
|
|
|
|
|
) -> Optional[Union[discord.Message, discord.WebhookMessage]]:
|
|
|
|
|
"""send the first page; obj is an Interaction or a Messageable"""
|
|
|
|
|
kwargs = await self.get_page_kwargs(self.get_page(self.current_page))
|
|
|
|
|
# update_buttons already suppresses the nav row for a single page (max_pages < 2),
|
|
|
|
|
# keeping only the page's custom buttons — so a single page with custom buttons
|
|
|
|
|
# renders them (live callbacks) with no nav row
|
|
|
|
|
self.update_buttons()
|
|
|
|
|
|
|
|
|
|
if self.max_pages < 2:
|
|
|
|
|
if self.max_pages < 2 and not self.current_page_buttons:
|
|
|
|
|
# single page, no custom buttons: no interactive row at all, drop the view
|
|
|
|
|
self.stop()
|
|
|
|
|
kwargs.pop("view", None)
|
|
|
|
|
|
|
|
|
@@ -402,3 +434,7 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
|
|
|
|
|
await self.message.delete()
|
|
|
|
|
except (discord.NotFound, discord.Forbidden):
|
|
|
|
|
pass
|
|
|
|
|
except discord.HTTPException:
|
|
|
|
|
# on_timeout runs as a fire-and-forget task; a transient delete failure must
|
|
|
|
|
# not surface as an unretrieved-task traceback on a best-effort cleanup
|
|
|
|
|
log.warning("paginator on_timeout: failed to delete message", exc_info=True)
|
|
|
|
|