Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- from discord.ext import commands
- client = commands.Bot(command_prefix="-", intents=discord.Intents.default())
- tree = client.tree
- @client.event
- async def on_ready():
- synced = await tree.sync()
- print(f"Synced {len(synced)} Command(s)")
- content = ["Message 1", "Message 2", "Message 3",
- "Message 4", "Message 5", "Message 6",
- "Message 7", "Message 8", "Message 9",
- "Message 10", "Message 11", "Message 12"]
- class Pagination(discord.ui.View):
- def __init__(self, chunks: list, page: int, embed: discord.Embed):
- super().__init__(timeout=None)
- self.page = page
- self.embed = embed
- self.chunks = chunks
- @discord.ui.button(label="Previous", emoji="⏮")
- async def prev(self, interaction: discord.Interaction, button: discord.ui.Button):
- self.page -= 1
- if self.page < 1:
- self.page = len(self.chunks)
- self.embed.description = "\n".join(
- x for x in self.chunks[self.page - 1])
- self.embed.set_footer(text=f"Page: {self.page} / {len(self.chunks)}")
- await interaction.response.defer()
- await interaction.message.edit(embed=self.embed)
- @discord.ui.button(label="Next", emoji="⏭")
- async def next(self, interaction: discord.Interaction, button: discord.ui.Button):
- self.page += 1
- if self.page > len(self.chunks):
- self.page = 1
- self.embed.description = "\n".join(
- x for x in self.chunks[self.page - 1])
- self.embed.set_footer(text=f"Page: {self.page} / {len(self.chunks)}")
- await interaction.response.defer()
- await interaction.message.edit(embed=self.embed)
- @tree.command(name="test", description="test pagination")
- async def _test(interaction: discord.Interaction):
- chunks = list(chunk(content, 6))
- page = 1
- embed = discord.Embed(color=discord.Color.dark_gold(),
- title="Discord Pagination")
- embed.description = "\n".join(x for x in chunks[page - 1])
- embed.set_footer(text=f"Page: {page} / {len(chunks)}")
- await interaction.response.send_message(embed=embed, view=Pagination(chunks, page, embed))
- def chunk(list, n):
- for i in range(0, len(list), n):
- yield list[i:i+n]
- client.run("TOKEN")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement