2021-09-06 12:46:16 +00:00
use chrono ::offset ::Utc ;
use chrono_tz ::{ Tz , TZ_VARIANTS } ;
use levenshtein ::levenshtein ;
2020-08-18 19:09:21 +00:00
2022-09-12 15:45:00 +00:00
use super ::autocomplete ::timezone_autocomplete ;
use crate ::{ consts ::THEME_COLOR , models ::CtxData , Context , Error } ;
2020-09-28 15:11:27 +00:00
2022-02-19 14:32:03 +00:00
/// Select your timezone
2022-02-20 12:19:39 +00:00
#[ poise::command(slash_command, identifying_name = " timezone " ) ]
2022-02-19 14:32:03 +00:00
pub async fn timezone (
ctx : Context < '_ > ,
#[ description = " Timezone to use from this list: https://gist.github.com/JellyWX/913dfc8b63d45192ad6cb54c829324ee " ]
#[ autocomplete = " timezone_autocomplete " ]
timezone : Option < String > ,
) -> Result < ( ) , Error > {
let mut user_data = ctx . author_data ( ) . await . unwrap ( ) ;
2021-09-10 23:14:23 +00:00
let footer_text = format! ( " Current timezone: {} " , user_data . timezone ) ;
2022-02-19 14:32:03 +00:00
if let Some ( timezone ) = timezone {
2021-09-10 23:14:23 +00:00
match timezone . parse ::< Tz > ( ) {
Ok ( tz ) = > {
user_data . timezone = timezone . clone ( ) ;
2022-02-19 14:32:03 +00:00
user_data . commit_changes ( & ctx . data ( ) . database ) . await ;
2020-08-27 20:37:44 +00:00
2021-09-10 23:14:23 +00:00
let now = Utc ::now ( ) . with_timezone ( & tz ) ;
2022-02-19 14:32:03 +00:00
ctx . send ( | m | {
m . embed ( | e | {
e . title ( " Timezone Set " )
. description ( format! (
" Timezone has been set to **{}**. Your current time should be `{}` " ,
timezone ,
2022-05-13 22:08:52 +00:00
now . format ( " %H:%M " )
2022-02-19 14:32:03 +00:00
) )
. color ( * THEME_COLOR )
} )
} )
. await ? ;
2020-08-29 17:07:15 +00:00
}
2020-08-27 20:37:44 +00:00
2020-08-29 17:07:15 +00:00
Err ( _ ) = > {
2020-11-22 23:58:46 +00:00
let filtered_tz = TZ_VARIANTS
. iter ( )
2021-06-27 15:31:54 +00:00
. filter ( | tz | {
2021-09-10 23:14:23 +00:00
timezone . contains ( & tz . to_string ( ) )
2022-02-19 14:32:03 +00:00
| | tz . to_string ( ) . contains ( & timezone )
| | levenshtein ( & tz . to_string ( ) , & timezone ) < 4
2021-06-27 15:31:54 +00:00
} )
2020-11-22 23:58:46 +00:00
. take ( 25 )
2021-06-27 15:31:54 +00:00
. map ( | t | t . to_owned ( ) )
. collect ::< Vec < Tz > > ( ) ;
let fields = filtered_tz . iter ( ) . map ( | tz | {
(
tz . to_string ( ) ,
2022-05-13 22:08:52 +00:00
format! ( " 🕗 ` {} ` " , Utc ::now ( ) . with_timezone ( tz ) . format ( " %H:%M " ) ) ,
2021-06-27 15:31:54 +00:00
true ,
)
} ) ;
2020-11-22 23:58:46 +00:00
2022-02-19 14:32:03 +00:00
ctx . send ( | m | {
m . embed ( | e | {
e . title ( " Timezone Not Recognized " )
. description ( " Possibly you meant one of the following timezones, otherwise click [here](https://gist.github.com/JellyWX/913dfc8b63d45192ad6cb54c829324ee): " )
. color ( * THEME_COLOR )
. fields ( fields )
. footer ( | f | f . text ( footer_text ) )
. url ( " https://gist.github.com/JellyWX/913dfc8b63d45192ad6cb54c829324ee " )
} )
} )
. await ? ;
2020-08-29 17:07:15 +00:00
}
2020-08-27 20:37:44 +00:00
}
2020-10-12 20:01:27 +00:00
} else {
2022-02-19 14:32:03 +00:00
let popular_timezones_iter = ctx . data ( ) . popular_timezones . iter ( ) . map ( | t | {
2022-05-13 22:08:52 +00:00
( t . to_string ( ) , format! ( " 🕗 ` {} ` " , Utc ::now ( ) . with_timezone ( t ) . format ( " %H:%M " ) ) , true )
2020-11-22 23:58:46 +00:00
} ) ;
2022-02-19 14:32:03 +00:00
ctx . send ( | m | {
m . embed ( | e | {
e . title ( " Timezone Usage " )
. description (
" **Usage:**
2021-09-10 23:14:23 +00:00
` / timezone Name `
* * Example :* *
` / timezone Europe / London `
You may want to use one of the popular timezones below , otherwise click [ here ] ( https ://gist.github.com/JellyWX/913dfc8b63d45192ad6cb54c829324ee):",
2022-02-19 14:32:03 +00:00
)
. color ( * THEME_COLOR )
. fields ( popular_timezones_iter )
. footer ( | f | f . text ( footer_text ) )
. url ( " https://gist.github.com/JellyWX/913dfc8b63d45192ad6cb54c829324ee " )
} )
} )
. await ? ;
2020-08-29 17:07:15 +00:00
}
2022-02-19 14:32:03 +00:00
Ok ( ( ) )
2020-08-27 11:15:20 +00:00
}
2020-08-29 19:57:11 +00:00
2022-07-29 18:22:15 +00:00
/// Configure whether other users can set reminders to your direct messages
#[ poise::command(slash_command, rename = " dm " , identifying_name = " allowed_dm " ) ]
pub async fn allowed_dm ( _ctx : Context < '_ > ) -> Result < ( ) , Error > {
Ok ( ( ) )
}
/// Allow other users to set reminders in your direct messages
#[ poise::command(slash_command, rename = " allow " , identifying_name = " allowed_dm " ) ]
pub async fn set_allowed_dm ( ctx : Context < '_ > ) -> Result < ( ) , Error > {
let mut user_data = ctx . author_data ( ) . await ? ;
user_data . allowed_dm = true ;
user_data . commit_changes ( & ctx . data ( ) . database ) . await ;
ctx . send ( | r | {
r . ephemeral ( true ) . embed ( | e | {
e . title ( " DMs permitted " )
. description ( " You will receive a message if a user sets a DM reminder for you. " )
. color ( * THEME_COLOR )
} )
} )
. await ? ;
Ok ( ( ) )
}
/// Block other users from setting reminders in your direct messages
#[ poise::command(slash_command, rename = " block " , identifying_name = " allowed_dm " ) ]
pub async fn unset_allowed_dm ( ctx : Context < '_ > ) -> Result < ( ) , Error > {
let mut user_data = ctx . author_data ( ) . await ? ;
user_data . allowed_dm = false ;
user_data . commit_changes ( & ctx . data ( ) . database ) . await ;
ctx . send ( | r | {
r . ephemeral ( true ) . embed ( | e | {
e . title ( " DMs blocked " )
. description (
" You can still set DM reminders for yourself or for users with DMs enabled. " ,
)
. color ( * THEME_COLOR )
} )
} )
. await ? ;
Ok ( ( ) )
}
2022-06-17 16:15:48 +00:00
/// View the webhook being used to send reminders to this channel
#[ poise::command(
slash_command ,
identifying_name = " webhook_url " ,
required_permissions = " ADMINISTRATOR "
) ]
pub async fn webhook ( ctx : Context < '_ > ) -> Result < ( ) , Error > {
match ctx . channel_data ( ) . await {
Ok ( data ) = > {
if let ( Some ( id ) , Some ( token ) ) = ( data . webhook_id , data . webhook_token ) {
let _ = ctx
. send ( | b | {
b . ephemeral ( true ) . content ( format! (
" **Warning!**
This link can be used by users to anonymously send messages , with or without permissions .
Do not share it !
| | https ://discord.com/api/webhooks/{}/{} ||",
id , token ,
) )
} )
. await ;
} else {
let _ = ctx . say ( " No webhook configured on this channel. " ) . await ;
}
}
Err ( _ ) = > {
let _ = ctx . say ( " No webhook configured on this channel. " ) . await ;
}
}
Ok ( ( ) )
}