Add time and timezone command

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/2/head
trivernis 4 years ago
parent 750ee7d0dc
commit 7005fa7317
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

20
Cargo.lock generated

@ -225,6 +225,16 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "chrono-tz"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2554a3155fec064362507487171dcc4edc3df60cb10f3a1fb10ed8094822b120"
dependencies = [
"chrono",
"parse-zoneinfo",
]
[[package]] [[package]]
name = "cipher" name = "cipher"
version = "0.2.5" version = "0.2.5"
@ -1259,6 +1269,15 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "parse-zoneinfo"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c705f256449c60da65e11ff6626e0c16a0a0b96aaa348de61376b249bc340f41"
dependencies = [
"regex",
]
[[package]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.1.0" version = "2.1.0"
@ -2099,6 +2118,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"aspotify", "aspotify",
"chrono", "chrono",
"chrono-tz",
"colored", "colored",
"database", "database",
"dotenv", "dotenv",

@ -30,3 +30,4 @@ colored = "2.0.0"
sysinfo = "0.16.5" sysinfo = "0.16.5"
database = {path="./database"} database = {path="./database"}
reqwest = "0.11.2" reqwest = "0.11.2"
chrono-tz = "0.5.3"

@ -4,13 +4,17 @@ use pekofy::PEKOFY_COMMAND;
use ping::PING_COMMAND; use ping::PING_COMMAND;
use shutdown::SHUTDOWN_COMMAND; use shutdown::SHUTDOWN_COMMAND;
use stats::STATS_COMMAND; use stats::STATS_COMMAND;
use time::TIME_COMMAND;
use timezones::TIMEZONES_COMMAND;
pub(crate) mod help; pub(crate) mod help;
mod pekofy; mod pekofy;
mod ping; mod ping;
mod shutdown; mod shutdown;
mod stats; mod stats;
mod time;
mod timezones;
#[group] #[group]
#[commands(ping, stats, shutdown, pekofy)] #[commands(ping, stats, shutdown, pekofy, time, timezones)]
pub struct Misc; pub struct Misc;

@ -0,0 +1,68 @@
use chrono::prelude::*;
use chrono_tz::Tz;
use serenity::client::Context;
use serenity::framework::standard::macros::command;
use serenity::framework::standard::{Args, CommandResult};
use serenity::model::channel::Message;
#[command]
#[description("Converts a time into a different timezone")]
#[min_args(1)]
#[max_args(3)]
#[usage("<%H:%M/now> (<from-timezone>) (<to-timezone>)")]
async fn time(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let when = args.single::<String>().unwrap_or("now".to_string());
let first_timezone = args.single::<String>().ok();
let second_timezone = args.single::<String>().ok();
let from_timezone: Tz = if let Some(first) = &first_timezone {
first.parse::<Tz>()?
} else {
Tz::UTC
};
let to_timezone = if let Some(second) = &second_timezone {
second.parse::<Tz>()?
} else {
Tz::UTC
};
let time = if when.to_lowercase() == "now" {
Utc::now().with_timezone(&from_timezone)
} else {
let now = Utc::now();
if second_timezone.is_some() {
from_timezone.datetime_from_str(
&format!("{} {}:00", now.format("%Y-%m-%d"), &*when),
"%Y-%m-%d %H:%M:%S",
)?
} else {
let timezone: Tz = "UTC".parse().unwrap();
timezone
.datetime_from_str(
&format!("{} {}:00", now.format("%Y-%m-%d"), &*when),
"%Y-%m-%d %H:%M:%S",
)?
.with_timezone(&from_timezone)
}
};
if second_timezone.is_some() {
msg.channel_id
.say(
ctx,
format!(
"{} is {}",
time.format("%H:%M %Z"),
time.with_timezone(&to_timezone).format("%H:%M %Z"),
),
)
.await?;
} else {
msg.channel_id
.say(ctx, format!("{}", time.format("%H:%M %Z")))
.await?;
}
Ok(())
}

@ -0,0 +1,38 @@
use serenity::client::Context;
use serenity::framework::standard::macros::command;
use serenity::framework::standard::{Args, CommandResult};
use serenity::model::channel::Message;
#[command]
#[description("Searches for timezones matching the query")]
#[min_args(1)]
#[usage("<query...>")]
#[example("Europe Berlin")]
async fn timezones(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let query = args
.iter::<String>()
.map(|s| s.unwrap().to_lowercase())
.collect::<Vec<String>>();
let mut variants: Vec<String> = chrono_tz::TZ_VARIANTS
.iter()
.map(|t| t.to_string())
.filter(|name| query.iter().all(|q| name.to_lowercase().contains(q)))
.collect();
if variants.len() > 20 {
let remaining = variants.len() - 20;
variants = variants[0..20].to_vec();
variants.push(format!("*and {} more...*", remaining));
}
let mut variants = variants.join("\n");
if variants.is_empty() {
variants = "*nothing found*".to_string();
}
msg.channel_id
.send_message(ctx, |m| {
m.embed(|e| e.title("Available Timezones").description(variants))
})
.await?;
Ok(())
}
Loading…
Cancel
Save