Files
baythn_bot/PaginatedView.py
2025-12-19 20:40:02 +01:00

56 lines
2.3 KiB
Python

import discord
from discord.ui import View, Button
from typing import List
class PaginatedView(View):
"""Base class for paginated views"""
def __init__(self, data: List, page_size: int = 5, timeout: int = 60):
super().__init__(timeout=timeout)
self.data = data
self.page_size = page_size
self.current_page = 0
self.total_pages = (len(data) + page_size - 1) // page_size
self.update_buttons()
def update_buttons(self):
"""Enable/disable navigation buttons based on current page"""
self.children[0].disabled = self.current_page == 0 # First
self.children[1].disabled = self.current_page == 0 # Previous
self.children[2].disabled = self.current_page >= self.total_pages - 1 # Next
self.children[3].disabled = self.current_page >= self.total_pages - 1 # Last
def get_page_data(self):
"""Get data for current page"""
start = self.current_page * self.page_size
end = start + self.page_size
return self.data[start:end]
async def update_message(self, interaction: discord.Interaction):
"""Update the message with current page"""
pass
@discord.ui.button(emoji="", style=discord.ButtonStyle.secondary)
async def first_page(self, interaction: discord.Interaction, button: Button):
self.current_page = 0
self.update_buttons()
await self.update_message(interaction)
@discord.ui.button(emoji="◀️", style=discord.ButtonStyle.secondary)
async def previous_page(self, interaction: discord.Interaction, button: Button):
self.current_page = max(0, self.current_page - 1)
self.update_buttons()
await self.update_message(interaction)
@discord.ui.button(emoji="▶️", style=discord.ButtonStyle.secondary)
async def next_page(self, interaction: discord.Interaction, button: Button):
self.current_page = min(self.total_pages - 1, self.current_page + 1)
self.update_buttons()
await self.update_message(interaction)
@discord.ui.button(emoji="", style=discord.ButtonStyle.secondary)
async def last_page(self, interaction: discord.Interaction, button: Button):
self.current_page = self.total_pages - 1
self.update_buttons()
await self.update_message(interaction)