diff options
Diffstat (limited to 'src/commands')
| -rw-r--r-- | src/commands/meme/create.rs | 6 | ||||
| -rw-r--r-- | src/commands/meme/history.rs | 124 | ||||
| -rw-r--r-- | src/commands/meme/mod.rs | 10 | ||||
| -rw-r--r-- | src/commands/mod.rs | 3 | ||||
| -rw-r--r-- | src/commands/playback.rs | 6 |
5 files changed, 78 insertions, 71 deletions
diff --git a/src/commands/meme/create.rs b/src/commands/meme/create.rs index 1c12f2a..ef6090e 100644 --- a/src/commands/meme/create.rs +++ b/src/commands/meme/create.rs @@ -110,6 +110,8 @@ pub async fn addmeme(ctx: &Context, msg: &Message, args: Args) -> CommandResult #[command] pub async fn addaudiomeme(ctx: &Context, msg: &Message, args: Args) -> CommandResult { + debug!("running addaudiomeme"); + let mut args = Args::new(args.rest(), DELIMS.as_ref()); let title = args.single_quoted::<String>()?; @@ -117,7 +119,7 @@ pub async fn addaudiomeme(ctx: &Context, msg: &Message, args: Args) -> CommandRe let elems = audio_str.split_whitespace().collect::<Vec<_>>(); - if elems.len() == 0 { + if elems.is_empty() { util::send(ctx, msg.channel_id, "are you stupid", msg.tts).await?; return Err(anyhow!("no audio link was provided").into()); } @@ -127,6 +129,7 @@ pub async fn addaudiomeme(ctx: &Context, msg: &Message, args: Args) -> CommandRe let (start, end) = parse_times(opts); let youtube_url = util::ytdl_url(audio_link.as_str()).await?; + debug!("got download url: {youtube_url}"); let duration_opts = if let Some(e) = end { vec![ @@ -184,6 +187,7 @@ pub async fn addaudiomeme(ctx: &Context, msg: &Message, args: Args) -> CommandRe let mut audio_data = Vec::new(); let bytes = audio_reader.read_to_end(&mut audio_data).await?; + debug!("downloaded audio ({} bytes)", audio_data.len()); if bytes == 0 { debug!("read 0 bytes from audio reader"); diff --git a/src/commands/meme/history.rs b/src/commands/meme/history.rs index ed50e27..33a2de2 100644 --- a/src/commands/meme/history.rs +++ b/src/commands/meme/history.rs @@ -2,7 +2,6 @@ use anyhow::anyhow; use diesel::{ result::Error as DieselError, NotFound, - PgConnection, }; use itertools::Itertools; use lazy_static::lazy_static; @@ -115,8 +114,6 @@ pub async fn wat(ctx: &Context, msg: &Message, _: Args) -> CommandResult { #[command] pub async fn history(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { - let mut conn = connection()?; - let n = args.single_quoted::<usize>().unwrap_or(CONFIG.default_hist); if n > CONFIG.max_hist { @@ -126,7 +123,10 @@ pub async fn history(ctx: &Context, msg: &Message, mut args: Args) -> CommandRes let n = n.min(CONFIG.max_hist); - let records = InvocationRecord::last_n(&mut conn, n)?; + let records = { + let mut conn = connection()?; + InvocationRecord::last_n(&mut conn, n)? + }; if records.len() == 0 { info!("no memes in history"); @@ -138,74 +138,70 @@ pub async fn history(ctx: &Context, msg: &Message, mut args: Args) -> CommandRes info!("reporting meme history (len {})", n); let resp = serenity::futures::stream::iter(records.into_iter().enumerate().rev()) - .then(|(i, rec)| ir_info(ctx, i, rec, &mut conn)) - .try_collect::<Vec<String>>() - .await?; - - let resp = resp.join("\n"); - - util::send(ctx, msg.channel_id, &resp, false).await.map_err(CommandError::from) -} + .then(|(i, rec)| async move { + let mut conn = connection()?; -async fn ir_info( - ctx: &Context, - i: usize, - rec: InvocationRecord, - conn: &mut PgConnection, -) -> Result<String, CommandError> { - let dt = chrono::DateTime::from_utc(rec.time, chrono::Utc {}); - let ago = TIME_FORMATTER.convert((chrono::Utc::now() - dt).to_std().unwrap()); - - let rand = if rec.random { - "R, " - } else { - "" - }; + let dt = chrono::DateTime::from_utc(rec.time, chrono::Utc {}); + let ago = TIME_FORMATTER.convert((chrono::Utc::now() - dt).to_std().unwrap()); - let meme = Meme::find(conn, rec.meme_id) - .and_then(|meme| Metadata::find(conn, meme.metadata_id).map(|metadata| (metadata, meme))); + let rand = if rec.random { + "R, " + } else { + "" + }; - let invoker_name = CONFIG - .discord - .guild() - .member(&ctx, rec.user_id as u64) - .await - .map(|m| m.display_name().to_owned()) - .unwrap_or("???".to_owned()); + let meme = Meme::find(&mut conn, rec.meme_id).and_then(|meme| { + Metadata::find(&mut conn, meme.metadata_id).map(|metadata| (metadata, meme)) + }); - let result = match meme { - Ok((metadata, meme)) => { - let author_name = CONFIG + let invoker_name = CONFIG .discord .guild() - .member(&ctx, metadata.created_by as u64) + .member(&ctx, rec.user_id as u64) .await .map(|m| m.display_name().to_owned()) .unwrap_or("???".to_owned()); - format!( - "{}. [{}{}] \"{}\" by {} ({}). invoked by {}.", - i + 1, - rand, - ago, - meme.title, - author_name, - metadata.created.date().format(CLEAN_DATE_FORMAT), - invoker_name - ) - }, - Err(e) => { - if let Some(variant) = e.downcast_ref::<DieselError>() { - if *variant != NotFound { - error!("error encountered loading meme history: {}", e); - } - } + let result = match meme { + Ok((metadata, meme)) => { + let author_name = CONFIG + .discord + .guild() + .member(&ctx, metadata.created_by as u64) + .await + .map(|m| m.display_name().to_owned()) + .unwrap_or("???".to_owned()); - format!("{}. [{}{}] not found. invoked by {}.", i + 1, rand, ago, invoker_name) - }, - }; + format!( + "{}. [{}{}] \"{}\" by {} ({}). invoked by {}.", + i + 1, + rand, + ago, + meme.title, + author_name, + metadata.created.date().format(CLEAN_DATE_FORMAT), + invoker_name + ) + }, + Err(e) => { + if let Some(variant) = e.downcast_ref::<DieselError>() { + if *variant != NotFound { + error!("error encountered loading meme history: {}", e); + } + } + + format!("{}. [{}{}] not found. invoked by {}.", i + 1, rand, ago, invoker_name) + }, + }; - Ok(result) + Result::<_, CommandError>::Ok(result) + }) + .try_collect::<Vec<String>>() + .await?; + + let resp = resp.join("\n"); + + util::send(ctx, msg.channel_id, &resp, false).await.map_err(CommandError::from) } #[command] @@ -328,12 +324,12 @@ pub async fn query(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul static ref AGE_REGEX: Regex = Regex::new(r"(?i)(?:age|order)=(.*)").unwrap(); } - let guild = - msg.channel_id.to_channel(&ctx).await?.guild().ok_or(anyhow!("couldn't find guild"))?; + let creator: Option<u64> = { + let guild = + msg.channel_id.to_channel(&ctx).await?.guild().ok_or(anyhow!("couldn't find guild"))?; - let guild = guild.guild(&ctx).ok_or(anyhow!("couldn't find guild"))?; + let guild = guild.guild(&ctx).ok_or(anyhow!("couldn't find guild"))?; - let creator: Option<u64> = { let creator = args.quoted().current().map(|s| CREATOR_REGEX.is_match(s)).unwrap_or(false); if creator { args.single_quoted::<String>() diff --git a/src/commands/meme/mod.rs b/src/commands/meme/mod.rs index 24fc50d..fe69b1c 100644 --- a/src/commands/meme/mod.rs +++ b/src/commands/meme/mod.rs @@ -16,7 +16,10 @@ use serenity::{ prelude::*, }; use songbird::input::{ - core::io::MediaSource, + core::{ + io::MediaSource, + probe::Hint, + }, AudioStream, AudioStreamError, Compose, @@ -127,9 +130,12 @@ impl Compose for Audio { let ms = std::io::Cursor::new(self.data.clone()); let ms: Box<dyn MediaSource> = Box::new(ms); + let mut hint = Hint::new(); + hint.mime_type("audio/opus"); + Ok(AudioStream { input: ms, - hint: None, + hint: Some(hint), }) } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index c8a7014..3f69a3b 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -21,7 +21,6 @@ pub(crate) mod today; mod help; #[group] -#[prefix = ""] #[only_in(guild)] #[commands(roll, today)] struct General; @@ -52,7 +51,7 @@ pub fn register_commands(f: StandardFramework) -> StandardFramework { }, }; - let _ = _play(ctx, msg, &url); + let _ = _play(ctx, msg, url).await; }) }) } diff --git a/src/commands/playback.rs b/src/commands/playback.rs index 7ecef47..1c3ab95 100644 --- a/src/commands/playback.rs +++ b/src/commands/playback.rs @@ -110,7 +110,7 @@ pub async fn _play(ctx: &Context, msg: &Message, url: &str) -> CommandResult { #[command] pub async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { - if args.len() == 0 { + if args.is_empty() { return _resume(ctx, msg).await; } @@ -168,9 +168,11 @@ pub async fn skip(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { pub async fn die(ctx: &Context, msg: &Message, _: Args) -> CommandResult { let (_sb, call) = songbird(ctx, msg).await?; - let call = call.lock().await; + let mut call = call.lock().await; call.queue().stop(); + call.leave().await?; + Ok(()) } |
