Update paginator.py

This commit is contained in:
Maxikinz 2021-10-09 20:29:00 +02:00 committed by GitHub
parent 7228ef64a5
commit c74fd90a9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,16 +9,17 @@ class FirstPageComps(View):
async def interaction_check(self, interaction: Interaction):
return interaction.user.id == self.author_id
@discord.ui.button(style=ButtonStyle.primary, disabled=True, label='Left')
@discord.ui.button(style=ButtonStyle.primary, disabled=True, label="Left")
async def left(self, button: discord.ui.Button, interaction: Interaction):
self.value = "left"
self.stop()
@discord.ui.button(style=ButtonStyle.primary, label='Right')
@discord.ui.button(style=ButtonStyle.primary, label="Right")
async def right(self, button: discord.ui.Button, interaction: Interaction):
self.value = "right"
self.stop()
# The middle pages have both left and right buttons available
class MiddlePageComps(View):
def __init__(self, author_id: int):
@ -29,16 +30,17 @@ class MiddlePageComps(View):
async def interaction_check(self, interaction: Interaction):
return interaction.user.id == self.author_id
@discord.ui.button(style=ButtonStyle.primary, label='Left')
@discord.ui.button(style=ButtonStyle.primary, label="Left")
async def left(self, button: discord.ui.Button, interaction: Interaction):
self.value = "left"
self.stop()
@discord.ui.button(style=ButtonStyle.primary, label='Right')
@discord.ui.button(style=ButtonStyle.primary, label="Right")
async def right(self, button: discord.ui.Button, interaction: Interaction):
self.value = "right"
self.stop()
# The last page has the right button disabled, because there's no right page to go to anymore...
class LastPageComps(View):
def __init__(self, author_id: int):
@ -49,12 +51,12 @@ class LastPageComps(View):
async def interaction_check(self, interaction: Interaction):
return interaction.user.id == self.author_id
@discord.ui.button(style=ButtonStyle.primary, label='Left')
@discord.ui.button(style=ButtonStyle.primary, label="Left")
async def left(self, button: discord.ui.Button, interaction: Interaction):
self.value = "left"
self.stop()
@discord.ui.button(style=ButtonStyle.primary, label='Right', disabled=True)
@discord.ui.button(style=ButtonStyle.primary, label="Right", disabled=True)
async def right(self, button: discord.ui.Button, interaction: Interaction):
self.value = "right"
self.stop()
@ -73,7 +75,7 @@ async def paginate(ctx, pages: list, title: str = None):
embed = first_page
if title:
embed.title = f'{title} | Page {index+1}/{total_pages}'
embed.title = f"{title} | Page {index+1}/{total_pages}"
view = FirstPageComps(ctx.author.id)
# Here we send the message with the view of the first page and the FirstPageComps buttons
@ -89,17 +91,25 @@ async def paginate(ctx, pages: list, title: str = None):
if view.value == "right":
index += 1
current_page = pages[index]
view = MiddlePageComps(ctx.author.id) if current_page != last_page else LastPageComps(ctx.author.id)
view = (
MiddlePageComps(ctx.author.id)
if current_page != last_page
else LastPageComps(ctx.author.id)
)
embed = current_page
if title:
embed.title = f'{title} | Page {index+1}/{total_pages}'
embed.title = f"{title} | Page {index+1}/{total_pages}"
elif view.value == "left":
index -= 1
current_page = pages[index]
view = MiddlePageComps(ctx.author.id) if current_page != first_page else FirstPageComps(ctx.author.id)
view = (
MiddlePageComps(ctx.author.id)
if current_page != first_page
else FirstPageComps(ctx.author.id)
)
embed = current_page
if title:
embed.title = f'{title} | Page {index+1}/{total_pages}'
embed.title = f"{title} | Page {index+1}/{total_pages}"
await msg.edit(embed=embed, view=view)