From 8b2ccf363ac6894c21e256844948c8327645f0db Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Sun, 17 Nov 2019 22:51:59 -0500 Subject: mostly fixed --- src/commands/meme/create.rs | 18 +++++++++++++++--- src/commands/meme/delete.rs | 12 +++++++++--- src/commands/meme/history.rs | 25 +++++++++++++++++++------ src/commands/meme/invoke.rs | 27 ++++++++++++++------------- src/commands/meme/mod.rs | 23 +++++++++++++++++++++++ 5 files changed, 80 insertions(+), 25 deletions(-) (limited to 'src/commands/meme') diff --git a/src/commands/meme/create.rs b/src/commands/meme/create.rs index 0851961..7f590b1 100644 --- a/src/commands/meme/create.rs +++ b/src/commands/meme/create.rs @@ -7,13 +7,25 @@ use std::{ }; use diesel::result::Error as DieselError; +use log::{ + debug, + error, + warn, +}; use serenity::{ - framework::standard::{Args, Delimiter}, + framework::standard::{ + Args, CommandResult, + Delimiter, + macros::command, + }, model::channel::Message, prelude::*, }; use url::Url; +use anyhow::anyhow; +use lazy_static::lazy_static; + use crate::{ audio::{ parse_times, @@ -34,7 +46,7 @@ lazy_static! { } #[command] -pub fn addmeme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { +pub fn addmeme(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { let mut args = Args::new(args.rest(), delims.as_ref()); let title = args.single_quoted::()?; @@ -81,7 +93,7 @@ pub fn addmeme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { } #[command] -pub fn addaudiomeme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { +pub fn addaudiomeme(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { let mut args = Args::new(args.rest(), delims.as_ref()); let title = args.single_quoted::()?; diff --git a/src/commands/meme/delete.rs b/src/commands/meme/delete.rs index 72226e5..148dfd6 100644 --- a/src/commands/meme/delete.rs +++ b/src/commands/meme/delete.rs @@ -2,8 +2,13 @@ use diesel::{ NotFound, result::Error as DieselError, }; +use log::info; use serenity::{ - framework::standard::Args, + framework::standard::{ + Args, + CommandResult, + macros::command, + }, model::channel::Message, prelude::*, }; @@ -19,7 +24,7 @@ use crate::{ #[command] #[aliases("delmem")] -pub fn delmeme(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { +pub fn delmeme(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { let title = args.single_quoted::()?; let conn = connection()?; @@ -34,7 +39,8 @@ pub fn delmeme(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { return Ok(()); } - Err(e) + Err(e)?; + Ok(()) } } } diff --git a/src/commands/meme/history.rs b/src/commands/meme/history.rs index 6c03bef..5921e1a 100644 --- a/src/commands/meme/history.rs +++ b/src/commands/meme/history.rs @@ -2,8 +2,17 @@ use diesel::{ NotFound, result::Error as DieselError, }; +use log::{ + debug, + error, + info, +}; use serenity::{ - framework::standard::Args, + framework::standard::{ + Args, + CommandResult, + macros::command, + }, model::channel::Message, prelude::*, }; @@ -12,8 +21,12 @@ use timeago::{ TimeUnit, }; +use anyhow::anyhow; +use lazy_static::lazy_static; + use crate::{ db::{ + self, connection, InvocationRecord, Meme, @@ -38,7 +51,7 @@ static CLEAN_DATE_FORMAT: &'static str = "%b %-e %Y"; #[command] #[aliases("what")] -pub fn wat(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { +pub fn wat(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult { let conn = connection()?; let record = match InvocationRecord::last(&conn) { @@ -80,7 +93,7 @@ pub fn wat(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { } #[command] -pub fn history(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { +pub fn history(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { use itertools::Itertools; lazy_static! { @@ -143,7 +156,7 @@ pub fn history(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { #[command] #[aliases("stat")] -pub fn stats(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { +pub fn stats(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult { use db; use serenity::model::{ id::UserId, @@ -206,7 +219,7 @@ and *{}* was the most-memed overall ({})"#, } #[command] -pub fn memers(ctx: &mut Context, msg: &Message, _args: Args) -> Result<()> { +pub fn memers(ctx: &mut Context, msg: &Message, _args: Args) -> CommandResult { use db; use itertools::Itertools; use serenity::model::{ @@ -240,7 +253,7 @@ pub fn memers(ctx: &mut Context, msg: &Message, _args: Args) -> Result<()> { } #[command] -pub fn query(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { +pub fn query(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { use std::borrow::Borrow; use itertools::Itertools; diff --git a/src/commands/meme/invoke.rs b/src/commands/meme/invoke.rs index 89ca999..93c5141 100644 --- a/src/commands/meme/invoke.rs +++ b/src/commands/meme/invoke.rs @@ -3,8 +3,13 @@ use diesel::{ result::Error as DieselError, }; use itertools::Itertools; +use log::info; use serenity::{ - framework::standard::Args, + framework::standard::{ + Args, + CommandResult, + macros::command, + }, model::channel::Message, prelude::*, }; @@ -23,22 +28,19 @@ use crate::{ #[command] #[aliases("mem")] -#[inline] -pub fn meme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { +pub fn meme(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { _meme(ctx, msg, args, AudioPlayback::Optional) } #[command] #[aliases("audiomeme", "audiomem")] -#[inline] -pub fn audio_meme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { +pub fn audio_meme(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { _meme(ctx, msg, args, AudioPlayback::Required) } #[command] #[aliases("silentmeme", "silentmem")] -#[inline] -pub fn silent_meme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { +pub fn silent_meme(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { _meme(ctx, msg, args, AudioPlayback::Prohibited) } @@ -49,7 +51,7 @@ enum AudioPlayback { Prohibited, } -fn _meme(ctx: &mut Context, msg: &Message, args: Args, audio_playback: AudioPlayback) -> Result<()> { +fn _meme(ctx: &mut Context, msg: &Message, args: Args, audio_playback: AudioPlayback) -> CommandResult { if args.len() == 0 || audio_playback != AudioPlayback::Optional { return rand_meme(ctx, msg, audio_playback); } @@ -77,9 +79,7 @@ fn _meme(ctx: &mut Context, msg: &Message, args: Args, audio_playback: AudioPlay send_meme(ctx, &mem, &conn, msg) } -#[command] -#[aliases("rarememe", "raremem")] -fn rand_meme(ctx: &Context, message: &Message, audio_playback: AudioPlayback) -> Result<()> { +fn rand_meme(ctx: &Context, message: &Message, audio_playback: AudioPlayback) -> CommandResult { let conn = connection()?; let should_audio = ctx.users_listening()?; @@ -93,7 +93,8 @@ fn rand_meme(ctx: &Context, message: &Message, audio_playback: AudioPlayback) -> match mem { Ok(mem) => { InvocationRecord::create(&conn, message.author.id.0, message.id.0, mem.id, true)?; - send_meme(ctx, &mem, &conn, message).map_err(Error::from) + send_meme(ctx, &mem, &conn, message)?; + Ok(()) }, Err(e) => { match e.downcast_ref::() { @@ -112,7 +113,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) -> Result<()> { +pub fn rare_meme(ctx: &mut Context, msg: &Message, _args: Args) -> CommandResult { let should_audio = ctx.users_listening()?; let conn = connection()?; diff --git a/src/commands/meme/mod.rs b/src/commands/meme/mod.rs index e5244aa..1761ab1 100644 --- a/src/commands/meme/mod.rs +++ b/src/commands/meme/mod.rs @@ -1,7 +1,9 @@ use diesel::PgConnection; +use log::debug; use rand::{Rng, thread_rng}; use serenity::{ builder::CreateMessage, + framework::standard::macros::group, http::AttachmentType, model::channel::Message, prelude::*, @@ -28,6 +30,27 @@ mod create; mod invoke; mod delete; +group!({ + name: "memes", + options: { + only_in: "guild", + }, + commands: [ + meme, + audio_meme, + silent_Meme, + addmeme, + addaudiomeme, + delmeme, + wat, + stats, + history, + rare_meme, + memers, + query, + ], +}); + fn send_meme(ctx: &Context, t: &Meme, conn: &PgConnection, msg: &Message) -> Result<()> { let should_tts = t.content.as_ref().map(|t| t.len() > 0).unwrap_or(false) && thread_rng().gen::() % 25 == 0; -- cgit v1.3.1