11use std:: default:: Default ;
22use axum:: Json ;
33use http:: StatusCode ;
4- use lambda_http:: tracing:: info;
54use serde_json:: { json, Value } ;
65use twilight_model:: application:: interaction:: { Interaction , InteractionData } ;
76use twilight_model:: channel:: message:: MessageFlags ;
87use twilight_model:: guild:: PartialMember ;
98use twilight_model:: http:: interaction:: { InteractionResponse , InteractionResponseData , InteractionResponseType } ;
109use crate :: AppState ;
1110use crate :: guilds:: models:: Guild ;
12- use crate :: guilds:: votes:: models:: { RoleListType , VoteOption , VoteVote } ;
11+ use crate :: guilds:: votes:: models:: { RoleListType , VoteVote } ;
1312use crate :: guilds:: votes:: utils:: VoteOptionComponent ;
1413
1514pub ( crate ) async fn handle_component_interaction (
@@ -31,16 +30,14 @@ pub(crate) async fn handle_component_interaction(
3130 let user_id = user. unwrap ( ) . id ;
3231
3332 let mut guild = Guild :: from_db ( guild_id. unwrap ( ) , & app_state. dynamo ) . await . unwrap ( ) ;
34- info ! ( "guild retreived" ) ;
3533
36- let VoteVote { options, role_list, role_list_type, .. } = match guild. vote . votes . iter_mut ( ) . find ( |v| v. message_id == message_id) {
34+ let VoteVote { options, role_list, role_list_type,
35+ is_multi_select, .. } = match guild. vote . votes . iter_mut ( ) . find ( |v| v. message_id == message_id) {
3736 Some ( v) => v,
3837 None => return Err ( StatusCode :: NOT_FOUND ) ,
3938 } ;
40- info ! ( "debug 1" ) ;
4139
4240 let role_in_role_list = roles. iter ( ) . any ( |r| role_list. contains ( r) ) ;
43- info ! ( "debug 2" ) ;
4441
4542 let allowed = match role_list_type {
4643 RoleListType :: BLACKLIST => {
@@ -50,7 +47,6 @@ pub(crate) async fn handle_component_interaction(
5047 role_in_role_list
5148 }
5249 } ;
53- info ! ( "debug 3" ) ;
5450 if !allowed {
5551 return Ok ( Json ( json ! (
5652 InteractionResponse {
@@ -63,31 +59,24 @@ pub(crate) async fn handle_component_interaction(
6359 }
6460 ) ) ) ;
6561 }
66- info ! ( "debug 4" ) ;
62+ let mut responses = vec ! [ ] ;
6763
68- let VoteOption { users , label , .. } = options. iter_mut ( ) . find ( |o| o . custom_id ( ) == data . custom_id ) . unwrap ( ) ;
69- let exists = users. iter ( ) . any ( | & u| u == user_id ) ;
70- if exists {
71- // Already voted, need to remove.
72- users. retain ( | & u| u != user_id ) ;
73-
74- } else {
75- users . insert ( user_id ) ;
64+ for o in options. iter_mut ( ) {
65+ if o . users . contains ( & user_id ) && ( data . custom_id == o . custom_id ( ) || ! * is_multi_select ) {
66+ o . users . remove ( & user_id ) ;
67+ responses . push ( format ! ( "You have removed your vote for {o}." ) ) ;
68+ } else if !o . users . contains ( & user_id ) && data . custom_id == o . custom_id ( ) {
69+ o . users . insert ( user_id ) ;
70+ responses . push ( format ! ( "You have added a vote for {o}." ) ) ;
71+ }
7672 }
77- let label = label. as_ref ( ) . unwrap ( ) ;
78- let content = Some ( if exists {
79- format ! ( "You have removed your vote for {:?}." , label) . to_string ( )
80- } else {
81- format ! ( "You have voted for {:?}." , label) . to_string ( )
82- } ) ;
83- info ! ( "guild about to be saved" ) ;
8473 guild. save ( & app_state. dynamo ) . await ;
8574
8675 Ok ( Json ( json ! (
8776 InteractionResponse {
8877 kind: InteractionResponseType :: ChannelMessageWithSource ,
8978 data: Some ( InteractionResponseData {
90- content,
79+ content: Some ( responses . join ( " \n " ) ) ,
9180 flags: Some ( MessageFlags :: EPHEMERAL ) ,
9281 ..Default :: default ( )
9382 } ) ,
0 commit comments