1
- # pyright: reportUnknownParameterType=false, reportMissingParameterType=false, reportUnknownArgumentType=false, reportUnknownMemberType=false
2
- from typing import override
1
+ from collections . abc import Iterable
2
+ from typing import Self , override
3
3
4
4
import discord
5
- from discord import ui
6
- from discord .ui import Button
5
+ from discord . ext . commands import Bot , Context
6
+ from discord .ui import Button , Modal , TextInput
7
7
8
- from nameless . custom . ui . view . base import BaseView
8
+ __all__ = [ "NamelessPaginatedView" ]
9
9
10
- __all__ = ["ViewButton" ]
11
10
11
+ class JumpToPageModal (Modal ):
12
+ """Modal to ask for specific page."""
12
13
13
- class ToPageModal (discord .ui .Modal ):
14
- page : ui .TextInput [BaseView ] = ui .TextInput (label = "Page" )
14
+ page : TextInput [Self ] = TextInput (
15
+ label = "Page number" ,
16
+ default = "0" ,
17
+ placeholder = "Any page number, will failsafe to '0' (zero)." ,
18
+ )
15
19
16
20
@override
17
21
async def on_submit (self , interaction : discord .Interaction ) -> None :
18
22
self .stop ()
19
23
20
- def get_values (self ):
21
- return self .page .value
24
+ def get_value (self ) -> int :
25
+ """Get parsed page value. Will failsafe to 0."""
26
+ try :
27
+ return int (self .page .value )
28
+ except ValueError :
29
+ return 0
22
30
23
31
24
- class ViewButton (Button [BaseView ]):
32
+ class NamelessPaginatedView (discord .ui .View ):
33
+ """nameless* custom paginated view."""
34
+
35
+ def __init__ (self , ctx : Context [Bot ], timeout : int = 60 ):
36
+ super ().__init__ (timeout = timeout )
37
+ self .ctx : Context [Bot ] = ctx
38
+ self .pages : list [discord .Embed ] = []
39
+ self .current_page : int = 0
40
+ self ._current_message : discord .Message | None = None
41
+
42
+ @property
43
+ def message (self ):
44
+ if not self ._current_message :
45
+ return self .ctx .message
46
+
47
+ return self ._current_message
48
+
49
+ @message .setter
50
+ def message (self , value : discord .Message ):
51
+ self ._current_message = value
52
+
53
+ def add_pages (self , pages : Iterable [discord .Embed ]) -> None :
54
+ self .pages .extend (pages )
55
+
56
+ def add_button (self , button : Button [Self ]):
57
+ self .add_item (button )
58
+
59
+ async def next_page (self ):
60
+ if self .current_page + 1 >= len (self .pages ):
61
+ self .current_page = 0
62
+ else :
63
+ self .current_page += 1
64
+ await self .message .edit (embed = self .pages [self .current_page ], view = self )
65
+
66
+ async def previous_page (self ):
67
+ if self .current_page - 1 < 0 :
68
+ self .current_page = len (self .pages ) - 1
69
+ else :
70
+ self .current_page -= 1
71
+ await self .message .edit (embed = self .pages [self .current_page ], view = self )
72
+
73
+ async def go_to_first_page (self ):
74
+ await self .message .edit (embed = self .pages [0 ], view = self )
75
+
76
+ async def go_to_last_page (self ):
77
+ await self .message .edit (embed = self .pages [- 1 ], view = self )
78
+
79
+ async def go_to_page (self , page : int ):
80
+ self .current_page = page
81
+ await self .ctx .send (embed = self .pages [self .current_page ], view = self )
82
+
83
+ async def start (self ):
84
+ self .message = await self .ctx .send (embed = self .pages [0 ], view = self )
85
+ return await self .wait ()
86
+
87
+ async def end (self ):
88
+ self .stop ()
89
+ # await self.__current_message.delete()
90
+ await self .message .edit (view = None )
91
+
92
+
93
+ class NavigationButton (Button [NamelessPaginatedView ]):
25
94
NEXT_PAGE_ID : str = "0"
26
95
PREVIOUS_PAGE_ID : str = "1"
27
96
GO_TO_FIRST_PAGE_ID : str = "2"
28
97
GO_TO_LAST_PAGE_ID : str = "3"
29
98
GO_TO_PAGE_ID : str = "4"
30
99
END_ID : str = "5"
31
100
32
- def __init__ (self , * args , ** kwargs ):
101
+ def __init__ (self , * args : object , ** kwargs : object ):
33
102
super ().__init__ (* args , ** kwargs )
34
- self ._view : BaseView | None = None
103
+ self ._view : NamelessPaginatedView | None = None
35
104
36
105
@override
37
106
async def callback (self , interaction : discord .Interaction ):
@@ -49,7 +118,7 @@ async def callback(self, interaction: discord.Interaction):
49
118
case self .GO_TO_LAST_PAGE_ID :
50
119
await self .view .go_to_last_page ()
51
120
case self .GO_TO_PAGE_ID :
52
- modal = ToPageModal ()
121
+ modal = JumpToPageModal ()
53
122
await interaction .response .send_modal (modal )
54
123
if await modal .wait ():
55
124
return
@@ -71,7 +140,7 @@ def create_button(
71
140
with_label : bool ,
72
141
with_emote : bool ,
73
142
with_disabled : bool ,
74
- ** kwargs ,
143
+ ** kwargs : object ,
75
144
):
76
145
return cls (
77
146
style = discord .ButtonStyle .gray ,
@@ -88,7 +157,7 @@ def back(
88
157
with_label : bool = False ,
89
158
with_emote : bool = True ,
90
159
with_disabled : bool = False ,
91
- ** kwargs ,
160
+ ** kwargs : object ,
92
161
):
93
162
return cls .create_button (
94
163
"Back" ,
@@ -106,7 +175,7 @@ def next(
106
175
with_label : bool = False ,
107
176
with_emote : bool = True ,
108
177
with_disabled : bool = False ,
109
- ** kwargs ,
178
+ ** kwargs : object ,
110
179
):
111
180
return cls .create_button (
112
181
"Next" ,
@@ -124,7 +193,7 @@ def go_to_first_page(
124
193
with_label : bool = False ,
125
194
with_emote : bool = True ,
126
195
with_disabled : bool = False ,
127
- ** kwargs ,
196
+ ** kwargs : object ,
128
197
):
129
198
return cls .create_button (
130
199
"First Page" ,
@@ -142,7 +211,7 @@ def go_to_last_page(
142
211
with_label : bool = False ,
143
212
with_emote : bool = True ,
144
213
with_disabled : bool = False ,
145
- ** kwargs ,
214
+ ** kwargs : object ,
146
215
):
147
216
return cls .create_button (
148
217
"Last Page" ,
@@ -160,7 +229,7 @@ def go_to_page(
160
229
with_label : bool = False ,
161
230
with_emote : bool = True ,
162
231
with_disabled : bool = False ,
163
- ** kwargs ,
232
+ ** kwargs : object ,
164
233
):
165
234
return cls .create_button (
166
235
"Page Selection" ,
@@ -178,7 +247,7 @@ def end(
178
247
with_label : bool = False ,
179
248
with_emote : bool = True ,
180
249
with_disabled : bool = False ,
181
- ** kwargs ,
250
+ ** kwargs : object ,
182
251
):
183
252
return cls .create_button (
184
253
"End" , cls .END_ID , "⏹️" , with_label , with_emote , with_disabled , ** kwargs
0 commit comments