aboutsummaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
authorNathan Perry <avaglir@gmail.com>2019-03-04 12:52:03 -0500
committerNathan Perry <avaglir@gmail.com>2019-03-04 12:52:03 -0500
commitfc1fcbad504617053d6e8cb63b5eba2f051ec665 (patch)
tree79bbb57463dd0e187d17416a88cab68500a58f15 /src/commands
parent47be7de23836aa97a9283c73bdf90d91c5f45485 (diff)
improve logging and user feedback for commands
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/meme.rs73
-rw-r--r--src/commands/playback.rs19
-rw-r--r--src/commands/roll.rs2
-rw-r--r--src/commands/sound_levels.rs17
4 files changed, 91 insertions, 20 deletions
diff --git a/src/commands/meme.rs b/src/commands/meme.rs
index 9b77bd2..784c07e 100644
--- a/src/commands/meme.rs
+++ b/src/commands/meme.rs
@@ -79,6 +79,7 @@ fn _meme(ctx: &mut Context, msg: &Message, args: Args, audio_only: bool) -> Resu
},
Err(e) => {
return if let Some(NotFound) = e.downcast_ref::<DieselError>() {
+ info!("requested meme not found in database");
send(msg.channel_id, "c'mon baby, guesstimate", msg.tts)
} else {
send(msg.channel_id, "what in ryan's name", msg.tts)?;
@@ -97,6 +98,7 @@ pub fn wat(_: &mut Context, msg: &Message, _: Args) -> Result<()> {
Ok(x) => x,
Err(e) => {
if let Some(NotFound) = e.downcast_ref::<DieselError>() {
+ info!("found no memes in history");
return send(msg.channel_id, "no one has ever memed before", msg.tts);
}
@@ -118,6 +120,7 @@ pub fn wat(_: &mut Context, msg: &Message, _: Args) -> Result<()> {
},
Err(e) => {
if let Some(NotFound) = e.downcast_ref::<DieselError>() {
+ info!("last meme not found in database");
return send(msg.channel_id, "heuueueeeeh?", msg.tts);
}
@@ -140,6 +143,7 @@ pub fn history(_: &mut Context, msg: &Message, mut args: Args) -> Result<()> {
let n = args.single_quoted::<usize>().unwrap_or(DEFAULT_HIST);
if n > MAX_HIST {
+ debug!("user requested more than MAX_HIST ({}) items from history", MAX_HIST);
send(msg.channel_id, "YER PUSHIN ME OVER THE FUCKIN LINE", true)?;
}
@@ -148,9 +152,11 @@ pub fn history(_: &mut Context, msg: &Message, mut args: Args) -> Result<()> {
let records = InvocationRecord::last_n(&conn, n)?;
if records.len() == 0 {
+ info!("no memes in history");
return send(msg.channel_id, "i don't remember anything :(", msg.tts);
}
+ info!("reporting meme history (len {})", n);
let resp = records
.into_iter()
.enumerate()
@@ -206,18 +212,31 @@ pub fn addmeme(_: &mut Context, msg: &Message, args: Args) -> Result<()> {
.ok();
if image.is_none() && text.is_none() {
+ warn!("tried to create non-audio meme with no image or text");
return send(msg.channel_id, "hahAA it's empty xdddd", msg.tts);
}
- NewMeme {
+ let save_result = NewMeme {
title,
content: text,
image_id: image,
audio_id: None,
metadata_id: 0,
- }.save(&conn, msg.author.id.0).map(|_| {})?;
+ }.save(&conn, msg.author.id.0).map(|_| {});
- msg.react("👌")
+ use diesel::result::DatabaseErrorKind;
+ match save_result {
+ Ok(_) => msg.react("👌"),
+ Err(e) => {
+ if let Some(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) = e.downcast_ref::<DieselError>() {
+ error!("tried to create meme that already exists");
+ msg.react("❌")?;
+ return send(msg.channel_id, "that meme already exists", msg.tts);
+ }
+
+ return Err(e);
+ }
+ }
}
pub fn addaudiomeme(_: &mut Context, msg: &Message, args: Args) -> Result<()> {
@@ -295,24 +314,46 @@ pub fn addaudiomeme(_: &mut Context, msg: &Message, args: Args) -> Result<()> {
let audio_id = Audio::create(&conn, audio_data, msg.author.id.0)?;
- NewMeme {
+ let save_result = NewMeme {
title,
content: text,
image_id: image,
audio_id: Some(audio_id),
metadata_id: 0,
- }.save(&conn, msg.author.id.0).map(|_| {})?;
+ }.save(&conn, msg.author.id.0).map(|_| {});
- msg.react("👌")
+ use diesel::result::DatabaseErrorKind;
+ match save_result {
+ Ok(_) => msg.react("👌"),
+ Err(e) => {
+ if let Some(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) = e.downcast_ref::<DieselError>() {
+ error!("tried to create meme that already exists");
+ msg.react("❌")?;
+ return send(msg.channel_id, "that meme already exists", msg.tts);
+ }
+
+ return Err(e);
+ }
+ }
}
pub fn delmeme(_: &mut Context, msg: &Message, mut args: Args) -> Result<()> {
let title = args.single_quoted::<String>()?;
let conn = connection()?;
- delete_meme(&conn, &title, msg.author.id.0)?;
+ match delete_meme(&conn, &title, msg.author.id.0) {
+ Ok(_) => msg.react("💀"),
+ Err(e) => {
+ if let Some(NotFound) = e.downcast_ref::<DieselError>() {
+ msg.react("❓")?;
+ info!("attempted to delete nonexistent meme: '{}'", title);
+ send(msg.channel_id, "nice try", msg.tts)?;
+ return Ok(());
+ }
- msg.react("💀")
+ Err(e)
+ }
+ }
}
pub fn stats(_: &mut Context, msg: &Message, _: Args) -> Result<()> {
@@ -322,6 +363,8 @@ pub fn stats(_: &mut Context, msg: &Message, _: Args) -> Result<()> {
let conn = connection()?;
let stats = db::stats(&conn)?;
+ debug!("reporting stats");
+
let s = format!(
r#"
{} memes total
@@ -363,9 +406,17 @@ fn rand_meme(ctx: &Context, message: &Message, audio_only: bool) -> Result<()> {
InvocationRecord::create(&conn, message.author.id.0, message.id.0, mem.id, true)?;
send_meme(ctx, &mem, &conn, message).map_err(Error::from)
},
- err @ Err(_) => {
- send(message.channel_id, "i don't know any :(", message.tts)?;
- err.map(|_| {})
+ Err(e) => {
+ match e.downcast_ref::<DieselError>() {
+ Some(NotFound) => {
+ info!("random meme not found");
+ return send(message.channel_id, "i don't know any :(", message.tts)
+ },
+ _ => {},
+ }
+
+ send(message.channel_id, "HELP", message.tts)?;
+ return Err(e);
},
}
}
diff --git a/src/commands/playback.rs b/src/commands/playback.rs
index dd1ffbd..64d30cd 100644
--- a/src/commands/playback.rs
+++ b/src/commands/playback.rs
@@ -22,14 +22,15 @@ pub fn _play(ctx: &Context, msg: &Message, url: &str) -> Result<()> {
debug!("playing '{}'", url);
if !url.starts_with("http") {
+ warn!("got bad url argument to play: {}", url);
send(msg.channel_id, "bAD LiNk", msg.tts)?;
return Ok(());
}
let url = match Url::parse(url) {
Err(e) => {
- send(msg.channel_id, "INVALID URL", msg.tts)?;
- return Err(e.into());
+ error!("bad url: {}", e);
+ return send(msg.channel_id, "INVALID URL", msg.tts);
},
Ok(u) => u,
};
@@ -40,6 +41,8 @@ pub fn _play(ctx: &Context, msg: &Message, url: &str) -> Result<()> {
});
if host.map(|h| h.to_lowercase().contains("imgur")).unwrap_or(false) {
+ info!("detected imgur link");
+
if msg.author.id.0 == 106160362109272064 {
send(msg.channel_id, "fuck you conway", true)?;
} else {
@@ -72,7 +75,10 @@ pub fn play(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> {
let url = match args.single::<String>() {
Ok(url) => url,
- Err(_) => return send(msg.channel_id, "BAD LINK", msg.tts),
+ Err(e) => {
+ error!("unable to parse url from args: {}", e);
+ return send(msg.channel_id, "BAD LINK", msg.tts);
+ },
};
_play(ctx, msg, &url)
@@ -102,6 +108,8 @@ pub fn pause(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> {
let queue = queue_lock.write().unwrap();
let ref audio = queue.playing.clone().unwrap().audio;
audio.lock().pause();
+
+ info!("paused playback");
}
Ok(())
@@ -132,6 +140,7 @@ fn _resume(ctx: &mut Context, msg: &Message) -> Result<()> {
if playing {
done()?;
+ debug!("attempted to resume playback while sound was already playing");
return Ok(());
}
@@ -139,6 +148,7 @@ fn _resume(ctx: &mut Context, msg: &Message) -> Result<()> {
let queue = queue_lock.write().unwrap();
let ref audio = queue.playing.clone().unwrap().audio;
audio.lock().play();
+ info!("playback resumed");
}
Ok(())
@@ -156,6 +166,7 @@ pub fn skip(ctx: &mut Context, _msg: &Message, _args: Args) -> Result<()> {
handler.stop();
let mut play_queue = queue_lock.write().unwrap();
play_queue.playing = None;
+ info!("skipped currently-playing audio");
} else {
debug!("got skip with no handler attached");
}
@@ -180,6 +191,7 @@ pub fn die(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> {
}
if let Some(handler) = manager.get_mut(*TARGET_GUILD_ID) {
+ info!("killing playback");
handler.stop();
handler.leave();
} else {
@@ -197,6 +209,7 @@ pub fn list(ctx: &mut Context, msg: &Message, _: Args) -> Result<()> {
let channel_tmp = msg.channel().unwrap().guild().unwrap();
let channel = channel_tmp.read();
+ info!("listing queue");
match play_queue.playing {
Some(ref info) => {
let audio = info.audio.lock();
diff --git a/src/commands/roll.rs b/src/commands/roll.rs
index a6ea3d8..a2a05ef 100644
--- a/src/commands/roll.rs
+++ b/src/commands/roll.rs
@@ -294,7 +294,7 @@ pub fn roll(_ctx: &mut Context, msg: &Message, args: Args) -> Result<()> {
Err(e) => {
let parse_err = e.downcast::<CalcParseError>().unwrap();
if let CalcParseError::NotReadToEnd { remaining } = parse_err {
- error!("parsing {}: failed to consume '{}'", args.rest(), remaining);
+ error!("parsing '{}': failed to consume '{}'", args.rest(), remaining);
send(msg.channel_id, "I COULDN'T READ THAT YOU FUCK", msg.tts)
} else {
Err(parse_err.into())
diff --git a/src/commands/sound_levels.rs b/src/commands/sound_levels.rs
index fee3e37..60803a3 100644
--- a/src/commands/sound_levels.rs
+++ b/src/commands/sound_levels.rs
@@ -6,9 +6,7 @@ use serenity::{
use crate::{
audio::{PlayQueue, VoiceManager},
- commands::{
- send,
- },
+ commands::send,
Result,
TARGET_GUILD_ID,
};
@@ -58,13 +56,21 @@ pub fn volume(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> {
(play_queue.volume / DEFAULT_VOLUME * 100.0) as usize
};
+ trace!("reporting volume {}", vol);
+
return send(msg.channel_id, &format!("volume: {}%", vol), msg.tts);
}
let vol: usize = match args.single::<f32>() {
- Ok(vol) if vol.is_nan() => return send(msg.channel_id, "you're a fuck", msg.tts),
+ Ok(vol) if vol.is_nan() => {
+ warn!("reporting NaN volume");
+ return send(msg.channel_id, "you're a fuck", msg.tts);
+ },
Ok(vol) => vol as usize,
- Err(_) => return send(msg.channel_id, "???????", msg.tts),
+ Err(e) => {
+ error!("parsing volume arg: {}", e);
+ return send(msg.channel_id, "???????", msg.tts)
+ },
};
let mut vol: f32 = (vol as f32)/100.0; // force aliasing to reasonable values
@@ -83,6 +89,7 @@ pub fn volume(ctx: &mut Context, msg: &Message, mut args: Args) -> Result<()> {
{
let mut play_queue = queue_lock.write().unwrap();
play_queue.volume = vol * DEFAULT_VOLUME;
+ info!("volume updated to {}", vol);
}
send(msg.channel_id, format!("volume adjusted{}", adjusted_text), msg.tts)?;