11use std:: { process:: exit, time:: { SystemTime , UNIX_EPOCH , Duration } , thread} ;
2- use clap:: StructOpt ;
2+ use clap:: Parser ;
33use discord_rich_presence:: { activity:: { self , Activity , Party , Secrets } , DiscordIpcClient , DiscordIpc } ;
44use colored:: * ;
55use user_idle:: UserIdle ;
66
77mod cli;
88
9- fn check_current_party_size ( ) ->String {
10- let args = cli:: Cli :: parse ( ) ;
11- if args. party_size != "__None" {
12- args. party_size . trim_matches ( '[' ) . trim_matches ( ']' ) . split ( "," ) . nth ( 0 ) . unwrap ( ) . to_string ( )
9+ fn check_current_party_size ( party_size : & str ) -> String {
10+ if party_size != "__None" {
11+ party_size. trim_matches ( '[' ) . trim_matches ( ']' ) . split ( "," ) . next ( ) . unwrap_or ( "1" ) . to_string ( )
1312 } else {
14- return " ". to_string ( ) ;
13+ "1 ". to_string ( )
1514 }
1615}
17- fn check_max_party_size ( ) -> String {
18- let args = cli :: Cli :: parse ( ) ;
19- if args . party_size != "__None" {
20- args . party_size . trim_matches ( '[' ) . trim_matches ( ']' ) . split ( "," ) . nth ( 1 ) . unwrap ( ) . to_string ( )
16+
17+ fn check_max_party_size ( party_size : & str ) -> String {
18+ if party_size != "__None" {
19+ party_size. trim_matches ( '[' ) . trim_matches ( ']' ) . split ( "," ) . nth ( 1 ) . unwrap_or ( "1" ) . to_string ( )
2120 } else {
22- return " ". to_string ( ) ;
21+ "1 ". to_string ( )
2322 }
2423}
2524
26- fn main ( ) -> ( ) {
25+ fn main ( ) {
2726 let args = cli:: Cli :: parse ( ) ;
2827 let afk_rpc = args. afk_rpc ;
2928 let afk_after = args. afk_after ;
3029 let afk_update = args. afk_update ;
3130
32- if afk_rpc == false {
31+ if ! afk_rpc {
3332 let mut client = DiscordIpcClient :: new ( & args. clientid ) . expect ( "Failed to create client" ) ;
3433
3534 let state = args. state ;
@@ -42,8 +41,8 @@ fn main() -> () {
4241 let button_url_1 = args. button_url_1 ;
4342 let button_text_2 = args. button_text_2 ;
4443 let button_url_2 = args. button_url_2 ;
45- let current_party_size = & check_current_party_size ( ) ;
46- let max_party_size = & check_max_party_size ( ) ;
44+ let current_party_size = & check_current_party_size ( & args . party_size ) ;
45+ let max_party_size = & check_max_party_size ( & args . party_size ) ;
4746 let party_id = args. party_id ;
4847 let match_id = args. match_id ;
4948 let join_id = args. join_id ;
@@ -67,7 +66,7 @@ For more information try --help");
6766 println ! ( "button_text_1: {} button_url_1: {} button_text_2: {} button_url_2: {}" , button_text_1, button_url_1, button_text_2, button_url_2) ;
6867 exit ( 1 )
6968 }
70- if ( enable_time == true && args. start_time != -1 ) || ( enable_time == true && args. end_time != -1 ) {
69+ if ( enable_time && args. start_time != -1 ) || ( enable_time && args. end_time != -1 ) {
7170 println ! ( "{}{}" , "error: " . red( ) . bold( ) , "Start time and End time cannot be set while enable_time is true." . red( ) . bold( ) ) ;
7271 exit ( 1 )
7372 }
@@ -76,7 +75,7 @@ For more information try --help");
7675 println ! ( "{}{}" , "error: " . red( ) . bold( ) , "party_id has to be run with a party_size." . red( ) . bold( ) ) ;
7776 exit ( 1 )
7877 }
79- if ( match_id == "__None" && join_id != "__None" ) || ( match_id == "__None" && spectate_id != "__None" ) {
78+ if match_id == "__None" && ( join_id != "__None" || spectate_id != "__None" ) {
8079 println ! ( "{}{}" , "error: " . red( ) . bold( ) , "match_id is not specified." . red( ) . bold( ) ) ;
8180 exit ( 1 )
8281 }
@@ -97,38 +96,39 @@ For more information try --help");
9796 } else {
9897 activity_state
9998 } ;
100- let activity_large_image: Activity = if large_image != "__None" {
101- activity_details. assets ( activity:: Assets :: new ( ) . large_image ( & large_image) ) . clone ( )
99+ // Build assets incrementally
100+ let mut assets = activity:: Assets :: new ( ) ;
101+ if large_image != "__None" {
102+ assets = assets. large_image ( & large_image) ;
103+ }
104+ if large_text != "__None" {
105+ assets = assets. large_text ( & large_text) ;
106+ }
107+ if small_image != "__None" {
108+ assets = assets. small_image ( & small_image) ;
109+ }
110+ if small_text != "__None" {
111+ assets = assets. small_text ( & small_text) ;
112+ }
113+
114+ let activity_with_assets = if large_image != "__None" || large_text != "__None" ||
115+ small_image != "__None" || small_text != "__None" {
116+ activity_details. assets ( assets)
102117 } else {
103118 activity_details
104119 } ;
105- let activity_large_text : Activity = if large_text != "__None" {
106- activity_large_image . assets ( activity:: Assets :: new ( ) . large_image ( & large_image ) . large_text ( & large_text ) ) . clone ( )
120+ let activity_button_1 : Activity = if button_text_1 != "__None" && !button_url_1 . is_empty ( ) {
121+ activity_with_assets . buttons ( vec ! [ activity:: Button :: new( & button_text_1 , & button_url_1 ) ] ) . clone ( )
107122 } else {
108- activity_large_image
123+ activity_with_assets
109124 } ;
110- let activity_small_image: Activity = if small_image != "__None" {
111- activity_large_text. assets ( activity:: Assets :: new ( ) . large_image ( & large_image) . large_text ( & large_text) . small_image ( & small_image) ) . clone ( )
112- } else {
113- activity_large_text
114- } ;
115- let activity_small_text: Activity = if small_text != "__None" {
116- activity_small_image. assets ( activity:: Assets :: new ( ) . large_image ( & large_image) . large_text ( & large_text) . small_image ( & small_image) . small_text ( & small_text) ) . clone ( )
117- } else {
118- activity_small_image
119- } ;
120- let activity_button_1: Activity = if button_text_1 != "__None" && button_url_1 !="" {
121- activity_small_text. buttons ( vec ! [ activity:: Button :: new( & button_text_1, & button_url_1) ] ) . clone ( )
122- } else {
123- activity_small_text
124- } ;
125- let activity_button_2: Activity = if button_text_2 != "__None" && button_url_2 !="" {
125+ let activity_button_2: Activity = if button_text_2 != "__None" && !button_url_2. is_empty ( ) {
126126 activity_button_1. buttons ( vec ! [ activity:: Button :: new( & button_text_1, & button_url_1) , activity:: Button :: new( & button_text_2, & button_url_2) ] ) . clone ( )
127127 } else {
128128 activity_button_1
129129 } ;
130130 let time_unix = SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( ) . as_secs ( ) as i64 ;
131- let activity_time: Activity = if enable_time == true {
131+ let activity_time: Activity = if enable_time {
132132 activity_button_2. timestamps ( activity:: Timestamps :: new ( ) . start ( time_unix) ) . clone ( )
133133 } else {
134134 activity_button_2
@@ -144,12 +144,24 @@ For more information try --help");
144144 activity_start_time
145145 } ;
146146 let activity_party_size: Activity = if args. party_size != "__None" {
147- activity_end_time. party ( Party :: new ( ) . size ( [ current_party_size. parse :: < i32 > ( ) . unwrap ( ) , max_party_size. parse :: < i32 > ( ) . unwrap ( ) ] ) )
147+ let current_size = current_party_size. parse :: < i32 > ( ) . unwrap_or ( 1 ) ;
148+ let max_size = max_party_size. parse :: < i32 > ( ) . unwrap_or ( 1 ) ;
149+ if current_size < 1 || max_size < 1 || current_size > max_size {
150+ println ! ( "{}{}" , "error: " . red( ) . bold( ) , "Invalid party size values. Current size and max size must be positive, and current size cannot exceed max size." ) ;
151+ exit ( 1 ) ;
152+ }
153+ activity_end_time. party ( Party :: new ( ) . size ( [ current_size, max_size] ) )
148154 } else {
149155 activity_end_time
150156 } ;
151157 let activity_party_id: Activity = if party_id != "__None" {
152- activity_party_size. party ( Party :: new ( ) . size ( [ current_party_size. parse :: < i32 > ( ) . unwrap ( ) , max_party_size. parse :: < i32 > ( ) . unwrap ( ) ] ) . id ( & party_id) )
158+ let current_size = current_party_size. parse :: < i32 > ( ) . unwrap_or ( 1 ) ;
159+ let max_size = max_party_size. parse :: < i32 > ( ) . unwrap_or ( 1 ) ;
160+ if current_size < 1 || max_size < 1 || current_size > max_size {
161+ println ! ( "{}{}" , "error: " . red( ) . bold( ) , "Invalid party size values. Current size and max size must be positive, and current size cannot exceed max size." ) ;
162+ exit ( 1 ) ;
163+ }
164+ activity_party_size. party ( Party :: new ( ) . size ( [ current_size, max_size] ) . id ( & party_id) )
153165 } else {
154166 activity_party_size
155167 } ;
@@ -224,7 +236,10 @@ For more information try --help");
224236 else {
225237 println ! ( "Exiting in {} seconds" , args. exit_after. to_string( ) ) ; }
226238
227- thread:: sleep ( Duration :: from_secs ( args. exit_after . try_into ( ) . unwrap ( ) ) ) ;
239+ thread:: sleep ( Duration :: from_secs ( args. exit_after . try_into ( ) . unwrap_or_else ( |_| {
240+ println ! ( "{}{}" , "error: " . red( ) . bold( ) , "Invalid exit_after value" ) ;
241+ exit ( 1 ) ;
242+ } ) ) ) ;
228243 exit ( 0 )
229244 } else {
230245 if args. disable_color == false {
@@ -233,8 +248,7 @@ For more information try --help");
233248 println ! ( "Running indefinitely, Press Ctrl+C to exit." ) ; }
234249 loop { thread:: sleep ( Duration :: from_secs ( 10 ) ) ; } } // didn't have any better idea to do this
235250 }
236- if afk_rpc == true {
237-
251+ if afk_rpc {
238252 let mut client = DiscordIpcClient :: new ( "871404899915665438" ) . expect ( "Failed to create client" ) ;
239253 match client. connect ( ) {
240254 Ok ( _) => { println ! ( "Client connected to Discord successfully." ) ; } ,
@@ -269,10 +283,16 @@ For more information try --help");
269283 let mut connected = false ;
270284
271285 loop {
272- thread:: sleep ( Duration :: from_secs ( afk_update. try_into ( ) . expect ( "Failed to convert to seconds" ) ) ) ;
286+ thread:: sleep ( Duration :: from_secs ( afk_update. try_into ( ) . unwrap_or_else ( |_| {
287+ println ! ( "{}{}" , "error: " . red( ) . bold( ) , "Invalid afk_update value" ) ;
288+ exit ( 1 ) ;
289+ } ) ) ) ;
273290 let idle = UserIdle :: get_time ( ) . expect ( "Failed to get idle time" ) ;
274291
275- if idle. as_minutes ( ) >= afk_after. try_into ( ) . expect ( "Couldn't convert afk_after to u64" ) && connected == false {
292+ if idle. as_minutes ( ) >= afk_after. try_into ( ) . unwrap_or_else ( |_| {
293+ println ! ( "{}{}" , "error: " . red( ) . bold( ) , "Invalid afk_after value" ) ;
294+ exit ( 1 ) ;
295+ } ) && connected == false {
276296 client. connect ( ) . expect ( "Failed to connect to Discord" ) ;
277297
278298 match idle. as_seconds ( ) {
@@ -284,7 +304,10 @@ For more information try --help");
284304 connected = true ;
285305 }
286306
287- if connected == true && idle. as_minutes ( ) < afk_after. try_into ( ) . expect ( "Couldn't convert afk_after to u64" ) {
307+ if connected == true && idle. as_minutes ( ) < afk_after. try_into ( ) . unwrap_or_else ( |_| {
308+ println ! ( "{}{}" , "error: " . red( ) . bold( ) , "Invalid afk_after value" ) ;
309+ exit ( 1 ) ;
310+ } ) {
288311 client. clear_activity ( ) . expect ( "Failed to clear activity" ) ;
289312 connected = false ;
290313 } ;
0 commit comments