5 Commits
Author SHA1 Message Date
dsql 20ea4f9291 fix: single page keeps custom buttons instead of dropping the view (v0.1.2)
a single-page result (max_pages < 2) suppressed the navigation row by dropping the
whole view, which also discarded the consumer's custom per-page buttons. now: if the
page carries custom buttons, keep the view and rebuild with update_buttons(nav=False)
— nav items suppressed, custom buttons kept, and stop() NOT called so their callbacks
still fire. a page with no custom buttons keeps the original drop-the-view behavior.

verified by execution against real discord.py: single page + custom button -> start()
-> callback FIRES on click (view kept, stop() not called); negative control on the old
code drops the button entirely; the no-button single-page case is unregressed.

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 17:26:04 -04:00
dsql bca6b874c6 fix: only pass ephemeral on interaction paths, not raw-channel send
start() put ephemeral into send_kwargs unconditionally, so the raw Messageable.send() path raised TypeError (discord.py's channel send has no ephemeral param). ephemeral is now passed only on the interaction-response paths (followup.send / response.send_message), which support it; raw-channel send never receives it.

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 01:10:25 -04:00
dsql f6caa6f016 fix: wire on_timeout so delete_message_after actually deletes
delete_message_after was stored but never read and no on_timeout override existed, so the documented 'delete the message on timeout' never happened. added on_timeout that deletes self.message when the flag is set, swallowing only the expected discord.NotFound (already deleted) / discord.Forbidden (no permission) and letting any unexpected error surface.

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-28 18:45:25 -04:00
dsql 6dae35001e fix: out-of-range page wrap returns a per_page slice; validate cache length
get_page() out-of-range/negative wrap reset to page 0 but returned pages[0] (a single item) even with per_page>1, mis-shaping the page downstream; it now reroutes through get_page(0) so the normal slice logic applies. cache shorter than max_pages now raises ValueError at construction instead of an IndexError when the cache button is clicked on a later page.

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-28 17:46:20 -04:00
dsql 4d52ef7f50 fix: reject empty pages at construction (v0.1.1)
empty pages gave max_pages=0; get_page(0)/start() then IndexError'd on self.pages[0].
guard in __init__ with a clear ValueError instead of a deferred crash at render.

verified: ButtonPaginator([]) -> ValueError; non-empty and single-page intact.
Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-28 15:51:40 -04:00
3 changed files with 58 additions and 23 deletions
+4 -3
View File
@@ -9,13 +9,13 @@ buttons) behind previous / jump / next navigation, with an optional cache button
`requirements.txt`:
```
dpy_paginator @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_paginator.git@v0.1.0
dpy_paginator @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_paginator.git@v0.1.2
```
Direct:
```bash
pip install "dpy_paginator @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_paginator.git@v0.1.0"
pip install "dpy_paginator @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_paginator.git@v0.1.2"
```
Requires `discord.py` (pulled transitively).
@@ -116,7 +116,8 @@ await ButtonPaginator(pages, cache=cache, cache_sleep=1.0).start(ctx)
```
Omit `cache` or pass `None`/`[]` and the button never appears. When set, `cache`
must have one entry per rendered page.
must have one entry per rendered page — a shorter `cache` raises `ValueError` at
construction rather than failing with an `IndexError` mid-navigation.
## Constructor options
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "dpy_paginator"
version = "0.1.0"
version = "0.1.2"
description = "Button-navigated paginator for discord.py — config-free, injectable emojis, installable."
requires-python = ">=3.10"
dependencies = [
+53 -19
View File
@@ -159,6 +159,8 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
cache_sleep: seconds to wait after priming before refreshing the view
"""
super().__init__(timeout=timeout)
if not pages:
raise ValueError("ButtonPaginator requires at least one page")
self.author_id: Optional[int] = author_id
self.delete_message_after: bool = delete_message_after
self.mentions_allowed = mentions_allowed or discord.AllowedMentions.all()
@@ -174,6 +176,11 @@ 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:
raise ValueError(
f"cache has {len(cache)} entries but there are {self.max_pages} pages; "
"cache needs one entry per page"
)
self._page_kwargs: Dict[str, Any] = self._fresh_kwargs()
def _fresh_kwargs(self) -> Dict[str, Any]:
@@ -202,7 +209,7 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
"""return the content for a page index, wrapping out-of-range to 0"""
if page_number < 0 or page_number >= self.max_pages:
self.current_page = 0
return self.pages[self.current_page]
return self.get_page(0)
if self.per_page == 1:
return self.pages[page_number]
base = page_number * self.per_page
@@ -273,27 +280,34 @@ 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.
"""
self.clear_items()
self.previous_page.emoji = self.emojis["previous"]
self.previous_page.disabled = self.current_page <= 0
self.add_item(self.previous_page)
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)
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)"""
@@ -363,21 +377,41 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
self.update_buttons()
if self.max_pages < 2:
self.stop()
kwargs.pop("view", None)
if self.current_page_buttons:
# single page WITH custom buttons: keep the view live so the
# buttons' callbacks still fire; strip only the navigation row
self.update_buttons(nav=False)
else:
# single page, no custom buttons: no interactive row at all
self.stop()
kwargs.pop("view", None)
self.reset_files(kwargs)
send_kwargs["ephemeral"] = self.ephemeral
if isinstance(obj, discord.Interaction):
# ephemeral is an interaction-response concept; only these paths accept it
if obj.response.is_done():
self.message = await obj.followup.send(**kwargs, **send_kwargs)
self.message = await obj.followup.send(**kwargs, ephemeral=self.ephemeral, **send_kwargs)
else:
await obj.response.send_message(**kwargs, **send_kwargs)
await obj.response.send_message(**kwargs, ephemeral=self.ephemeral, **send_kwargs)
self.message = await obj.original_response()
elif isinstance(obj, Messageable):
# Messageable.send (a raw channel) has no ephemeral param — never pass it
self.message = await obj.send(**kwargs, **send_kwargs)
else:
raise TypeError(f"expected Interaction or Messageable, got {obj.__class__.__name__}")
return self.message
async def on_timeout(self) -> None:
"""delete the message on timeout when delete_message_after is set
only the expected discord failures (already deleted, or no permission) are
swallowed; an unexpected error surfaces rather than being silently dropped.
"""
if not self.delete_message_after or self.message is None:
return
try:
await self.message.delete()
except (discord.NotFound, discord.Forbidden):
pass