From 8e567bf10b99202b29946abbbdd6dde55d98f531 Mon Sep 17 00:00:00 2001 From: jude Date: Fri, 23 Oct 2020 22:23:43 +0100 Subject: [PATCH] bumped to rc.7. embed avatar inside executable. nudge now outputs the current nudge when ran with no arguments --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 2 +- src/commands/reminder_cmds.rs | 54 ++++++++++++++++++++++------------- src/consts.rs | 18 +----------- 5 files changed, 38 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dd032b0..cee07bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1165,7 +1165,7 @@ dependencies = [ [[package]] name = "reminder_rs" -version = "1.0.0-rc.6" +version = "1.0.0-rc.7" dependencies = [ "Inflector", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index d5d2580..e6b3886 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "reminder_rs" -version = "1.0.0-rc.6" +version = "1.0.0-rc.7" authors = ["jellywx "] edition = "2018" diff --git a/README.md b/README.md index 36ccb98..5e640ec 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Serenity and Rust are proving wonders for SoundFX. This is all in an effort to r You'll need rustc and cargo for compilation. To run, you'll need Python 3 still (due to no suitable replacement for dateparser in Rust) ### Compiling -Reminder Bot can be built by running `cargo build --release` in the top level directory. +Reminder Bot can be built by running `cargo build --release` in the top level directory. It is necessary to create a folder called 'assets' containing an image file with its name specified in the environment as `WEBHOOK_AVATAR`, of dimensions 128x128px to be used as the webhook avatar. ### Setting up Python Reminder Bot by default looks for a venv within it's working directory to run Python out of. To set up a venv, install `python3-venv` and run `python3 -m venv venv`. Then, run `source venv/bin/activate` to activate the venv, and do `pip install dateparser` to install the required library diff --git a/src/commands/reminder_cmds.rs b/src/commands/reminder_cmds.rs index 8f0e874..f4ee058 100644 --- a/src/commands/reminder_cmds.rs +++ b/src/commands/reminder_cmds.rs @@ -6,7 +6,7 @@ use serenity::{ cache::Cache, client::Context, framework::standard::CommandResult, - http::{AttachmentType, CacheHttp}, + http::CacheHttp, model::{ channel::GuildChannel, channel::Message, @@ -23,7 +23,7 @@ use crate::{ check_subscription_on_message, consts::{ CHARACTERS, DAY, HOUR, LOCAL_TIMEZONE, MAX_TIME, MINUTE, MIN_INTERVAL, PYTHON_LOCATION, - REGEX_CHANNEL, REGEX_CHANNEL_USER, WEBHOOK_AVATAR, + REGEX_CHANNEL, REGEX_CHANNEL_USER, }, framework::SendIterator, models::{ChannelData, GuildData, Timer, UserData}, @@ -47,7 +47,6 @@ use std::{ convert::TryInto, default::Default, fmt::Display, - path::Path, string::ToString, time::{SystemTime, UNIX_EPOCH}, }; @@ -55,10 +54,17 @@ use std::{ use regex::Regex; fn shorthand_displacement(seconds: u64) -> String { + let (days, seconds) = seconds.div_rem(&DAY); let (hours, seconds) = seconds.div_rem(&HOUR); let (minutes, seconds) = seconds.div_rem(&MINUTE); - format!("{:02}:{:02}:{:02}", hours, minutes, seconds) + let time_repr = format!("{:02}:{:02}:{:02}", hours, minutes, seconds); + + if days > 0 { + format!("{} days, {}", days, time_repr) + } else { + time_repr + } } fn longhand_displacement(seconds: u64) -> String { @@ -84,15 +90,24 @@ async fn create_webhook( ctx: impl CacheHttp, channel: GuildChannel, name: impl Display, - avatar: Option, ) -> SerenityResult { - if let Some(path) = avatar { - channel - .create_webhook_with_avatar(ctx.http(), name, AttachmentType::from(Path::new(&path))) - .await - } else { - channel.create_webhook(ctx.http(), name).await - } + channel + .create_webhook_with_avatar( + ctx.http(), + name, + ( + include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/assets/", + env!( + "WEBHOOK_AVATAR", + "WEBHOOK_AVATAR not provided for compilation" + ) + )) as &[u8], + env!("WEBHOOK_AVATAR"), + ), + ) + .await } #[command] @@ -249,10 +264,12 @@ async fn nudge(ctx: &Context, msg: &Message, args: String) -> CommandResult { .unwrap(); if args.is_empty() { - let _ = msg - .channel_id - .say(&ctx, user_data.response(&pool, "nudge/invalid_time").await) - .await; + let content = user_data + .response(&pool, "nudge/no_argument") + .await + .replace("{nudge}", &format!("{}s", &channel.nudge.to_string())); + + let _ = msg.channel_id.say(&ctx, content).await; } else { let parser = TimeParser::new(args, user_data.timezone.parse().unwrap()); let nudge_time = parser.displacement(); @@ -1234,10 +1251,7 @@ async fn create_reminder, S: ToString + Type + Encode = env::var("WEBHOOK_AVATAR") - .ok() - .map(|s| { - let p = Path::new(&s); - - if !p.exists() { - warn!("WEBHOOK_AVATAR path doesn't exist. Falling back"); - - None - } else { - Some(s) - } - }) - .flatten(); }