Compare commits

...

12 Commits
v0.1.0 ... main

Author SHA1 Message Date
66933b1e2e chore: ignore .claude/ dir (CLAUDE.md now lives under .claude/)
Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 21:55:13 -04:00
18bbde19b6 docs: note cache=None/[] disables the cache button
Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 21:41:37 -04:00
fa5cdf3e1d fix: F3 guard per_page>=1; swallow on_timeout HTTPException
per_page<=0 raised ZeroDivisionError (==0) or yielded a negative max_pages (<0) at the
divmod; now a clear ValueError. on_timeout swallows a transient delete HTTPException (with
a log) so a best-effort cleanup doesn't surface as an unretrieved-task traceback. add the
module logger.

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 21:35:01 -04:00
1416375e40 fix: cache=[] disables the button (not raises); nav row stays suppressed on re-render (v0.1.4)
M-3: the cache-length guard used 'cache is not None', so cache=[] ([] is not None) hit the
length check and raised at construction — contradicting README/CLAUDE that say a falsy
cache disables the button (matching the render path 'if self.cache:'). guard now uses
'if cache' so [] and None both disable; a non-empty too-short cache still raises.

dpy_paginator-F2: update_buttons() now ANDs nav with max_pages>=2, so a single-page
re-render (update_page -> _build_render_kwargs, default nav=True) no longer resurrects the
nav row over the page's custom buttons. start()'s single-page branch simplified accordingly.

verified by execution with real discord.py: []-disables and None-disables both construct,
real cache renders, too-short still raises; single-page re-render keeps nav suppressed +
custom button, multi-page control still shows nav.

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 20:47:43 -04:00
de572af675 docs: pin install line to release, note unpinned-latest option
Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 18:13:51 -04:00
234f663f04 docs: show unpinned install line; note tag-pinning for reproducibility
Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 18:07:36 -04:00
8c3bacb2f2 fix: reject wrong-typed embed/file page keys; author_id is-None check (v0.1.3)
- a dict page with a wrong-typed embed/embeds/file/files key now raises a clear
  paginator-side ValueError instead of forwarding it to discord.py as a conflicting
  kwarg (opaque TypeError) (L12)
- interaction_check uses 'author_id is None' so an author_id of 0 still restricts (nit).

Signed-off-by: disqualifier <dev@disqualifier.me>
2026-06-29 17:58:09 -04:00
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
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
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
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
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
4 changed files with 87 additions and 25 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
# claude # claude
CLAUDE.md .claude/
# python # python
__pycache__/ __pycache__/

View File

@ -9,17 +9,19 @@ buttons) behind previous / jump / next navigation, with an optional cache button
`requirements.txt`: `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.4
``` ```
Direct: Direct:
```bash ```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.4"
``` ```
Requires `discord.py` (pulled transitively). Requires `discord.py` (pulled transitively).
Drop the `@v0.1.4` suffix from the line above to install the latest unpinned.
## Basic usage ## Basic usage
Plain pages — just navigation: Plain pages — just navigation:
@ -97,6 +99,8 @@ echoes its `data`.
## Cache button (mention priming) ## Cache button (mention priming)
Discord clients render `<@id>` as a raw id until the user object is cached locally. Discord clients render `<@id>` as a raw id until the user object is cached locally.
Omit `cache` (or pass `None` / `[]`) and the button never appears; a non-empty cache
must have one entry per page or construction raises.
The cache button fixes that: pass `cache=[...]` with one entry per page, where each The cache button fixes that: pass `cache=[...]` with one entry per page, where each
entry is a string of the user mentions on that page (`"<@111> <@222> <@333>"`). entry is a string of the user mentions on that page (`"<@111> <@222> <@333>"`).
@ -116,7 +120,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` 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 ## Constructor options
@ -138,4 +143,4 @@ in an embed). It may be sync or async.
## Versioning ## Versioning
Tagged `vX.Y.Z`. Pin the tag in `requirements.txt`. Releases are tagged `vX.Y.Z`. The install line above pins a release; drop the `@vX.Y.Z` suffix to install the latest unpinned. Pin deliberately for reproducible installs.

View File

@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project] [project]
name = "dpy_paginator" name = "dpy_paginator"
version = "0.1.0" version = "0.1.4"
description = "Button-navigated paginator for discord.py — config-free, injectable emojis, installable." description = "Button-navigated paginator for discord.py — config-free, injectable emojis, installable."
requires-python = ">=3.10" requires-python = ">=3.10"
dependencies = [ dependencies = [

View File

@ -29,6 +29,7 @@ config-free: no host config import; everything is passed at construction.
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
import logging
from typing import ( from typing import (
Any, Any,
Dict, Dict,
@ -69,6 +70,8 @@ DEFAULT_EMOJIS = {
"cache": "\U0001f5c2\ufe0f", # 🗂️ "cache": "\U0001f5c2\ufe0f", # 🗂️
} }
log = logging.getLogger(__name__)
PageT_co = TypeVar("PageT_co", bound=Page, covariant=True) PageT_co = TypeVar("PageT_co", bound=Page, covariant=True)
@ -159,6 +162,12 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
cache_sleep: seconds to wait after priming before refreshing the view cache_sleep: seconds to wait after priming before refreshing the view
""" """
super().__init__(timeout=timeout) 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.author_id: Optional[int] = author_id
self.delete_message_after: bool = delete_message_after self.delete_message_after: bool = delete_message_after
self.mentions_allowed = mentions_allowed or discord.AllowedMentions.all() self.mentions_allowed = mentions_allowed or discord.AllowedMentions.all()
@ -174,6 +183,13 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
total_pages, left_over = divmod(len(self.pages), self.per_page) total_pages, left_over = divmod(len(self.pages), self.per_page)
self.max_pages: int = total_pages + (1 if left_over else 0) self.max_pages: int = total_pages + (1 if left_over else 0)
# 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"
)
self._page_kwargs: Dict[str, Any] = self._fresh_kwargs() self._page_kwargs: Dict[str, Any] = self._fresh_kwargs()
def _fresh_kwargs(self) -> Dict[str, Any]: def _fresh_kwargs(self) -> Dict[str, Any]:
@ -193,7 +209,7 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
async def interaction_check(self, interaction: Interaction) -> bool: async def interaction_check(self, interaction: Interaction) -> bool:
"""restrict interaction to author_id when set""" """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 return True
await interaction.response.send_message("You cannot interact with this menu.", ephemeral=True) await interaction.response.send_message("You cannot interact with this menu.", ephemeral=True)
return False return False
@ -202,7 +218,7 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
"""return the content for a page index, wrapping out-of-range to 0""" """return the content for a page index, wrapping out-of-range to 0"""
if page_number < 0 or page_number >= self.max_pages: if page_number < 0 or page_number >= self.max_pages:
self.current_page = 0 self.current_page = 0
return self.pages[self.current_page] return self.get_page(0)
if self.per_page == 1: if self.per_page == 1:
return self.pages[page_number] return self.pages[page_number]
base = page_number * self.per_page base = page_number * self.per_page
@ -252,6 +268,14 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
if isinstance(value, discord.Attachment): if isinstance(value, discord.Attachment):
value = await value.to_file() value = await value.to_file()
self._page_kwargs["files"].append(value) 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: else:
self._page_kwargs[key] = value self._page_kwargs[key] = value
elif isinstance(formatted_page, str): elif isinstance(formatted_page, str):
@ -273,27 +297,38 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
return self._page_kwargs return self._page_kwargs
def update_buttons(self) -> None: def update_buttons(self, nav: bool = True) -> None:
"""rebuild the action row for the current page state""" """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.clear_items()
self.previous_page.emoji = self.emojis["previous"] nav = nav and self.max_pages >= 2
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) if nav:
self.add_item(self.jump_button) 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: for button in self.current_page_buttons:
self.add_item(button) self.add_item(button)
if self.cache: if nav:
self.cache_button.emoji = self.emojis["cache"] if self.cache:
self.add_item(self.cache_button) self.cache_button.emoji = self.emojis["cache"]
self.add_item(self.cache_button)
self.next_page.emoji = self.emojis["next"] self.next_page.emoji = self.emojis["next"]
self.next_page.disabled = self.current_page >= self.max_pages - 1 self.next_page.disabled = self.current_page >= self.max_pages - 1
self.add_item(self.next_page) self.add_item(self.next_page)
async def _build_render_kwargs(self) -> Dict[str, Any]: async def _build_render_kwargs(self) -> Dict[str, Any]:
"""build the edit-ready kwargs for the current page (buttons + attachments)""" """build the edit-ready kwargs for the current page (buttons + attachments)"""
@ -360,24 +395,46 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
) -> Optional[Union[discord.Message, discord.WebhookMessage]]: ) -> Optional[Union[discord.Message, discord.WebhookMessage]]:
"""send the first page; obj is an Interaction or a Messageable""" """send the first page; obj is an Interaction or a Messageable"""
kwargs = await self.get_page_kwargs(self.get_page(self.current_page)) 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() 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() self.stop()
kwargs.pop("view", None) kwargs.pop("view", None)
self.reset_files(kwargs) self.reset_files(kwargs)
send_kwargs["ephemeral"] = self.ephemeral
if isinstance(obj, discord.Interaction): if isinstance(obj, discord.Interaction):
# ephemeral is an interaction-response concept; only these paths accept it
if obj.response.is_done(): 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: 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() self.message = await obj.original_response()
elif isinstance(obj, Messageable): elif isinstance(obj, Messageable):
# Messageable.send (a raw channel) has no ephemeral param — never pass it
self.message = await obj.send(**kwargs, **send_kwargs) self.message = await obj.send(**kwargs, **send_kwargs)
else: else:
raise TypeError(f"expected Interaction or Messageable, got {obj.__class__.__name__}") raise TypeError(f"expected Interaction or Messageable, got {obj.__class__.__name__}")
return self.message 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
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)