diff options
Diffstat (limited to 'src/commands/meme/create.rs')
| -rw-r--r-- | src/commands/meme/create.rs | 104 |
1 files changed, 45 insertions, 59 deletions
diff --git a/src/commands/meme/create.rs b/src/commands/meme/create.rs index 9aff370..cad9bfc 100644 --- a/src/commands/meme/create.rs +++ b/src/commands/meme/create.rs @@ -2,25 +2,12 @@ use std::process::Stdio; use anyhow::anyhow; use diesel::result::Error as DieselError; -use lazy_static::lazy_static; use log::{ debug, error, warn, }; -use serenity::{ - all::ReactionType, - framework::standard::{ - macros::command, - Args, - CommandError, - CommandResult, - Delimiter, - }, - futures::TryFutureExt, - model::channel::Message, - prelude::*, -}; +use serenity::all::ReactionType; use tap::Pipe; use tokio::{ io::AsyncReadExt, @@ -37,20 +24,16 @@ use crate::{ }, parse_times, util, + PoiseContext, FFMPEG_COMMAND, }; -lazy_static! { - static ref DELIMS: Vec<Delimiter> = vec![' '.into(), '\n'.into(), '\t'.into()]; -} - -#[command] -pub async fn addmeme(ctx: &Context, msg: &Message, args: Args) -> CommandResult { - let mut args = Args::new(args.rest(), DELIMS.as_ref()); - - let title = args.single_quoted::<String>()?; - let text = args.rest().to_owned(); - +#[poise::command(slash_command, prefix_command, guild_only, category = "memes")] +pub async fn addmeme( + ctx: PoiseContext<'_>, + title: String, + #[rest] text: String, +) -> anyhow::Result<()> { let text = if text.is_empty() { None } else { @@ -59,20 +42,21 @@ pub async fn addmeme(ctx: &Context, msg: &Message, args: Args) -> CommandResult let mut conn = connection().await?; - let image = msg.attachments.first(); + let image = util::msg(ctx).and_then(|msg| msg.attachments.first()); if image.is_none() && text.is_none() { warn!("tried to create non-audio meme with no image or text"); - return util::send(ctx, msg.channel_id, "hahAA it's empty xdddd", msg.tts) - .map_err(CommandError::from) - .await; + + util::reply(ctx, "hahAA it's empty xdddd").await?; + return Ok(()); } let mut image_id = None; if let Some(att) = image { let data = att.download().await?; - image_id = Some(Image::create(&mut conn, &att.filename, data, msg.author.id.get()).await?); + image_id = + Some(Image::create(&mut conn, &att.filename, data, ctx.author().id.get()).await?); }; let save_result = NewMeme { @@ -82,24 +66,25 @@ pub async fn addmeme(ctx: &Context, msg: &Message, args: Args) -> CommandResult audio_id: None, metadata_id: 0, } - .save(&mut conn, msg.author.id.get()) + .save(&mut conn, ctx.author().id.get()) .await .map(|_| {}); use diesel::result::DatabaseErrorKind; match save_result { Ok(_) => { - msg.react(&ctx, ReactionType::Unicode("👌".to_string())).await?; + util::react(ctx, ReactionType::Unicode("👌".to_string())).await?; }, Err(e) => { if let Some(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) = e.downcast_ref::<DieselError>() { error!("tried to create meme that already exists"); - msg.react(&ctx, ReactionType::Unicode("❌".to_owned())).await?; - return util::send(ctx, msg.channel_id, "that meme already exists", msg.tts) - .map_err(CommandError::from) - .await; + + util::react(ctx, ReactionType::Unicode("❌".to_owned())).await?; + util::reply(ctx, "that meme already exists").await?; + + return Ok(()); } return Err(e.into()); @@ -109,19 +94,19 @@ pub async fn addmeme(ctx: &Context, msg: &Message, args: Args) -> CommandResult Ok(()) } -#[command] -pub async fn addaudiomeme(ctx: &Context, msg: &Message, args: Args) -> CommandResult { +#[poise::command(slash_command, prefix_command, guild_only, category = "memes")] +pub async fn addaudiomeme( + ctx: PoiseContext<'_>, + title: String, + audio_str: String, + #[rest] rest: String, +) -> anyhow::Result<()> { debug!("running addaudiomeme"); - let mut args = Args::new(args.rest(), DELIMS.as_ref()); - - let title = args.single_quoted::<String>()?; - let audio_str = args.single_quoted::<String>()?; - let elems = audio_str.split_whitespace().collect::<Vec<_>>(); if elems.is_empty() { - util::send(ctx, msg.channel_id, "are you stupid", msg.tts).await?; + util::reply(ctx, "are you stupid").await?; return Err(anyhow!("no audio link was provided").into()); } @@ -168,23 +153,23 @@ pub async fn addaudiomeme(ctx: &Context, msg: &Message, args: Args) -> CommandRe let mut audio_reader = ffmpeg_command.stdout.unwrap(); - let text = args.rest().to_owned(); - let text = if text.is_empty() { + let text = if rest.is_empty() { None } else { - Some(text) + Some(rest) }; let mut conn = connection().await?; - let image_att = msg.attachments.first().ok_or(anyhow!("no attachment")); + let image_att = + util::msg(ctx).and_then(|x| x.attachments.first()).ok_or(anyhow!("no attachment")); let mut image_id = None; if let Ok(att) = image_att { let data = att.download().await?; image_id = - Image::create(&mut conn, &att.filename, data, msg.author.id.get()).await?.pipe(Some); + Image::create(&mut conn, &att.filename, data, ctx.author().id.get()).await?.pipe(Some); } let mut audio_data = Vec::new(); @@ -193,12 +178,12 @@ pub async fn addaudiomeme(ctx: &Context, msg: &Message, args: Args) -> CommandRe if bytes == 0 { debug!("read 0 bytes from audio reader"); - return util::send(ctx, msg.channel_id, "🔇🔇🔇🔕🔕🔕🔕🔕🔇🔕🔕🔇🔕🔕📣📢📣📢📣", msg.tts) - .map_err(CommandError::from) - .await; + + util::reply(ctx, "🔇🔇🔇🔕🔕🔕🔕🔕🔇🔕🔕🔇🔕🔕📣📢📣📢📣").await?; + return Ok(()); } - let audio_id = Audio::create(&mut conn, audio_data, msg.author.id.get()).await?; + let audio_id = Audio::create(&mut conn, audio_data, ctx.author().id.get()).await?; let save_result = NewMeme { title, @@ -207,24 +192,25 @@ pub async fn addaudiomeme(ctx: &Context, msg: &Message, args: Args) -> CommandRe audio_id: Some(audio_id), metadata_id: 0, } - .save(&mut conn, msg.author.id.get()) + .save(&mut conn, ctx.author().id.get()) .await .map(|_| {}); use diesel::result::DatabaseErrorKind; match save_result { Ok(_) => { - msg.react(&ctx, ReactionType::Unicode("👌".to_owned())).await?; + util::react(ctx, ReactionType::Unicode("👌".to_owned())).await?; }, Err(e) => { if let Some(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) = e.downcast_ref::<DieselError>() { error!("tried to create meme that already exists"); - msg.react(&ctx, ReactionType::Unicode("❌".to_owned())).await?; - return util::send(ctx, msg.channel_id, "that meme already exists", msg.tts) - .map_err(CommandError::from) - .await; + + util::react(ctx, ReactionType::Unicode("❌".to_owned())).await?; + util::reply(ctx, "that meme already exists").await?; + + return Ok(()); } return Err(e.into()); |
