Skip to content

Commit 2d1b124

Browse files
authored
feat: support format_page returning list of embeds (#31)
1 parent 98a9c2b commit 2d1b124

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

docs/ext/menus/reaction_menus.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,6 @@ For the sake of example, here’s a basic list source that is paginated:
131131
await pages.start(ctx)
132132
133133
The :meth:`PageSource.format_page` can return either a :class:`str` for content,
134-
:class:`nextcord.Embed` for an embed, or a :class:`dict` to pass into the kwargs
134+
:class:`nextcord.Embed` for an embed, :class:`List[nextcord.Embed]` for
135+
sending multiple embeds, or a :class:`dict` to pass into the kwargs
135136
of :meth:`nextcord.Message.edit`.

nextcord/ext/menus/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Dict, Union
2+
from typing import Dict, List, Union
33

44
import nextcord
55

@@ -15,7 +15,7 @@
1515
SendKwargsType = Dict[str, Union[str, nextcord.Embed, nextcord.ui.View, None]]
1616

1717
# type definition for possible page formats
18-
PageFormatType = Union[str, nextcord.Embed, SendKwargsType]
18+
PageFormatType = Union[str, nextcord.Embed, List[nextcord.Embed], SendKwargsType]
1919

2020
# type definition for emoji parameters
2121
EmojiType = Union[str, nextcord.Emoji, nextcord.PartialEmoji]

nextcord/ext/menus/menu_pages.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,32 @@ async def _get_kwargs_from_page(self, page: List[Any]) -> SendKwargsType:
7777
"""|coro|
7878
7979
Calls :meth:`PageSource.format_page` and returns a dict of send kwargs
80+
81+
Raises
82+
--------
83+
TypeError
84+
The return value of :meth:`PageSource.format_page` was not a
85+
:class:`str`, :class:`nextcord.Embed`, :class:`List[nextcord.Embed]`,
86+
or :class:`dict`.
8087
"""
8188
value: PageFormatType = await nextcord.utils.maybe_coroutine(
8289
self._source.format_page, self, page
8390
)
8491
if isinstance(value, dict):
8592
return value
8693
elif isinstance(value, str):
87-
return {"content": value, "embed": None}
94+
return {"content": value}
8895
elif isinstance(value, nextcord.Embed):
89-
return {"embed": value, "content": None}
96+
return {"embed": value}
97+
elif isinstance(value, list) and all(
98+
isinstance(v, nextcord.Embed) for v in value
99+
):
100+
return {"embeds": value}
101+
raise TypeError(
102+
"Expected {0!r} not {1.__class__!r}.".format(
103+
(dict, str, nextcord.Embed, List[nextcord.Embed]), value
104+
)
105+
)
90106

91107
async def show_page(self, page_number: int):
92108
"""|coro|
@@ -321,6 +337,13 @@ async def show_page(self, page_number: int):
321337
async def _get_kwargs_from_page(self, page: List[Any]) -> SendKwargsType:
322338
"""|coro|
323339
Calls :meth:`PageSource.format_page` and returns a dict of send kwargs
340+
341+
Raises
342+
--------
343+
TypeError
344+
The return value of :meth:`PageSource.format_page` was not a
345+
:class:`str`, :class:`nextcord.Embed`, :class:`List[nextcord.Embed]`,
346+
or :class:`dict`.
324347
"""
325348
kwargs = await super()._get_kwargs_from_page(page)
326349
# add view to kwargs if it's not already there

nextcord/ext/menus/page_source.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,22 @@ async def format_page(self, menu: Menu, page: Any) -> PageFormatType:
112112
113113
This method must return one of the following types.
114114
115-
If this method returns a ``str`` then it is interpreted as returning
115+
If this method returns a :class:`str` then it is interpreted as returning
116116
the ``content`` keyword argument in :meth:`nextcord.Message.edit`
117117
and :meth:`nextcord.abc.Messageable.send`.
118118
119119
If this method returns a :class:`nextcord.Embed` then it is interpreted
120120
as returning the ``embed`` keyword argument in :meth:`nextcord.Message.edit`
121121
and :meth:`nextcord.abc.Messageable.send`.
122122
123-
If this method returns a ``dict`` then it is interpreted as the
123+
If this method returns a :class:`List[nextcord.Embed]` then it is interpreted
124+
as returning the ``embeds`` keyword argument in :meth:`nextcord.Message.edit`
125+
and :meth:`nextcord.abc.Messageable.send`.
126+
127+
If this method returns a :class:`dict` then it is interpreted as the
124128
keyword-arguments that are used in both :meth:`nextcord.Message.edit`
125-
and :meth:`nextcord.abc.Messageable.send`. The two of interest are
126-
``embed`` and ``content``.
129+
and :meth:`nextcord.abc.Messageable.send`. A few of interest are:
130+
``content``, ``embed``, ``embeds``, ``file``, ``files``.
127131
128132
Parameters
129133
------------

0 commit comments

Comments
 (0)