diff options
| -rw-r--r-- | src/commands/today.rs | 109 | ||||
| -rw-r--r-- | src/commands/today/halloween.rs | 20 | ||||
| -rw-r--r-- | src/commands/today/mod.rs | 117 | ||||
| -rw-r--r-- | src/commands/today/nov_5.rs | 17 | ||||
| -rw-r--r-- | src/commands/today/prelude.rs | 31 | ||||
| -rw-r--r-- | src/commands/today/sept_21.rs | 41 | ||||
| -rw-r--r-- | src/commands/today/tomorrow.rs | 13 | ||||
| -rw-r--r-- | src/commands/today/wednesday.rs | 13 |
8 files changed, 252 insertions, 109 deletions
diff --git a/src/commands/today.rs b/src/commands/today.rs deleted file mode 100644 index eea323c..0000000 --- a/src/commands/today.rs +++ /dev/null @@ -1,109 +0,0 @@ -use serenity::{ - prelude::*, - model::{ - channel::Message, - }, - framework::standard::{ - Args, - macros::command, - }, -}; -use chrono::{Datelike, Duration}; -use either::Left; -use lazy_static::lazy_static; -use rand::{ - thread_rng, - seq::SliceRandom, -}; - -use crate::{ - Result, - CtxExt, - audio::{ - PlayArgs, - PlayQueue, - }, -}; - -#[derive(Clone, Debug, Hash, Default)] -struct TodayArgs { - url: &'static str, - start: Option<Duration>, - end: Option<Duration>, -} - -impl TodayArgs { - #[inline] - fn as_play_args(&self, msg: &Message) -> PlayArgs { - PlayArgs { - 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, - } - } -} - -lazy_static! { - static ref SEPT_21_CHOICES: Vec<TodayArgs> = vec![ - TodayArgs { - url: "https://www.youtube.com/watch?v=kPwG6L73-VU", - ..Default::default() - }, - TodayArgs { - url: "https://www.youtube.com/watch?v=fPpUYXZb2AA", - ..Default::default() - }, - TodayArgs { - url: "https://www.youtube.com/watch?v=CG7YHFT4hjw", - end: Some(Duration::seconds(69)), - ..Default::default() - }, - TodayArgs { - url: "https://www.youtube.com/watch?v=_hpU6UEq8hA", - end: Some(Duration::seconds(67)), - ..Default::default() - }, - TodayArgs { - url: "https://www.youtube.com/watch?v=_zzEDrYTkkg", - end: Some(Duration::seconds(68)), - ..Default::default() - }, - TodayArgs { - url: "https://www.youtube.com/watch?v=Gs069dndIYk", - ..Default::default() - }, - ]; -} - -#[command] -pub fn today(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { - let today = chrono::Local::today().naive_local(); - - let args: Option<PlayArgs> = match (today.month(), today.day()) { - (9, 21) => SEPT_21_CHOICES.choose(&mut thread_rng()) - .map(|choice| choice.as_play_args(msg)), - _ => { - let result = TodayArgs { - url: "https://www.youtube.com/watch?v=W78AGkm_AtE", - start: None, - end: Some(Duration::seconds(6)) - }.as_play_args(msg); - - Some(result) - }, - }; - - if let Some(args) = args { - let queue_lock = ctx.data.write().get::<PlayQueue>().cloned().unwrap(); - let mut play_queue = queue_lock.write().unwrap(); - - play_queue.general_queue.push_front(args); - } else { - ctx.send(msg.channel_id, "no", false)?; - ctx.send(msg.channel_id, ":angry:", false)?; - } - - Ok(()) -} diff --git a/src/commands/today/halloween.rs b/src/commands/today/halloween.rs new file mode 100644 index 0000000..847da8e --- /dev/null +++ b/src/commands/today/halloween.rs @@ -0,0 +1,20 @@ +use super::prelude::*; + +lazy_static! { + static ref HALLOWEEN: Vec<TodayArgs> = vec![ + TodayArgs { + url: "https://www.youtube.com/watch?v=-1dSY6ZuXEY", + ..Default::default() + }, + ]; +} + +pub fn halloween(date: chrono::NaiveDate) -> TodayIter { + if (10, 31) != month_day(date) { + return Box::new(empty()); + } + + Box::new( + HALLOWEEN.iter().cloned() + ) +} diff --git a/src/commands/today/mod.rs b/src/commands/today/mod.rs new file mode 100644 index 0000000..bf9a439 --- /dev/null +++ b/src/commands/today/mod.rs @@ -0,0 +1,117 @@ +use serenity::{ + prelude::*, + model::{ + channel::Message, + }, + framework::standard::{ + Args, + macros::command, + }, +}; +use chrono::{Duration}; +use either::Left; +use lazy_static::lazy_static; +use rand::{ + thread_rng, + seq::SliceRandom, +}; +use log::debug; + +use crate::{ + Result, + CtxExt, + audio::{ + PlayArgs, + PlayQueue, + }, +}; + +mod prelude; + +mod sept_21; +mod nov_5; + +mod halloween; + +mod wednesday; + +mod tomorrow; + +pub type TodayIter = Box<dyn Iterator<Item=TodayArgs>>; + +#[derive(Clone, Debug, Hash, Default)] +pub struct TodayArgs { + pub url: &'static str, + pub start: 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()), + sender_channel: msg.channel_id, + start: self.start, + end: self.end, + } + } +} + + +lazy_static! { + static ref ALL: Vec<fn(chrono::NaiveDate) -> TodayIter> = vec! [ + sept_21::sept_21, + nov_5::nov_5, + + halloween::halloween, + + wednesday::wednesday, + + tomorrow::tomorrow, + ]; +} + + +#[command] +pub fn today(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { + let today = { + let mut result = chrono::Local::today().naive_local(); + + #[cfg(debug_assertions)] { + match args.parse::<chrono::NaiveDate>() { + Ok(date) => { + log::debug!("overriding with date: {}", date); + result = date; + }, + Err(e) => { + log::debug!("parsing date: {:?}", e); + } + }; + } + + result + }; + + let options: Vec<TodayArgs> = ALL.iter() + .flat_map(|f| f(today)) + .collect(); + + debug!("selected {} options for {}", options.len(), today); + + let play_args = options.choose(&mut thread_rng()) + .map(|x| x.as_play_args(msg)); + + if let Some(play_args) = play_args { + let queue_lock = ctx.data.write().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)?; + } + + Ok(()) +} diff --git a/src/commands/today/nov_5.rs b/src/commands/today/nov_5.rs new file mode 100644 index 0000000..30d4f79 --- /dev/null +++ b/src/commands/today/nov_5.rs @@ -0,0 +1,17 @@ +use super::prelude::*; + +pub fn nov_5(date: chrono::NaiveDate) -> TodayIter { + if (11, 5) != month_day(date) { + return Box::new(empty()); + } + + Box::new( + once( + TodayArgs { + url: "https://www.youtube.com/watch?v=LF1951pENdk", + start: Some(Duration::seconds(25)), + end: Some(Duration::seconds(39)), + } + ) + ) +} diff --git a/src/commands/today/prelude.rs b/src/commands/today/prelude.rs new file mode 100644 index 0000000..d0570fd --- /dev/null +++ b/src/commands/today/prelude.rs @@ -0,0 +1,31 @@ +pub use std::iter::{ + once, + empty, +}; + +pub use lazy_static::lazy_static; + +pub use chrono::{ + Datelike, + Duration, +}; + +pub use super::{ + TodayArgs, + TodayIter, +}; + + +#[inline] +pub fn month_day(date: chrono::NaiveDate) -> (u32, u32) { + (date.month(), date.day()) +} + +pub const fn by_url(url: &'static str) -> TodayArgs { + TodayArgs { + url, + + start: None, + end: None, + } +} diff --git a/src/commands/today/sept_21.rs b/src/commands/today/sept_21.rs new file mode 100644 index 0000000..06689e8 --- /dev/null +++ b/src/commands/today/sept_21.rs @@ -0,0 +1,41 @@ +use super::prelude::*; + +lazy_static! { + static ref SEPT_21_CHOICES: Vec<TodayArgs> = vec![ + TodayArgs { + url: "https://www.youtube.com/watch?v=kPwG6L73-VU", + ..Default::default() + }, + TodayArgs { + url: "https://www.youtube.com/watch?v=fPpUYXZb2AA", + ..Default::default() + }, + TodayArgs { + url: "https://www.youtube.com/watch?v=CG7YHFT4hjw", + end: Some(Duration::seconds(69)), + ..Default::default() + }, + TodayArgs { + url: "https://www.youtube.com/watch?v=_hpU6UEq8hA", + end: Some(Duration::seconds(67)), + ..Default::default() + }, + TodayArgs { + url: "https://www.youtube.com/watch?v=_zzEDrYTkkg", + end: Some(Duration::seconds(68)), + ..Default::default() + }, + TodayArgs { + url: "https://www.youtube.com/watch?v=Gs069dndIYk", + ..Default::default() + }, + ]; +} + +pub fn sept_21(date: chrono::NaiveDate) -> TodayIter { + if (9, 21) != (date.month(), date.day()) { + return Box::new(empty()); + } + + Box::new(SEPT_21_CHOICES.iter().cloned()) +} diff --git a/src/commands/today/tomorrow.rs b/src/commands/today/tomorrow.rs new file mode 100644 index 0000000..eb19250 --- /dev/null +++ b/src/commands/today/tomorrow.rs @@ -0,0 +1,13 @@ +use super::prelude::*; + +pub fn tomorrow(_date: chrono::NaiveDate) -> TodayIter { + Box::new( + once( + TodayArgs { + url: "https://www.youtube.com/watch?v=W78AGkm_AtE", + start: None, + end: Some(Duration::seconds(6)) + } + ) + ) +} diff --git a/src/commands/today/wednesday.rs b/src/commands/today/wednesday.rs new file mode 100644 index 0000000..8fec707 --- /dev/null +++ b/src/commands/today/wednesday.rs @@ -0,0 +1,13 @@ +use super::prelude::*; + +pub fn wednesday(date: chrono::NaiveDate) -> TodayIter { + if date.weekday() != chrono::Weekday::Wed { + return Box::new(empty()); + } + + Box::new( + once( + by_url("https://www.youtube.com/watch?v=du-TY1GUFGk") + ) + ) +} |
