diff options
Diffstat (limited to 'src/commands/meme')
| -rw-r--r-- | src/commands/meme/create.rs | 21 | ||||
| -rw-r--r-- | src/commands/meme/delete.rs | 6 | ||||
| -rw-r--r-- | src/commands/meme/history.rs | 39 | ||||
| -rw-r--r-- | src/commands/meme/invoke.rs | 14 | ||||
| -rw-r--r-- | src/commands/meme/mod.rs | 29 |
5 files changed, 57 insertions, 52 deletions
diff --git a/src/commands/meme/create.rs b/src/commands/meme/create.rs index 0da2031..047c556 100644 --- a/src/commands/meme/create.rs +++ b/src/commands/meme/create.rs @@ -14,7 +14,7 @@ use log::{ }; use serenity::{ framework::standard::{ - Args, CommandResult, + Args, Delimiter, macros::command, }, @@ -27,6 +27,7 @@ use anyhow::anyhow; use lazy_static::lazy_static; use crate::{ + Result, audio::{ parse_times, ytdl_url, @@ -41,12 +42,12 @@ use crate::{ }; lazy_static! { - static ref delims: Vec<Delimiter> = vec![' '.into(), '\n'.into(), '\t'.into()]; + static ref DELIMS: Vec<Delimiter> = vec![' '.into(), '\n'.into(), '\t'.into()]; } #[command] -pub fn addmeme(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { - let mut args = Args::new(args.rest(), delims.as_ref()); +pub fn addmeme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { + let mut args = Args::new(args.rest(), DELIMS.as_ref()); let title = args.single_quoted::<String>()?; let text = args.rest().to_owned(); @@ -78,11 +79,11 @@ pub fn addmeme(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { use diesel::result::DatabaseErrorKind; match save_result { - Ok(_) => msg.react(ctx, "👌"), + Ok(_) => msg.react(&ctx, "👌"), Err(e) => { if let Some(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) = e.downcast_ref::<DieselError>() { error!("tried to create meme that already exists"); - msg.react(ctx, "❌")?; + msg.react(&ctx, "❌")?; return ctx.send(msg.channel_id, "that meme already exists", msg.tts); } @@ -92,8 +93,8 @@ pub fn addmeme(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { } #[command] -pub fn addaudiomeme(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { - let mut args = Args::new(args.rest(), delims.as_ref()); +pub fn addaudiomeme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { + let mut args = Args::new(args.rest(), DELIMS.as_ref()); let title = args.single_quoted::<String>()?; let audio_str = args.single_quoted::<String>()?; @@ -177,11 +178,11 @@ pub fn addaudiomeme(ctx: &mut Context, msg: &Message, args: Args) -> CommandResu use diesel::result::DatabaseErrorKind; match save_result { - Ok(_) => msg.react(ctx, "👌"), + Ok(_) => msg.react(&ctx, "👌"), Err(e) => { if let Some(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) = e.downcast_ref::<DieselError>() { error!("tried to create meme that already exists"); - msg.react(ctx, "❌")?; + msg.react(&ctx, "❌")?; return ctx.send(msg.channel_id, "that meme already exists", msg.tts); } diff --git a/src/commands/meme/delete.rs b/src/commands/meme/delete.rs index cc507d1..e5e4333 100644 --- a/src/commands/meme/delete.rs +++ b/src/commands/meme/delete.rs @@ -6,7 +6,6 @@ use log::info; use serenity::{ framework::standard::{ Args, - CommandResult, macros::command, }, model::channel::Message, @@ -14,6 +13,7 @@ use serenity::{ }; use crate::{ + Result, db::{ connection, delete_meme, @@ -23,7 +23,7 @@ use crate::{ #[command] #[aliases("delmem")] -pub fn delmeme(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { +pub fn delmeme(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { let title = args.single_quoted::<String>()?; let conn = connection()?; @@ -32,7 +32,7 @@ pub fn delmeme(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResul Ok(_) => msg.react(ctx, "💀"), Err(e) => { if let Some(NotFound) = e.downcast_ref::<DieselError>() { - msg.react(ctx, "❓")?; + msg.react(&ctx, "❓")?; info!("attempted to delete nonexistent meme: '{}'", title); ctx.send(msg.channel_id, "nice try", msg.tts)?; return Ok(()); diff --git a/src/commands/meme/history.rs b/src/commands/meme/history.rs index 5921e1a..7579524 100644 --- a/src/commands/meme/history.rs +++ b/src/commands/meme/history.rs @@ -10,7 +10,6 @@ use log::{ use serenity::{ framework::standard::{ Args, - CommandResult, macros::command, }, model::channel::Message, @@ -51,7 +50,7 @@ static CLEAN_DATE_FORMAT: &'static str = "%b %-e %Y"; #[command] #[aliases("what")] -pub fn wat(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult { +pub fn wat(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { let conn = connection()?; let record = match InvocationRecord::last(&conn) { @@ -72,7 +71,7 @@ pub fn wat(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult { match meme { Ok(ref meme) => { let metadata = Metadata::find(&conn, meme.metadata_id)?; - let author = crate::TARGET_GUILD_ID.member(ctx, metadata.created_by as u64)?; + let author = crate::TARGET_GUILD_ID.member(&ctx, metadata.created_by as u64)?; ctx.send(msg.channel_id, &format!("that was \"{}\" by {} ({})", @@ -93,7 +92,7 @@ pub fn wat(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult { } #[command] -pub fn history(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { +pub fn history(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { use itertools::Itertools; lazy_static! { @@ -134,8 +133,8 @@ pub fn history(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResul Metadata::find(&conn, meme.metadata_id).map(|metadata| (metadata, meme)) }) .map(|(metadata, meme)| { - let author_name = crate::TARGET_GUILD_ID.member(ctx, metadata.created_by as u64).map(|m| m.display_name().into_owned()).unwrap_or("???".to_owned()); - let invoker_name = crate::TARGET_GUILD_ID.member(ctx, rec.user_id as u64).map(|m| m.display_name().into_owned()).unwrap_or("???".to_owned()); + let author_name = crate::TARGET_GUILD_ID.member(&ctx, metadata.created_by as u64).map(|m| m.display_name().into_owned()).unwrap_or("???".to_owned()); + let invoker_name = crate::TARGET_GUILD_ID.member(&ctx, rec.user_id as u64).map(|m| m.display_name().into_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) }) .unwrap_or_else(|e| { @@ -145,7 +144,7 @@ pub fn history(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResul } } - let invoker_name = crate::TARGET_GUILD_ID.member(ctx, rec.user_id as u64).map(|m| m.display_name().into_owned()).unwrap_or("???".to_owned()); + let invoker_name = crate::TARGET_GUILD_ID.member(&ctx, rec.user_id as u64).map(|m| m.display_name().into_owned()).unwrap_or("???".to_owned()); format!("{}. [{}{}] not found. invoked by {}.", i + 1, rand, ago, invoker_name) }) }) @@ -156,7 +155,7 @@ pub fn history(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResul #[command] #[aliases("stat")] -pub fn stats(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult { +pub fn stats(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { use db; use serenity::model::{ id::UserId, @@ -169,11 +168,11 @@ pub fn stats(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult { debug!("reporting stats"); - let rand_user: User = UserId(stats.most_random_meme_user).to_user(ctx)?; - let direct_user: User = UserId(stats.most_directly_named_meme_user).to_user(ctx)?; + let rand_user: User = UserId(stats.most_random_meme_user).to_user(&ctx)?; + let direct_user: User = UserId(stats.most_directly_named_meme_user).to_user(&ctx)?; - let rand_user = rand_user.nick_in(ctx, *TARGET_GUILD_ID).unwrap_or(rand_user.name); - let direct_user = direct_user.nick_in(ctx, *TARGET_GUILD_ID).unwrap_or(direct_user.name); + let rand_user = rand_user.nick_in(&ctx, *TARGET_GUILD_ID).unwrap_or(rand_user.name); + let direct_user = direct_user.nick_in(&ctx, *TARGET_GUILD_ID).unwrap_or(direct_user.name); let s = format!( r#" @@ -219,7 +218,7 @@ and *{}* was the most-memed overall ({})"#, } #[command] -pub fn memers(ctx: &mut Context, msg: &Message, _args: Args) -> CommandResult { +pub fn memers(ctx: &mut Context, msg: &Message, _args: Args) -> Result<()> { use db; use itertools::Itertools; use serenity::model::{ @@ -230,8 +229,8 @@ pub fn memers(ctx: &mut Context, msg: &Message, _args: Args) -> CommandResult { let s = db::memers()? .into_iter() .map(|info| { - let user = UserId(info.user_id).to_user(ctx)?; - let username = user.nick_in(ctx, *TARGET_GUILD_ID).unwrap_or(user.name); + let user = UserId(info.user_id).to_user(&ctx)?; + let username = user.nick_in(&ctx, *TARGET_GUILD_ID).unwrap_or(user.name); let res = format!( "**{}**: {} total, {} random, {} specific. favorite meme: *{}* ({})", @@ -253,7 +252,7 @@ pub fn memers(ctx: &mut Context, msg: &Message, _args: Args) -> CommandResult { } #[command] -pub fn query(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { +pub fn query(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { use std::borrow::Borrow; use itertools::Itertools; @@ -271,12 +270,12 @@ pub fn query(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult static ref AGE_REGEX: Regex = Regex::new(r"(?i)(?:age|order)=(.*)").unwrap(); } - let guild = msg.channel_id.to_channel(ctx)? + let guild = msg.channel_id.to_channel(&ctx)? .guild() .ok_or(anyhow!("couldn't find guild"))?; let guild = guild.read() - .guild(ctx) + .guild(&ctx) .ok_or(anyhow!("couldn't find guild"))?; let guild = guild @@ -310,8 +309,8 @@ pub fn query(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult let result = db::query_meme(args.rest(), creator, order)? .into_iter() .map(|(meme, metadata)| { - let user = UserId(metadata.created_by as u64).to_user(ctx)?; - let username = user.nick_in(ctx, *TARGET_GUILD_ID).unwrap_or(user.name); + let user = UserId(metadata.created_by as u64).to_user(&ctx)?; + let username = user.nick_in(&ctx, *TARGET_GUILD_ID).unwrap_or(user.name); Ok(format!("*{}* by **{}** ({}). text length: **{}**, image: **{}**, audio: **{}**", meme.title, diff --git a/src/commands/meme/invoke.rs b/src/commands/meme/invoke.rs index fb32ae2..d0b7a19 100644 --- a/src/commands/meme/invoke.rs +++ b/src/commands/meme/invoke.rs @@ -7,7 +7,6 @@ use log::info; use serenity::{ framework::standard::{ Args, - CommandResult, macros::command, }, model::channel::Message, @@ -16,6 +15,7 @@ use serenity::{ use crate::{ commands::meme::send_meme, + Result, db::{ self, connection, @@ -27,19 +27,19 @@ use crate::{ #[command] #[aliases("mem")] -pub fn meme(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { +pub fn meme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { _meme(ctx, msg, args, AudioPlayback::Optional) } #[command] #[aliases("audiomeme", "audiomem")] -pub fn audio_meme(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { +pub fn audio_meme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { _meme(ctx, msg, args, AudioPlayback::Required) } #[command] #[aliases("silentmeme", "silentmem")] -pub fn silent_meme(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { +pub fn silent_meme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { _meme(ctx, msg, args, AudioPlayback::Prohibited) } @@ -50,7 +50,7 @@ enum AudioPlayback { Prohibited, } -fn _meme(ctx: &mut Context, msg: &Message, args: Args, audio_playback: AudioPlayback) -> CommandResult { +fn _meme(ctx: &mut Context, msg: &Message, args: Args, audio_playback: AudioPlayback) -> Result<()> { if args.len() == 0 || audio_playback != AudioPlayback::Optional { return rand_meme(ctx, msg, audio_playback); } @@ -78,7 +78,7 @@ fn _meme(ctx: &mut Context, msg: &Message, args: Args, audio_playback: AudioPlay send_meme(ctx, &mem, &conn, msg) } -fn rand_meme(ctx: &Context, message: &Message, audio_playback: AudioPlayback) -> CommandResult { +fn rand_meme(ctx: &Context, message: &Message, audio_playback: AudioPlayback) -> Result<()> { let conn = connection()?; let should_audio = ctx.users_listening()?; @@ -112,7 +112,7 @@ fn rand_meme(ctx: &Context, message: &Message, audio_playback: AudioPlayback) -> #[command] #[aliases("rarememe", "raremem")] -pub fn rare_meme(ctx: &mut Context, msg: &Message, _args: Args) -> CommandResult { +pub fn rare_meme(ctx: &mut Context, msg: &Message, _args: Args) -> Result<()> { let should_audio = ctx.users_listening()?; let conn = connection()?; diff --git a/src/commands/meme/mod.rs b/src/commands/meme/mod.rs index 1761ab1..93de288 100644 --- a/src/commands/meme/mod.rs +++ b/src/commands/meme/mod.rs @@ -2,7 +2,6 @@ use diesel::PgConnection; use log::debug; use rand::{Rng, thread_rng}; use serenity::{ - builder::CreateMessage, framework::standard::macros::group, http::AttachmentType, model::channel::Message, @@ -60,24 +59,30 @@ fn send_meme(ctx: &Context, t: &Meme, conn: &PgConnection, msg: &Message) -> Res let image = t.image(conn); let audio = t.audio(conn); - let create_msg = |m: &mut CreateMessage| { - let ret = m.tts(should_tts); - - match t.content { - Some(ref text) if text.len() > 0 => ret.content(text), - _ => ret, - } - }; - match image { Some(image) => { let image = image?; - msg.channel_id.send_files(ctx, vec!(AttachmentType::Bytes((&image.data, &image.filename))), create_msg)?; + msg.channel_id.send_files(ctx, vec!(AttachmentType::Bytes((&image.data, &image.filename))), |m| { + let ret = m.tts(should_tts); + + match t.content { + Some(ref text) if text.len() > 0 => ret.content(text), + _ => ret, + } + })?; }, None => match t.content { - Some(_) => { msg.channel_id.send_message(ctx, create_msg)?; }, + Some(_) => { msg.channel_id.send_message(ctx, |m| { + let ret = m.tts(should_tts); + + match t.content { + Some(ref text) if text.len() > 0 => ret.content(text), + _ => ret, + } + })?; }, None => {}, + }, }; |
