Using Button Menus
Contents
Using Button Menus#
Contents
Menus#
Here is a button implementation of a basic menu that has a stop button and two reply reactions.
Note that the ButtonMenu
class is used instead of Menu
in order to use Button
components instead of reactions.
ButtonMenu
is a subclass of Menu
and therefore
any of the attributes and methods of Menu
are available.
Also note that view=self
is passed with the initial message and
nextcord.ui.button()
is used instead of menus.button()
.
ButtonMenu.disable()
can be used to disable all buttons in the menu.
ButtonMenu.enable()
can be used to enable all buttons in the menu.
Additionally, disable_buttons_after
can be used as a kwarg to
ButtonMenu
to disable all buttons when the menu stops and
clear_buttons_after
can be used to remove them.
import nextcord
from nextcord.ext import menus
class MyButtonMenu(menus.ButtonMenu):
def __init__(self):
super().__init__(disable_buttons_after=True)
async def send_initial_message(self, ctx, channel):
return await channel.send(f'Hello {ctx.author}', view=self)
@nextcord.ui.button(emoji="\N{THUMBS UP SIGN}")
async def on_thumbs_up(self, button, interaction):
await self.message.edit(content=f"Thanks {interaction.user}!")
@nextcord.ui.button(emoji="\N{THUMBS DOWN SIGN}")
async def on_thumbs_down(self, button, interaction):
await self.message.edit(content=f"That's not nice {interaction.user}...")
@nextcord.ui.button(emoji="\N{BLACK SQUARE FOR STOP}\ufe0f")
async def on_stop(self, button, interaction):
self.stop()
Instantiation is the same as for Reaction Menus
@bot.command()
async def menu_example(ctx):
await MyButtonMenu().start(ctx)
Pagination#
A ButtonMenuPages
class is provided for pagination with button components.
ButtonMenuPages
works the same way as MenuPages
, but with
Button
components instead of reactions.
A nextcord.ButtonStyle
can optionally be passed in to customize the appearance of the buttons.
The PageSource
deals with the data representation and formatting of the data we want to paginate.
MySource
is the same as defined earlier, but the menu is instantiated with
ButtonMenuPages
as follows:
from nextcord.ext import menus
class MySource(menus.ListPageSource):
def __init__(self, data):
super().__init__(data, per_page=4)
async def format_page(self, menu, entries):
offset = menu.current_page * self.per_page
return '\n'.join(f'{i}. {v}' for i, v in enumerate(entries, start=offset))
@bot.command()
async def pages_example(ctx):
data = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
pages = menus.ButtonMenuPages(
source=MySource(data),
clear_buttons_after=True,
style=nextcord.ButtonStyle.primary,
)
await pages.start(ctx)