diff options
Diffstat (limited to 'src/commands/today/mod.rs')
| -rw-r--r-- | src/commands/today/mod.rs | 93 |
1 files changed, 41 insertions, 52 deletions
diff --git a/src/commands/today/mod.rs b/src/commands/today/mod.rs index 0a5eefe..0a0ba7b 100644 --- a/src/commands/today/mod.rs +++ b/src/commands/today/mod.rs @@ -1,106 +1,98 @@ -use serenity::{ - prelude::*, - model::{ - channel::Message, - }, - framework::standard::{ - Args, - macros::command, - }, -}; -use chrono::{Duration}; +use chrono::Duration; use either::Left; use lazy_static::lazy_static; +use log::debug; use rand::{ - thread_rng, seq::SliceRandom, + thread_rng, +}; +use serenity::{ + framework::standard::{ + macros::command, + Args, + CommandResult, + }, + model::channel::Message, + prelude::*, }; -use log::debug; use crate::{ - Result, - CtxExt, audio::{ PlayArgs, PlayQueue, }, + util, }; mod prelude; -mod sept_21; mod nov_5; +mod sept_21; -mod halloween; -mod ussr; mod france; -mod shrek; +mod halloween; mod putin; +mod shrek; +mod ussr; -mod wednesday; mod thursday; mod tomorrow; +mod wednesday; mod pianoman; -pub type TodayIter = Box<dyn Iterator<Item=TodayArgs>>; +pub type TodayIter = Box<dyn Iterator<Item = TodayArgs>>; #[derive(Clone, Debug, Hash, Default)] pub struct TodayArgs { - pub url: &'static str, + pub url: &'static str, pub start: Option<Duration>, - pub end: Option<Duration>, + pub end: Option<Duration>, } impl TodayArgs { #[inline] pub fn as_play_args(&self, msg: &Message) -> PlayArgs { PlayArgs { - initiator: "you have done this to yourself :^)".to_string(), - data: Left(self.url.to_owned()), + initiator: "you have done this to yourself :^)".to_string(), + data: Left(self.url.to_owned()), sender_channel: msg.channel_id, - start: self.start, - end: self.end, + start: self.start, + end: self.end, } } } - lazy_static! { - static ref ALL: Vec<fn(chrono::NaiveDateTime) -> TodayIter> = vec! [ + static ref ALL: Vec<fn(chrono::NaiveDateTime) -> TodayIter> = vec![ sept_21::sept_21, nov_5::nov_5, - halloween::halloween, ussr::ussr, france::france, shrek::shrek, putin::putin, - wednesday::wednesday, thursday::thursday, tomorrow::tomorrow, - pianoman::pianoman, ]; } - #[command] -pub fn today(ctx: &mut Context, msg: &Message, _args: Args) -> Result<()> { +pub async fn today(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { let today = { #[allow(unused_mut)] let mut result = chrono::Local::now().naive_local(); - #[cfg(debug_assertions)] { - let dt = _args.parse::<chrono::NaiveDateTime>() - .or_else(|_| { - _args.parse::<chrono::NaiveDate>() - .map(|date| { - let time = chrono::NaiveTime::from_hms_opt(12, 0, 0).unwrap(); - date.and_time(time) - }) - }); + #[cfg(debug_assertions)] + { + let dt = _args.parse::<chrono::NaiveDateTime>().or_else(|_| { + _args.parse::<chrono::NaiveDate>().map(|date| { + let time = chrono::NaiveTime::from_hms_opt(12, 0, 0).unwrap(); + date.and_time(time) + }) + }); match dt { Ok(dt) => { @@ -109,34 +101,31 @@ pub fn today(ctx: &mut Context, msg: &Message, _args: Args) -> Result<()> { }, Err(e) => { log::debug!("parsing datetime: {:?}", e); - } + }, }; } result }; - let options: Vec<TodayArgs> = ALL.iter() - .flat_map(|f| f(today)) - .collect(); + let options: Vec<TodayArgs> = ALL.iter().flat_map(|f| f(today)).collect(); debug!("{} options for {}", options.len(), today); - let play_args = options.choose(&mut thread_rng()) - .map(|x| x.as_play_args(msg)); + let play_args = options.choose(&mut thread_rng()).map(|x| x.as_play_args(msg)); if let Some(play_args) = play_args { play_args.data.as_ref().left().iter().for_each(|url| { debug!("today selected: {}", url); }); - let queue_lock = ctx.data.write().get::<PlayQueue>().cloned().unwrap(); + let queue_lock = ctx.data.write().await.get::<PlayQueue>().cloned().unwrap(); let mut play_queue = queue_lock.write().unwrap(); play_queue.general_queue.push_front(play_args); } else { - ctx.send(msg.channel_id, "no", false)?; - ctx.send(msg.channel_id, ":angry:", false)?; + util::send(ctx, msg.channel_id, "no", false).await?; + util::send(ctx, msg.channel_id, ":angry:", false).await?; } Ok(()) |
