From c9e6b2e681088687f5a714c3324d4d0731b8393e Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Fri, 16 Aug 2024 02:53:45 -0400 Subject: better unrecognized handling, more expressive react/unreact indicators --- src/bot.rs | 59 ++++++++++++++++++++++++++++++++++----------- src/commands/meme/create.rs | 9 +++++-- src/commands/meme/invoke.rs | 7 +++--- src/commands/meme/mod.rs | 2 +- src/commands/mod.rs | 8 +----- src/commands/playback.rs | 10 ++++---- src/util/mod.rs | 21 +++++++++++++--- 7 files changed, 81 insertions(+), 35 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index dabb64c..836fb82 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -230,22 +230,53 @@ fn on_err(err: FrameworkError) -> BoxFuture<()> { __non_exhaustive: (), }; - match util::pop_string(msg_content) - .map_err(anyhow::Error::from) - .and_then(|(_rest, s)| s.parse().map_err(anyhow::Error::from)) - { - Ok(u) => { - if let Err(e) = commands::unrecognized(PoiseContext::Prefix(ctx), u).await { - error!("processing audio: {e}"); + let content = msg_content.trim(); + + if content.is_empty() { + if let Err(e) = util::reply(PoiseContext::Prefix(ctx), "what?").await { + error!("responding to empty message: {e}"); + }; + + return; + } + + lazy_static::lazy_static! { + static ref HTTP_REGEX: regex::Regex = regex::Regex::new(r#"^https?://"#).unwrap(); + } + + if HTTP_REGEX.is_match(content) { + match util::pop_string(msg_content) + .map_err(anyhow::Error::from) + .and_then(|(_rest, s)| s.parse().map_err(anyhow::Error::from)) + { + Ok(u) => { + if let Err(e) = + commands::link_unrecognized(PoiseContext::Prefix(ctx), u).await + { + error!("processing audio: {e}"); + "BANIC".to_string() + } else { + return; + } + }, + Err(e) => { + error!("processing unrecognized message: {e}"); "BANIC".to_string() - } else { - return; - } - }, - Err(e) => { - error!("processing unrecognized message: {e}"); + }, + } + } else { + if let Err(e) = commands::meme::invoke::_meme( + PoiseContext::Prefix(ctx), + msg_content, + Default::default(), + ) + .await + { + error!("producing meme for unrecognized: {e}"); "BANIC".to_string() - }, + } else { + return; + } } }, _ => "BANIC".to_string(), diff --git a/src/commands/meme/create.rs b/src/commands/meme/create.rs index c28f583..185e1f6 100644 --- a/src/commands/meme/create.rs +++ b/src/commands/meme/create.rs @@ -68,7 +68,7 @@ pub async fn addmeme( use diesel::result::DatabaseErrorKind; match save_result { Ok(_) => { - util::react(ctx, ReactionType::Unicode("👌".to_string())).await?; + util::react(ctx, '👌').await?; }, Err(e) => { if let Some(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) = @@ -76,7 +76,7 @@ pub async fn addmeme( { error!("tried to create meme that already exists"); - util::react(ctx, ReactionType::Unicode("❌".to_owned())).await?; + util::react(ctx, '❌').await?; util::reply(ctx, "that meme already exists").await?; return Ok(()); @@ -110,6 +110,8 @@ pub async fn addaudiomeme( let opts = elems[1..].join(" "); let (start, end) = parse_times(opts); + util::react(ctx, '🔃').await?; + let youtube_url = util::ytdl_url(audio_link.as_str()).await?; debug!("got download url: {youtube_url}"); @@ -169,6 +171,7 @@ pub async fn addaudiomeme( if bytes == 0 { debug!("read 0 bytes from audio reader"); + util::unreact(ctx, '🔃').await?; util::reply(ctx, "🔇🔇🔇🔕🔕🔕🔕🔕🔇🔕🔕🔇🔕🔕📣📢📣📢📣").await?; return Ok(()); } @@ -186,6 +189,8 @@ pub async fn addaudiomeme( .await .map(|_| {}); + util::unreact(ctx, '🔃').await?; + use diesel::result::DatabaseErrorKind; match save_result { Ok(_) => { diff --git a/src/commands/meme/invoke.rs b/src/commands/meme/invoke.rs index 31b0085..1d9040d 100644 --- a/src/commands/meme/invoke.rs +++ b/src/commands/meme/invoke.rs @@ -60,14 +60,15 @@ pub async fn silent_meme(ctx: PoiseContext<'_>) -> anyhow::Result<()> { _meme(ctx, "", AudioPlayback::Prohibited).await } -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -enum AudioPlayback { +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)] +pub(crate) enum AudioPlayback { Required, + #[default] Optional, Prohibited, } -async fn _meme( +pub(crate) async fn _meme( ctx: PoiseContext<'_>, args: impl AsRef, audio_playback: AudioPlayback, diff --git a/src/commands/meme/mod.rs b/src/commands/meme/mod.rs index 9495ec7..ca7714a 100644 --- a/src/commands/meme/mod.rs +++ b/src/commands/meme/mod.rs @@ -40,7 +40,7 @@ use crate::{ mod create; mod delete; mod history; -mod invoke; +pub(crate) mod invoke; pub fn commands() -> Vec> { vec![ diff --git a/src/commands/mod.rs b/src/commands/mod.rs index b7a8378..2729580 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -2,7 +2,6 @@ use poise::builtins::PrettyHelpConfiguration; use crate::{ commands::playback::_play, - util, PoiseContext, }; @@ -49,12 +48,7 @@ pub async fn help(ctx: PoiseContext<'_>, command: Option) -> anyhow::Res Ok(()) } -pub async fn unrecognized(ctx: PoiseContext<'_>, u: url::Url) -> anyhow::Result<()> { - if !u.scheme().starts_with("http") { - util::reply(ctx, "format your commands right. fuck you.").await?; - return Ok(()); - } - +pub async fn link_unrecognized(ctx: PoiseContext<'_>, u: url::Url) -> anyhow::Result<()> { let _ = _play(ctx, &u).await?; Ok(()) diff --git a/src/commands/playback.rs b/src/commands/playback.rs index 70dd704..9b74c02 100644 --- a/src/commands/playback.rs +++ b/src/commands/playback.rs @@ -5,10 +5,7 @@ use log::{ info, warn, }; -use serenity::{ - all::ReactionType, - prelude::*, -}; +use serenity::prelude::*; use songbird::{ input::YoutubeDl, Call, @@ -66,6 +63,8 @@ pub async fn _play(ctx: PoiseContext<'_>, url: &url::Url) -> anyhow::Result<()> return Ok(()); } + util::react(ctx, '🔃').await?; + let (_sb, call) = songbird(ctx).await?; let mut call = call.lock().await; @@ -82,7 +81,8 @@ pub async fn _play(ctx: PoiseContext<'_>, url: &url::Url) -> anyhow::Result<()> YoutubeDl::new_ytdl_like(&crate::config::YTDL_COMMAND, client.clone(), url.to_string()); call.enqueue_input(input.into()).await; - util::react(ctx, ReactionType::Unicode("📣".to_owned())).await?; + util::react(ctx, '📣').await?; + util::unreact(ctx, '🔃').await?; Ok(()) } diff --git a/src/util/mod.rs b/src/util/mod.rs index 5b943fe..fb4b552 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -131,9 +131,24 @@ pub async fn reply( } #[inline] -pub async fn react(ctx: PoiseContext<'_>, react: ReactionType) -> anyhow::Result { - let react = msg(ctx).ok_or_else(|| anyhow::anyhow!("elp"))?.react(ctx, react).await?; - Ok(react) +pub async fn react( + ctx: PoiseContext<'_>, + react: impl Into + Send, +) -> anyhow::Result { + msg(ctx) + .ok_or_else(|| anyhow::anyhow!("elp"))? + .react(ctx, react) + .await + .map_err(anyhow::Error::from) +} + +#[inline] +pub async fn unreact( + ctx: PoiseContext<'_>, + react: impl Into + Send, +) -> anyhow::Result<()> { + msg(ctx).ok_or_else(|| anyhow::anyhow!("elp"))?.delete_reaction(ctx, None, react).await?; + Ok(()) } pub async fn send_result( -- cgit v1.3.1