@@ -5,6 +5,9 @@ pub(crate) const ACCENT_COLOUR: Colour = Colour(0x8957e5);
55pub ( crate ) const OK_COLOUR : Colour = Colour ( 0x2ecc71 ) ;
66pub ( crate ) const ERROR_COLOUR : Colour = Colour ( 0xe74c3c ) ;
77
8+ use arrayvec:: ArrayString ;
9+ use to_arraystring:: ToArrayString ;
10+
811use crate :: { Context , Error } ;
912
1013use poise:: serenity_prelude:: {
@@ -64,20 +67,43 @@ pub async fn interaction_err(ctx: &serenity::Context, press: &ComponentInteracti
6467 let _ = press. create_response ( ctx, builder) . await ;
6568}
6669
67- pub async fn paginate_lists < U , E > (
68- ctx : poise:: Context < ' _ , U , E > ,
70+ enum Kind {
71+ Next ,
72+ Previous ,
73+ }
74+
75+ impl Kind {
76+ fn from_id ( id : & str , ctx_id : & str ) -> Option < Self > {
77+ let this = match id. strip_prefix ( ctx_id) ? {
78+ "next" => Self :: Next ,
79+ "prev" => Self :: Previous ,
80+ _ => return None ,
81+ } ;
82+
83+ Some ( this)
84+ }
85+ }
86+
87+ pub async fn paginate_lists (
88+ ctx : crate :: Context < ' _ > ,
6989 pages : & [ Vec < ( String , String , bool ) > ] ,
7090 embed_title : & str ,
7191) -> Result < ( ) , Error > {
72- let ctx_id = ctx. id ( ) ;
73- let prev_button_id = format ! ( "{ctx_id}prev" ) ;
74- let next_button_id = format ! ( "{ctx_id}next" ) ;
92+ let ctx_id = ctx. id ( ) . to_arraystring ( ) ;
93+
94+ let mut prev_button_id = ArrayString :: < 24 > :: new ( ) ;
95+ prev_button_id. push_str ( & ctx_id) ;
96+ prev_button_id. push_str ( "prev" ) ;
97+
98+ let mut next_button_id = ArrayString :: < 24 > :: new ( ) ;
99+ next_button_id. push_str ( & ctx_id) ;
100+ next_button_id. push_str ( "next" ) ;
75101
76102 let colour = Colour :: TEAL ;
77103
78104 let components = CreateActionRow :: Buttons ( vec ! [
79- CreateButton :: new( & prev_button_id) . emoji( '◀' ) ,
80- CreateButton :: new( & next_button_id) . emoji( '▶' ) ,
105+ CreateButton :: new( & * prev_button_id) . emoji( '◀' ) ,
106+ CreateButton :: new( & * next_button_id) . emoji( '▶' ) ,
81107 ] ) ;
82108 let mut current_page = 0 ;
83109
@@ -113,15 +139,17 @@ pub async fn paginate_lists<U, E>(
113139 . timeout ( std:: time:: Duration :: from_secs ( 180 ) )
114140 . await
115141 {
116- if press. data . custom_id == next_button_id {
117- current_page += 1 ;
118- if current_page >= pages. len ( ) {
119- current_page = 0 ;
142+ match Kind :: from_id ( & press. data . custom_id , & ctx_id) {
143+ Some ( Kind :: Next ) => {
144+ current_page += 1 ;
145+ if current_page >= pages. len ( ) {
146+ current_page = 0 ;
147+ }
148+ }
149+ Some ( Kind :: Previous ) => {
150+ current_page = current_page. checked_sub ( 1 ) . unwrap_or ( pages. len ( ) - 1 ) ;
120151 }
121- } else if press. data . custom_id == prev_button_id {
122- current_page = current_page. checked_sub ( 1 ) . unwrap_or ( pages. len ( ) - 1 ) ;
123- } else {
124- continue ;
152+ None => continue ,
125153 }
126154
127155 press
0 commit comments