2023-08-24 10:30:26 +00:00
use chrono ::TimeZone ;
2023-08-23 17:37:42 +00:00
use poise ::serenity_prelude ::* ;
use crate ::{ ManifoldContext , ManifoldData } ;
2023-08-24 09:27:15 +00:00
use chrono_tz ::Tz ;
2023-08-23 17:37:42 +00:00
use crate ::error ::{ ManifoldError , ManifoldResult } ;
2023-08-24 09:27:15 +00:00
use crate ::models ::user ::UserInfo ;
2023-08-23 17:37:42 +00:00
#[ poise::command(prefix_command, track_edits, slash_command) ]
async fn help (
ctx : ManifoldContext < '_ > ,
#[ description = " Help about a specific command " ] command : Option < String >
) -> ManifoldResult < ( ) > {
let responses = & ctx . data ( ) . responses ;
let config = poise ::builtins ::HelpConfiguration {
extra_text_at_bottom : & * format! ( " {} " , responses . get_response ( & " help footer " . to_string ( ) ) . unwrap_or ( & " " . to_string ( ) ) ) ,
.. Default ::default ( )
} ;
poise ::builtins ::help ( ctx , command . as_deref ( ) , config ) . await ? ;
Ok ( ( ) )
}
#[ poise::command(slash_command, prefix_command) ]
async fn ping ( ctx : ManifoldContext < '_ > , ) -> ManifoldResult < ( ) > {
let requestor = {
let calling_user = ctx . author ( ) ;
calling_user . mention ( ) . to_string ( )
} ;
ctx . say ( format! ( " {} : Ping? Pong! " , requestor ) ) . await ? ;
Ok ( ( ) )
}
#[ poise::command(slash_command, prefix_command) ]
async fn version ( ctx : ManifoldContext < '_ > ) -> ManifoldResult < ( ) > {
2023-08-24 09:00:16 +00:00
let version_string = & ctx . data ( ) . version_string ;
2023-08-23 17:37:42 +00:00
ctx . send ( | f | f
. reply ( true )
. content ( version_string ) ) . await ? ;
Ok ( ( ) )
}
2023-08-24 09:27:15 +00:00
#[ poise::command(slash_command, prefix_command, aliases( " st " )) ]
async fn set_timezone ( ctx : ManifoldContext < '_ > , input_timezone : String ) -> ManifoldResult < ( ) > {
let mut userinfo = ctx . data ( ) . user_info . lock ( ) . await ;
let validated_timezone : Tz = match input_timezone . parse ( ) {
Ok ( r ) = > r ,
Err ( e ) = > {
error! ( " Problem parsing timezone input: {:?} " , e ) ;
ctx . send ( | f | f . content ( format! ( " Are you having a laugh? What kind of timezone is that? {:?} " , e ) ) . reply ( true ) ) . await ? ;
Err ( e ) ?
}
2023-08-23 17:37:42 +00:00
} ;
2023-08-24 09:27:15 +00:00
if let Some ( user ) = userinfo . get_mut ( ctx . author ( ) . id . as_u64 ( ) ) {
user . timezone = Some ( validated_timezone . to_string ( ) ) ;
ctx . send ( | f | f . content ( format! ( " I have set your timezone to {} , but we all know that UTC is the One True Timezone. " , validated_timezone . to_string ( ) ) ) . reply ( true ) ) . await ? ;
} else {
let new_user = UserInfo {
user_id : ctx . author ( ) . id . as_u64 ( ) . to_owned ( ) as i64 ,
username : ctx . author ( ) . name . to_owned ( ) ,
weather_location : None ,
weather_units : None ,
timezone : Some ( validated_timezone . to_string ( ) ) ,
last_seen : Some ( chrono ::Utc ::now ( ) . timestamp ( ) )
} ;
userinfo . insert ( ctx . author ( ) . id . as_u64 ( ) . clone ( ) , new_user ) ;
ctx . send ( | f | f . content ( format! ( " I have set your timezone to {} , but we all know that UTC is the One True Timezone. " , validated_timezone . to_string ( ) ) ) . reply ( true ) ) . await ? ;
}
2023-08-23 17:37:42 +00:00
Ok ( ( ) )
}
2023-08-24 10:30:26 +00:00
#[ poise::command(slash_command, prefix_command, aliases( " t " )) ]
pub async fn time ( ctx : ManifoldContext < '_ > , target : u64 ) -> ManifoldResult < ( ) > {
let userinfo = ctx . data ( ) . user_info . lock ( ) . await ;
let user = match userinfo . get ( & target ) {
Some ( u ) = > match u . timezone . to_owned ( ) {
Some ( t ) = > u ,
None = > { ctx . send ( | f | f . content ( format! ( " This user hasn't given me their timezone yet. Shame on them! " ) ) . reply ( true ) ) . await ? ; Err ( " NoTZ " ) ? }
} ,
None = > { ctx . send ( | f | f . content ( format! ( " Who's that? I don't know them. " ) ) . reply ( true ) ) . await ? ; Err ( " User not found " ) ? }
} ;
let current_time_utc = chrono ::Utc ::now ( ) ;
let user_timezone : Tz = user . timezone . as_ref ( ) . unwrap ( ) . parse ::< Tz > ( ) . unwrap ( ) ;
let adjusted_time = user_timezone . from_utc_datetime ( & current_time_utc . naive_utc ( ) ) ;
ctx . send ( | f | f . content ( format! ( " Time in {} 's current timezone of {} is {} " , user . username , user_timezone . name ( ) . to_string ( ) , adjusted_time . time ( ) . to_string ( ) ) ) . reply ( true ) ) . await ? ;
Ok ( ( ) )
}
pub fn commands ( ) -> [ poise ::Command < ManifoldData , ManifoldError > ; 5 ] {
[ help ( ) , ping ( ) , version ( ) , set_timezone ( ) , time ( ) ]
2023-08-23 17:37:42 +00:00
}