@@ -21,24 +21,11 @@ impl<'a> Event<'a> {
2121 }
2222 }
2323
24- pub fn custom_id ( & self ) -> Option < & str > {
25- Some ( & self . interaction ?. data . custom_id )
26- }
27- pub fn custom_id_number ( & self , prefix : & str , user : Snowflake < User > ) -> Option < usize > {
28- if self . interaction ?. user . id != user {
29- return None ;
30- }
31-
32- let id = & self . interaction ?. data . custom_id ;
33- let s = id. strip_prefix ( prefix) ?;
34- let c = s. chars ( ) . next ( ) ?;
35- B64_TABLE . iter ( ) . position ( |& p| p == c)
36- }
37- pub fn values ( & self , id : & str ) -> Option < & Vec < String > > {
38- match self . interaction {
39- Some ( i) if i. data . custom_id == id => Some ( & i. data . values ) ,
40- _ => None ,
41- }
24+ pub fn matches < T > (
25+ & self ,
26+ f : impl FnOnce ( & ' a Interaction < MessageComponent > ) -> Option < T > ,
27+ ) -> Option < T > {
28+ f ( self . interaction ?)
4229 }
4330
4431 pub fn button (
@@ -83,7 +70,13 @@ impl GameMessage {
8370 selected : & mut Vec < usize > ,
8471 ) {
8572 // get selected values
86- let changed = match event. values ( & name) {
73+ let changed = match event. matches ( |i| {
74+ if i. data . custom_id == name {
75+ Some ( & i. data . values )
76+ } else {
77+ None
78+ }
79+ } ) {
8780 Some ( v) => {
8881 * selected = v
8982 . iter ( )
@@ -134,7 +127,7 @@ impl GameMessage {
134127 max : i32 ,
135128 ) {
136129 // get value
137- match event. custom_id ( ) . and_then ( |s| s . strip_prefix ( & name) ) {
130+ match event. matches ( |i| i . data . custom_id . strip_prefix ( & name) ) {
138131 Some ( "__min" ) => * val = val. saturating_sub ( 1 ) . max ( min) ,
139132 Some ( "__max" ) => * val = val. saturating_add ( 1 ) . min ( max) ,
140133 _ => ( ) ,
@@ -199,17 +192,26 @@ impl GameMessage {
199192 pub fn create_select_grid (
200193 & mut self ,
201194 event : & Event ,
202- user : Snowflake < User > ,
203195 count : usize ,
204196 selected : & mut Vec < Option < usize > > ,
205197 done : impl FnOnce ( & Vec < Option < usize > > ) -> bool ,
206198 ) -> bool {
207199 // TODO: scrolling if too big
208200
209201 let mut changed = false ;
202+
203+ // for some reason rust-analyzer thinks this is unused
204+ #[ allow( unused_assignments) ]
210205 let mut is_done = false ;
211206
212- if let Some ( i) = event. custom_id_number ( "#" , user) . filter ( |& i| i < count) {
207+ if let Some ( i) = event. matches ( |i| {
208+ let s = i. data . custom_id . strip_prefix ( '#' ) ?;
209+ let c = s. chars ( ) . next ( ) ?;
210+ B64_TABLE
211+ . iter ( )
212+ . position ( |& p| p == c)
213+ . filter ( |& i| i < count)
214+ } ) {
213215 if selected. contains ( & Some ( i) ) {
214216 // we are not done anymore
215217 changed = done ( selected) ;
0 commit comments