Skip to content
Merged
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
68 changes: 37 additions & 31 deletions CBattle/package/pagination.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
import discord
from discord import ButtonStyle, Colour, Interaction
from discord.ui import Button, Container, LayoutView, TextDisplay, Thumbnail

from .cog import THUMBNAILS, TUTORIAL

class TutorialPages(discord.ui.View):

class TutorialPages(LayoutView):
def __init__(self, pages, author_id: int):
super().__init__(timeout=None)

self.pages = pages
self.author_id = author_id

self.current = 0
self.build_page()

def build_page(self):
self.clear_items()

title = list(TUTORIAL.keys())[self.current]
description = TUTORIAL[title]
thumbnail_url = THUMBNAILS[self.current]

async def update_page(self, interaction: discord.Interaction):
embed, attachment = await self.pages[self.current]()
container = Container(accent_color=Colour.red())

if attachment:
await interaction.response.edit_message(embed=embed, attachments=[attachment], view=self)
return
container.add_item(TextDisplay(text=f"**Tutorial Page {self.current + 1}: {title}**\n{description}"))

await interaction.response.edit_message(embed=embed, view=self)
container.add_item(Thumbnail(url=thumbnail_url))

async def interaction_check(self, interaction: discord.Interaction) -> bool:
container.add_item(Button(label="⇐", style=ButtonStyle.secondary, custom_id="first"))
container.add_item(Button(label="◁", style=ButtonStyle.primary, custom_id="prev"))
container.add_item(Button(label="▷", style=ButtonStyle.primary, custom_id="next"))
container.add_item(Button(label="⇒", style=ButtonStyle.secondary, custom_id="last"))

self.add_item(container)

async def interaction_check(self, interaction: Interaction) -> bool:
if interaction.user.id != self.author_id:
await interaction.response.send_message("You are not allowed to interact with this menu.", ephemeral=True)
return False

return True

@discord.ui.button(style=discord.ButtonStyle.secondary, label="⇐")
async def go_first(self, interaction: discord.Interaction, button: discord.ui.Button):
self.current = 0
await self.update_page(interaction)

@discord.ui.button(style=discord.ButtonStyle.primary, label="◁")
async def go_previous(self, interaction: discord.Interaction, button: discord.ui.Button):
self.current = (self.current - 1) % len(self.pages)
await self.update_page(interaction)

@discord.ui.button(style=discord.ButtonStyle.primary, label="▷")
async def go_next(self, interaction: discord.Interaction, button: discord.ui.Button):
self.current = (self.current + 1) % len(self.pages)
await self.update_page(interaction)

@discord.ui.button(style=discord.ButtonStyle.secondary, label="⇒")
async def go_last(self, interaction: discord.Interaction, button: discord.ui.Button):
self.current = len(self.pages) - 1
await self.update_page(interaction)
async def on_button_click(self, interaction: Interaction):
match interaction.data["custom_id"]:
case "first":
self.current = 0
case "prev":
self.current = (self.current - 1) % len(self.pages)
case "next":
self.current = (self.current + 1) % len(self.pages)
case "last":
self.current = len(self.pages) - 1

self.build_page()
await interaction.response.edit_message(view=self)