aboutsummaryrefslogtreecommitdiff
path: root/src/commands/meme/create.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/meme/create.rs')
-rw-r--r--src/commands/meme/create.rs84
1 files changed, 41 insertions, 43 deletions
diff --git a/src/commands/meme/create.rs b/src/commands/meme/create.rs
index 97c5276..1c12f2a 100644
--- a/src/commands/meme/create.rs
+++ b/src/commands/meme/create.rs
@@ -1,50 +1,41 @@
-use std::{
- io::Read,
- process::{
- Command,
- Stdio,
- },
-};
+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 url::Url;
-
-use anyhow::anyhow;
-use lazy_static::lazy_static;
-use serenity::{
- all::ReactionType,
- framework::standard::{
- CommandError,
- CommandResult,
- },
- futures::TryFutureExt,
+use tap::Pipe;
+use tokio::{
+ io::AsyncReadExt,
+ process::Command,
};
+use url::Url;
use crate::{
- audio::{
- parse_times,
- ytdl_url,
- },
db::{
connection,
Audio,
Image,
NewMeme,
},
+ parse_times,
util,
FFMPEG_COMMAND,
};
@@ -77,12 +68,12 @@ pub async fn addmeme(ctx: &Context, msg: &Message, args: Args) -> CommandResult
.await;
}
- let image_id = image
- .map(|att| {
- let data = att.download()?;
- Image::create(&mut conn, &att.filename, data, msg.author.id.get())
- })
- .transpose()?;
+ 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())?);
+ };
let save_result = NewMeme {
title,
@@ -96,7 +87,9 @@ pub async fn addmeme(ctx: &Context, msg: &Message, args: Args) -> CommandResult
use diesel::result::DatabaseErrorKind;
match save_result {
- Ok(_) => msg.react(&ctx, "👌"),
+ Ok(_) => {
+ msg.react(&ctx, ReactionType::Unicode("👌".to_string())).await?;
+ },
Err(e) => {
if let Some(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) =
e.downcast_ref::<DieselError>()
@@ -111,6 +104,8 @@ pub async fn addmeme(ctx: &Context, msg: &Message, args: Args) -> CommandResult
return Err(e.into());
},
}
+
+ Ok(())
}
#[command]
@@ -131,7 +126,7 @@ pub async fn addaudiomeme(ctx: &Context, msg: &Message, args: Args) -> CommandRe
let opts = elems[1..].join(" ");
let (start, end) = parse_times(opts);
- let youtube_url = ytdl_url(audio_link.as_str())?;
+ let youtube_url = util::ytdl_url(audio_link.as_str()).await?;
let duration_opts = if let Some(e) = end {
vec![
@@ -178,18 +173,17 @@ pub async fn addaudiomeme(ctx: &Context, msg: &Message, args: Args) -> CommandRe
let mut conn = connection()?;
- let image = msg
- .attachments
- .first()
- .ok_or(anyhow!("no attachment"))
- .and_then(|att| {
- let data = att.download()?;
- Image::create(&mut conn, &att.filename, data, msg.author.id.get())
- })
- .ok();
+ let image_att = msg.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())?.pipe(Some);
+ }
let mut audio_data = Vec::new();
- let bytes = audio_reader.read_to_end(&mut audio_data)?;
+ let bytes = audio_reader.read_to_end(&mut audio_data).await?;
if bytes == 0 {
debug!("read 0 bytes from audio reader");
@@ -203,7 +197,7 @@ pub async fn addaudiomeme(ctx: &Context, msg: &Message, args: Args) -> CommandRe
let save_result = NewMeme {
title,
content: text,
- image_id: image,
+ image_id,
audio_id: Some(audio_id),
metadata_id: 0,
}
@@ -212,7 +206,9 @@ pub async fn addaudiomeme(ctx: &Context, msg: &Message, args: Args) -> CommandRe
use diesel::result::DatabaseErrorKind;
match save_result {
- Ok(_) => msg.react(&ctx, ReactionType::Unicode("👌".to_owned())),
+ Ok(_) => {
+ msg.react(&ctx, ReactionType::Unicode("👌".to_owned())).await?;
+ },
Err(e) => {
if let Some(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) =
e.downcast_ref::<DieselError>()
@@ -224,7 +220,9 @@ pub async fn addaudiomeme(ctx: &Context, msg: &Message, args: Args) -> CommandRe
.await;
}
- return Err(e);
+ return Err(e.into());
},
}
+
+ Ok(())
}