-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaginator.py
More file actions
110 lines (90 loc) · 4.54 KB
/
Copy pathPaginator.py
File metadata and controls
110 lines (90 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import discord
from discord.ext import commands
class Simple(discord.ui.View):
"""
Embed Paginator.
Parameters:
----------
timeout: int
How long the Paginator should timeout in, after the last interaction. (In seconds) (Overrides default of 60)
PreviousButton: discord.ui.Button
Overrides default previous button.
NextButton: discord.ui.Button
Overrides default next button.
PageCounterStyle: discord.ButtonStyle
Overrides default page counter style.
InitialPage: int
Page to start the pagination on.
AllowExtInput: bool
Overrides ability for 3rd party to interract with button.
"""
def __init__(self, *,
timeout: int = 60,
PreviousButton: discord.ui.Button = discord.ui.Button(emoji=discord.PartialEmoji(name="\U000025c0")),
NextButton: discord.ui.Button = discord.ui.Button(emoji=discord.PartialEmoji(name="\U000025b6")),
PageCounterStyle: discord.ButtonStyle = discord.ButtonStyle.grey,
InitialPage: int = 0, AllowExtInput: bool = False,
ephemeral: bool = False) -> None:
self.PreviousButton = PreviousButton
self.NextButton = NextButton
self.PageCounterStyle = PageCounterStyle
self.InitialPage = InitialPage
self.AllowExtInput = AllowExtInput
self.ephemeral = ephemeral
self.pages = None
self.ctx = None
self.message = None
self.current_page = None
self.page_counter = None
self.total_page_count = None
super().__init__(timeout=timeout)
async def start(self, ctx: discord.Interaction | commands.Context, pages: list[discord.Embed]):
if isinstance(ctx, discord.Interaction):
ctx = await commands.Context.from_interaction(ctx)
self.pages = pages
self.total_page_count = len(pages)
self.ctx = ctx
self.current_page = self.InitialPage
self.PreviousButton.callback = self.previous_button_callback
self.NextButton.callback = self.next_button_callback
self.page_counter = SimplePaginatorPageCounter(style=self.PageCounterStyle,
TotalPages=self.total_page_count,
InitialPage=self.InitialPage)
self.add_item(self.PreviousButton)
self.add_item(self.page_counter)
self.add_item(self.NextButton)
if len(pages) == 1:
self.message = await ctx.send(embed=self.pages[self.InitialPage], ephemeral=self.ephemeral)
else:
self.message = await ctx.send(embed=self.pages[self.InitialPage], view=self, ephemeral=self.ephemeral)
async def previous(self):
if self.current_page == 0:
self.current_page = self.total_page_count - 1
else:
self.current_page -= 1
self.page_counter.label = f"{self.current_page + 1}/{self.total_page_count}"
await self.message.edit(embed=self.pages[self.current_page], view=self)
async def next(self):
if self.current_page == self.total_page_count - 1:
self.current_page = 0
else:
self.current_page += 1
self.page_counter.label = f"{self.current_page + 1}/{self.total_page_count}"
await self.message.edit(embed=self.pages[self.current_page], view=self)
async def next_button_callback(self, interaction: discord.Interaction):
if interaction.user != self.ctx.author and self.AllowExtInput:
embed = discord.Embed(description="You cannot control this pagination because you did not execute it.",
color=discord.Colour.red())
return await interaction.response.send_message(embed=embed, ephemeral=True)
await self.next()
await interaction.response.defer()
async def previous_button_callback(self, interaction: discord.Interaction):
if interaction.user != self.ctx.author and self.AllowExtInput:
embed = discord.Embed(description="You cannot control this pagination because you did not execute it.",
color=discord.Colour.red())
return await interaction.response.send_message(embed=embed, ephemeral=True)
await self.previous()
await interaction.response.defer()
class SimplePaginatorPageCounter(discord.ui.Button):
def __init__(self, style: discord.ButtonStyle, TotalPages, InitialPage):
super().__init__(label=f"{InitialPage + 1}/{TotalPages}", style=style, disabled=True)