aboutsummaryrefslogtreecommitdiff
path: root/src/commands/meme/create.rs
diff options
context:
space:
mode:
authorNathan Perry <np@nathanperry.dev>2024-05-08 10:28:04 -0400
committerNathan Perry <np@nathanperry.dev>2024-05-08 14:16:01 -0400
commitfe467f60d99efa54f2ef64606e7d39b9b06d7294 (patch)
treea62bb50fedb1959d1a155878f0ff0ab7b1f699b6 /src/commands/meme/create.rs
parent48aa684dece2696e21fd871eb6f3825f28fe0200 (diff)
update all deps
Diffstat (limited to 'src/commands/meme/create.rs')
-rw-r--r--src/commands/meme/create.rs127
1 files changed, 83 insertions, 44 deletions
diff --git a/src/commands/meme/create.rs b/src/commands/meme/create.rs
index 06cc4ef..97c5276 100644
--- a/src/commands/meme/create.rs
+++ b/src/commands/meme/create.rs
@@ -14,9 +14,9 @@ use log::{
};
use serenity::{
framework::standard::{
+ macros::command,
Args,
Delimiter,
- macros::command,
},
model::channel::Message,
prelude::*,
@@ -25,20 +25,28 @@ use url::Url;
use anyhow::anyhow;
use lazy_static::lazy_static;
+use serenity::{
+ all::ReactionType,
+ framework::standard::{
+ CommandError,
+ CommandResult,
+ },
+ futures::TryFutureExt,
+};
use crate::{
- Result,
audio::{
parse_times,
ytdl_url,
},
db::{
- Audio,
connection,
+ Audio,
Image,
NewMeme,
},
- util::CtxExt, FFMPEG_COMMAND,
+ util,
+ FFMPEG_COMMAND,
};
lazy_static! {
@@ -46,13 +54,17 @@ lazy_static! {
}
#[command]
-pub fn addmeme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> {
+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();
- let text = if text.is_empty() { None } else { Some(text) };
+ let text = if text.is_empty() {
+ None
+ } else {
+ Some(text)
+ };
let mut conn = connection()?;
@@ -60,13 +72,17 @@ pub fn addmeme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> {
if image.is_none() && text.is_none() {
warn!("tried to create non-audio meme with no image or text");
- return ctx.send(msg.channel_id, "hahAA it's empty xdddd", msg.tts);
+ return util::send(ctx, msg.channel_id, "hahAA it's empty xdddd", msg.tts)
+ .map_err(CommandError::from)
+ .await;
}
- let image_id = image.map(|att| {
- let data = att.download()?;
- Image::create(&mut conn, &att.filename, data, msg.author.id.0)
- }).transpose()?;
+ let image_id = image
+ .map(|att| {
+ let data = att.download()?;
+ Image::create(&mut conn, &att.filename, data, msg.author.id.get())
+ })
+ .transpose()?;
let save_result = NewMeme {
title,
@@ -74,25 +90,31 @@ pub fn addmeme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> {
image_id,
audio_id: None,
metadata_id: 0,
- }.save(&mut conn, msg.author.id.0).map(|_| {});
+ }
+ .save(&mut conn, msg.author.id.get())
+ .map(|_| {});
use diesel::result::DatabaseErrorKind;
match save_result {
Ok(_) => msg.react(&ctx, "👌"),
Err(e) => {
- if let Some(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) = e.downcast_ref::<DieselError>() {
+ if let Some(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) =
+ e.downcast_ref::<DieselError>()
+ {
error!("tried to create meme that already exists");
- msg.react(&ctx, "❌")?;
- return ctx.send(msg.channel_id, "that meme already exists", msg.tts);
+ 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;
}
- return Err(e);
- }
+ return Err(e.into());
+ },
}
}
#[command]
-pub fn addaudiomeme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> {
+pub async fn addaudiomeme(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let mut args = Args::new(args.rest(), DELIMS.as_ref());
let title = args.single_quoted::<String>()?;
@@ -101,8 +123,8 @@ pub fn addaudiomeme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()>
let elems = audio_str.split_whitespace().collect::<Vec<_>>();
if elems.len() == 0 {
- ctx.send(msg.channel_id, "are you stupid", msg.tts)?;
- return Err(anyhow!("no audio link was provided"))
+ util::send(ctx, msg.channel_id, "are you stupid", msg.tts).await?;
+ return Err(anyhow!("no audio link was provided").into());
}
let audio_link = Url::parse(elems[0])?;
@@ -112,16 +134,24 @@ pub fn addaudiomeme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()>
let youtube_url = ytdl_url(audio_link.as_str())?;
let duration_opts = if let Some(e) = end {
- vec! [
- "-ss".to_owned(), start.map_or_else(
+ vec![
+ "-ss".to_owned(),
+ start.map_or_else(
|| "00:00:00".to_owned(),
- |s| format!("{:02}:{:02}:{:02}", s.num_hours(), s.num_minutes() % 60, s.num_seconds() % 60)
+ |s| {
+ format!(
+ "{:02}:{:02}:{:02}",
+ s.num_hours(),
+ s.num_minutes() % 60,
+ s.num_seconds() % 60
+ )
+ },
),
-
- "-to".to_owned(), format!("{:02}:{:02}:{:02}", e.num_hours(), e.num_minutes() % 60, e.num_seconds() % 60),
+ "-to".to_owned(),
+ format!("{:02}:{:02}:{:02}", e.num_hours(), e.num_minutes() % 60, e.num_seconds() % 60),
]
} else {
- vec! []
+ vec![]
};
let ffmpeg_command = Command::new(&*FFMPEG_COMMAND)
@@ -129,13 +159,8 @@ pub fn addaudiomeme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()>
.arg(youtube_url)
.args(duration_opts)
.args(&[
- "-ac", "2",
- "-ar", "48000",
- "-f", "opus",
- "-acodec", "libopus",
- "-b:a", "96k",
- "-fs", "5M",
- "-",
+ "-ac", "2", "-ar", "48000", "-f", "opus", "-acodec", "libopus", "-b:a", "96k", "-fs",
+ "5M", "-",
])
.stdout(Stdio::piped())
.stderr(Stdio::null())
@@ -145,15 +170,21 @@ pub fn addaudiomeme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()>
let mut audio_reader = ffmpeg_command.stdout.unwrap();
let text = args.rest().to_owned();
- let text = if text.is_empty() { None } else { Some(text) };
+ let text = if text.is_empty() {
+ None
+ } else {
+ Some(text)
+ };
let mut conn = connection()?;
- let image = msg.attachments.first()
+ 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.0)
+ Image::create(&mut conn, &att.filename, data, msg.author.id.get())
})
.ok();
@@ -162,10 +193,12 @@ pub fn addaudiomeme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()>
if bytes == 0 {
debug!("read 0 bytes from audio reader");
- return ctx.send(msg.channel_id, "🔇🔇🔇🔕🔕🔕🔕🔕🔇🔕🔕🔇🔕🔕📣📢📣📢📣", msg.tts);
+ return util::send(ctx, msg.channel_id, "🔇🔇🔇🔕🔕🔕🔕🔕🔇🔕🔕🔇🔕🔕📣📢📣📢📣", msg.tts)
+ .map_err(CommandError::from)
+ .await;
}
- let audio_id = Audio::create(&mut conn, audio_data, msg.author.id.0)?;
+ let audio_id = Audio::create(&mut conn, audio_data, msg.author.id.get())?;
let save_result = NewMeme {
title,
@@ -173,19 +206,25 @@ pub fn addaudiomeme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()>
image_id: image,
audio_id: Some(audio_id),
metadata_id: 0,
- }.save(&mut conn, msg.author.id.0).map(|_| {});
+ }
+ .save(&mut conn, msg.author.id.get())
+ .map(|_| {});
use diesel::result::DatabaseErrorKind;
match save_result {
- Ok(_) => msg.react(&ctx, "👌"),
+ Ok(_) => msg.react(&ctx, ReactionType::Unicode("👌".to_owned())),
Err(e) => {
- if let Some(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) = e.downcast_ref::<DieselError>() {
+ if let Some(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) =
+ e.downcast_ref::<DieselError>()
+ {
error!("tried to create meme that already exists");
- msg.react(&ctx, "❌")?;
- return ctx.send(msg.channel_id, "that meme already exists", msg.tts);
+ 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;
}
return Err(e);
- }
+ },
}
}