-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaginator.py
71 lines (58 loc) · 3.14 KB
/
paginator.py
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
from discord.ui import View, button, Button
from discord import ButtonStyle, Interaction
class Paginator(View):
"""
Paginator for Embeds.
Parameters:
----------
embeds: List[Embed]
List of embeds which are in the Paginator. Paginator starts from first embed.
author: int
The ID of the author who can interact with the buttons. Anyone can interact with the Paginator Buttons if not specified.
timeout: float
How long the Paginator should timeout in, after the last interaction.
"""
def __init__(self, embeds: list, author: int = None, timeout: float = None):
if not timeout:
super().__init__(timeout=None)
else:
super().__init__(timeout=timeout)
self.embeds = embeds
self.author = author
self.CurrentEmbed = 0
CountButton = [x for x in self.children if x.custom_id == "count"][0]
CountButton.label = f"{self.CurrentEmbed + 1} / {len(self.embeds)}"
previousButton = [x for x in self.children if x.custom_id == "previous"][0]
previousButton.disabled = True
nextButton = [x for x in self.children if x.custom_id == "next"][0]
nextButton.disabled = (False if len(self.embeds) > 1 else True)
@button(emoji="⬅️", style=ButtonStyle.blurple, custom_id="previous")
async def previous(self, button: Button, interaction: Interaction):
if interaction.user.id != self.author and self.author != None:
return await interaction.response.send_message("你無法點選這個按鈕!", ephemeral=True)
if self.CurrentEmbed:
nextButton = [x for x in self.children if x.custom_id == "next"][0]
if nextButton.disabled == True:
nextButton.disabled = False
self.CurrentEmbed -= 1
CountButton = [x for x in self.children if x.custom_id == "count"][0]
CountButton.label = f"{self.CurrentEmbed + 1} / {len(self.embeds)}"
if self.CurrentEmbed == 0:
button.disabled = True
await interaction.response.edit_message(embed=self.embeds[self.CurrentEmbed], view=self)
@button(label="/", style=ButtonStyle.green, disabled=True, custom_id="count")
async def count(self, button: Button, interaction: Interaction):
pass
@button(emoji="➡️", style=ButtonStyle.blurple, custom_id="next")
async def next(self, button: Button, interaction: Interaction):
if interaction.user.id != self.author and self.author != 123:
return await interaction.response.send_message("你無法點選這個按鈕!", ephemeral=True)
previousButton = [x for x in self.children if x.custom_id == "previous"][0]
if previousButton.disabled == True:
previousButton.disabled = False
self.CurrentEmbed += 1
CountButton = [x for x in self.children if x.custom_id == "count"][0]
CountButton.label = f"{self.CurrentEmbed + 1} / {len(self.embeds)}"
if self.CurrentEmbed == len(self.embeds) - 1:
button.disabled = True
await interaction.response.edit_message(embed=self.embeds[self.CurrentEmbed], view=self)