Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support format_page returning list of embeds #31

Merged
merged 5 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/ext/menus/reaction_menus.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,6 @@ For the sake of example, here’s a basic list source that is paginated:
await pages.start(ctx)

The :meth:`PageSource.format_page` can return either a :class:`str` for content,
:class:`nextcord.Embed` for an embed, or a :class:`dict` to pass into the kwargs
:class:`nextcord.Embed` for an embed, :class:`List[nextcord.Embed]` for
sending multiple embeds, or a :class:`dict` to pass into the kwargs
of :meth:`nextcord.Message.edit`.
4 changes: 2 additions & 2 deletions nextcord/ext/menus/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Dict, Union
from typing import Dict, List, Union

import nextcord

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

# type definition for possible page formats
PageFormatType = Union[str, nextcord.Embed, SendKwargsType]
PageFormatType = Union[str, nextcord.Embed, List[nextcord.Embed], SendKwargsType]

# type definition for emoji parameters
EmojiType = Union[str, nextcord.Emoji, nextcord.PartialEmoji]
27 changes: 25 additions & 2 deletions nextcord/ext/menus/menu_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,32 @@ async def _get_kwargs_from_page(self, page: List[Any]) -> SendKwargsType:
"""|coro|

Calls :meth:`PageSource.format_page` and returns a dict of send kwargs

Raises
--------
TypeError
The return value of :meth:`PageSource.format_page` was not a
:class:`str`, :class:`nextcord.Embed`, :class:`List[nextcord.Embed]`,
or :class:`dict`.
"""
value: PageFormatType = await nextcord.utils.maybe_coroutine(
self._source.format_page, self, page
)
if isinstance(value, dict):
return value
elif isinstance(value, str):
return {"content": value, "embed": None}
return {"content": value}
elif isinstance(value, nextcord.Embed):
return {"embed": value, "content": None}
return {"embed": value}
elif isinstance(value, list) and all(
isinstance(v, nextcord.Embed) for v in value
):
return {"embeds": value}
raise TypeError(
"Expected {0!r} not {1.__class__!r}.".format(
(dict, str, nextcord.Embed, List[nextcord.Embed]), value
)
)

async def show_page(self, page_number: int):
"""|coro|
Expand Down Expand Up @@ -323,6 +339,13 @@ async def show_page(self, page_number: int):
async def _get_kwargs_from_page(self, page: List[Any]) -> SendKwargsType:
"""|coro|
Calls :meth:`PageSource.format_page` and returns a dict of send kwargs

Raises
--------
TypeError
The return value of :meth:`PageSource.format_page` was not a
:class:`str`, :class:`nextcord.Embed`, :class:`List[nextcord.Embed]`,
or :class:`dict`.
"""
kwargs = await super()._get_kwargs_from_page(page)
# add view to kwargs if it's not already there
Expand Down
12 changes: 8 additions & 4 deletions nextcord/ext/menus/page_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,22 @@ async def format_page(self, menu: Menu, page: Any) -> PageFormatType:

This method must return one of the following types.

If this method returns a ``str`` then it is interpreted as returning
If this method returns a :class:`str` then it is interpreted as returning
the ``content`` keyword argument in :meth:`nextcord.Message.edit`
and :meth:`nextcord.abc.Messageable.send`.

If this method returns a :class:`nextcord.Embed` then it is interpreted
as returning the ``embed`` keyword argument in :meth:`nextcord.Message.edit`
and :meth:`nextcord.abc.Messageable.send`.

If this method returns a ``dict`` then it is interpreted as the
If this method returns a :class:`List[nextcord.Embed]` then it is interpreted
as returning the ``embeds`` keyword argument in :meth:`nextcord.Message.edit`
and :meth:`nextcord.abc.Messageable.send`.

If this method returns a :class:`dict` then it is interpreted as the
keyword-arguments that are used in both :meth:`nextcord.Message.edit`
and :meth:`nextcord.abc.Messageable.send`. The two of interest are
``embed`` and ``content``.
and :meth:`nextcord.abc.Messageable.send`. A few of interest are:
``content``, ``embed``, ``embeds``, ``file``, ``files``.

Parameters
------------
Expand Down