diff options
| author | Nathan Perry <np@nathanperry.dev> | 2019-11-17 22:51:59 -0500 |
|---|---|---|
| committer | Nathan Perry <np@nathanperry.dev> | 2019-11-17 22:51:59 -0500 |
| commit | 8b2ccf363ac6894c21e256844948c8327645f0db (patch) | |
| tree | f6742f2d7905613b8c0404bba46e237f984a8da2 /src/commands | |
| parent | 2a38c282dd57c2051a568549d62c80d6036e8920 (diff) | |
mostly fixed
Diffstat (limited to 'src/commands')
| -rw-r--r-- | src/commands/meme/create.rs | 18 | ||||
| -rw-r--r-- | src/commands/meme/delete.rs | 12 | ||||
| -rw-r--r-- | src/commands/meme/history.rs | 25 | ||||
| -rw-r--r-- | src/commands/meme/invoke.rs | 27 | ||||
| -rw-r--r-- | src/commands/meme/mod.rs | 23 | ||||
| -rw-r--r-- | src/commands/mod.rs | 65 | ||||
| -rw-r--r-- | src/commands/playback.rs | 46 | ||||
| -rw-r--r-- | src/commands/roll.rs | 15 | ||||
| -rw-r--r-- | src/commands/sound_levels.rs | 18 |
9 files changed, 155 insertions, 94 deletions
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::<String>()?; @@ -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::<String>()?; 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::<String>()?; 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::<DieselError>() { @@ -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::<u32>() % 25 == 0; diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 619335c..5c23418 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,5 +1,9 @@ +use log::info; use serenity::{ - framework::StandardFramework, + framework::{ + StandardFramework, + standard::macros::group, + }, }; use crate::{ @@ -19,42 +23,27 @@ pub(crate) mod playback; pub(crate) mod sound_levels; pub(crate) mod roll; -group!("playback", { - options: { - only_in: "guild", - }, - commands: [ - skip, - pause, - resume, - list, - die, - mute, - unmute, - play, - volume, - ], -}); -group!("general", { +group!({ + name: "general", options: { only_in: "guild", }, commands: [ - roll, + roll::roll, ], }); pub fn register_commands(f: StandardFramework) -> StandardFramework { let result = f - .group(&PLAYBACK_GROUP) + .group(&self::playback::PLAYBACK_GROUP) .group(&GENERAL_GROUP); #[cfg(feature = "diesel")] - let result = result.group(&MEMES_GROUP); + let result = result.group(&self::meme::MEMES_GROUP); #[cfg(feature = "games")] - let result = result.group(&GAME_GROUP); + let result = result.group(&crate::game::GAME_GROUP); result.unrecognised_command(|ctx, msg, unrec| { let url = match msg.content.split_whitespace().skip(1).next() { @@ -70,38 +59,6 @@ pub fn register_commands(f: StandardFramework) -> StandardFramework { }) } -#[cfg(feature = "games")] -group!("game", { - options: { - only_in: "guild", - }, - commands: [ - installedgame, - ownedgame, - updategaem, - ], -}); #[cfg(feature = "diesel")] mod meme; - -#[cfg(feature = "diesel")] -group!("memes", { - options: { - only_in: "guild", - }, - commands: [ - meme, - audio_meme, - silent_Meme, - addmeme, - addaudiomeme, - delmeme, - wat, - stats, - history, - rare_meme, - memers, - query, - ], -}); diff --git a/src/commands/playback.rs b/src/commands/playback.rs index 49ff44c..a54e937 100644 --- a/src/commands/playback.rs +++ b/src/commands/playback.rs @@ -1,6 +1,16 @@ use either::{Left, Right}; +use log::{ + debug, + error, + info, + warn, +}; use serenity::{ - framework::standard::Args, + framework::standard::{ + Args, + CommandResult, + macros::{command, group}, + }, model::channel::Message, prelude::*, }; @@ -17,7 +27,25 @@ use crate::{ util::CtxExt, }; -pub fn _play(ctx: &Context, msg: &Message, url: &str) -> Result<()> { +group!({ + name: "playback", + options: { + only_in: "guild", + }, + commands: [ + skip, + pause, + resume, + list, + die, + mute, + unmute, + play, + volume, + ], +}); + +pub fn _play(ctx: &Context, msg: &Message, url: &str) -> CommandResult { use url::{Url, Host}; debug!("playing '{}'", url); @@ -69,7 +97,7 @@ pub fn _play(ctx: &Context, msg: &Message, url: &str) -> Result<()> { } #[command] -pub fn play(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { +pub fn play(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { if args.len() == 0 { return _resume(ctx, msg); } @@ -86,7 +114,7 @@ pub fn play(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { } #[command] -pub fn pause(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { +pub fn pause(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult { let queue_lock = ctx.data.write().get::<PlayQueue>().cloned().unwrap(); let done = || ctx.send(msg.channel_id, "r u srs", msg.tts); @@ -119,11 +147,11 @@ pub fn pause(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { #[command] #[aliases("continue")] -pub fn resume(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { +pub fn resume(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult { _resume(ctx, msg) } -fn _resume(ctx: &mut Context, msg: &Message) -> Result<()> { +fn _resume(ctx: &mut Context, msg: &Message) -> CommandResult { let queue_lock = ctx.data.write().get::<PlayQueue>().cloned().unwrap(); let done = || ctx.send(msg.channel_id, "r u srs", msg.tts); @@ -160,7 +188,7 @@ fn _resume(ctx: &mut Context, msg: &Message) -> Result<()> { #[command] #[aliases("next")] -pub fn skip(ctx: &mut Context, _msg: &Message, _args: Args) -> Result<()> { +pub fn skip(ctx: &mut Context, _msg: &Message, _args: Args) -> CommandResult { let data = ctx.data.write(); let mgr_lock = data.get::<VoiceManager>().cloned().unwrap(); @@ -182,7 +210,7 @@ pub fn skip(ctx: &mut Context, _msg: &Message, _args: Args) -> Result<()> { #[command] #[aliases("sudoku", "fuckoff", "stop")] -pub fn die(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { +pub fn die(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult { let data = ctx.data.write(); let mgr_lock = data.get::<VoiceManager>().cloned().unwrap(); @@ -212,7 +240,7 @@ pub fn die(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { #[command] #[aliases("queue")] -pub fn list(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { +pub fn list(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult { let queue_lock = ctx.data.write().get::<PlayQueue>().cloned().unwrap(); let play_queue = queue_lock.read().unwrap(); diff --git a/src/commands/roll.rs b/src/commands/roll.rs index 3df91d0..1544654 100644 --- a/src/commands/roll.rs +++ b/src/commands/roll.rs @@ -1,12 +1,23 @@ use std::result::Result as StdResult; +use log::{ + debug, + error, +}; use rand::prelude::*; use serenity::{ - framework::standard::Args, + framework::standard::{ + Args, + CommandResult, + macros::command, + }, model::channel::Message, prelude::*, }; use statrs; +use thiserror::Error; + +use lazy_static::lazy_static; use crate::{ Result, @@ -198,7 +209,7 @@ mod test { #[command] #[aliases("calc", "calculate")] -pub fn roll(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { +pub fn roll(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { match Calc::eval(args.rest()) { Ok(result) => { debug!("got calc result '{}'", result); diff --git a/src/commands/sound_levels.rs b/src/commands/sound_levels.rs index 81508a6..8add787 100644 --- a/src/commands/sound_levels.rs +++ b/src/commands/sound_levels.rs @@ -1,5 +1,15 @@ +use log::{ + error, + info, + trace, + warn, +}; use serenity::{ - framework::standard::Args, + framework::standard::{ + Args, + CommandResult, + macros::command, + }, model::channel::Message, prelude::*, }; @@ -14,7 +24,7 @@ use crate::{ pub const DEFAULT_VOLUME: f32 = 0.10; #[command] -pub fn mute(ctx: &mut Context, _: &Message, _: Args) -> Result<()> { +pub fn mute(ctx: &mut Context, _: &Message, _: Args) -> CommandResult { let mgr_lock = ctx.data.write().get::<VoiceManager>().cloned().unwrap(); let mut manager = mgr_lock.lock(); @@ -32,7 +42,7 @@ pub fn mute(ctx: &mut Context, _: &Message, _: Args) -> Result<()> { } #[command] -pub fn unmute(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { +pub fn unmute(ctx: &mut Context, msg: &Message, _: Args) -> CommandResult { let mgr_lock = ctx.data.write().get::<VoiceManager>().cloned().unwrap(); let mut manager = mgr_lock.lock(); @@ -51,7 +61,7 @@ pub fn unmute(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> { } #[command] -pub fn volume(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> { +pub fn volume(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { if args.len() == 0 { let vol = { let queue_lock = ctx.data.write().get::<PlayQueue>().cloned().unwrap(); |
