From 42e79b6f29f5cea1d7207dc14aabf1f89dac7f4c Mon Sep 17 00:00:00 2001 From: Oupson Date: Sat, 5 Sep 2020 15:54:29 +0200 Subject: [PATCH] Upgrade Serenity Use log --- Cargo.toml | 2 +- log4rs.yaml | 3 ++- src/commands/general.rs | 13 +++++++------ src/commands/music.rs | 5 +++-- src/commands/roulette.rs | 11 ++++++----- src/main.rs | 29 ++++++++++++++--------------- 6 files changed, 33 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8a5b1f3..3398b1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ edition = "2018" music = ["serenity/voice"] [dependencies] -serenity = { version = "0.9.0-rc.0" } +serenity = { version = "0.9.0-rc.1" } toml = "0.5.6" serde = { version = "1.0", features = ["derive"] } reqwest = "0.10.7" diff --git a/log4rs.yaml b/log4rs.yaml index d522c12..49d457b 100644 --- a/log4rs.yaml +++ b/log4rs.yaml @@ -9,4 +9,5 @@ appenders: root: level: debug appenders: - - stdout \ No newline at end of file + - stdout + - file-debug \ No newline at end of file diff --git a/src/commands/general.rs b/src/commands/general.rs index 3b5125c..33ea7df 100644 --- a/src/commands/general.rs +++ b/src/commands/general.rs @@ -1,5 +1,6 @@ use crate::{api, debugln, ShardManagerContainer}; use futures::StreamExt; +use log::error; use serenity::{ client::bridge::gateway::ShardId, framework::standard::{ @@ -64,7 +65,7 @@ async fn latency(ctx: &Context, msg: &Message) -> CommandResult { #[bucket = "longcode"] pub async fn longcode(ctx: &Context, msg: &Message, args: Args) -> CommandResult { if let Err(e) = _longcode(ctx, msg, args).await { - eprintln!("Error in longcode : {:?}", e); + error!("Error in longcode : {:?}", e); } Ok(()) } @@ -164,12 +165,12 @@ async fn invite(ctx: &Context, msg: &Message) -> CommandResult { let invite = unsafe { &crate::INVITE_URL }; if let Some(invite_url) = invite { if let Err(e) = api::send_reply(ctx, msg, invite_url).await { - eprintln!("Error when sending invite : {:?}", e); + error!("Error when sending invite : {:?}", e); } } else if let Err(e) = api::send_reply(ctx, msg, "Error : Invite URL is not specified in config").await { - eprintln!("Error in invite : {:?}", e); + error!("Error in invite : {:?}", e); } Ok(()) } @@ -178,7 +179,7 @@ async fn invite(ctx: &Context, msg: &Message) -> CommandResult { #[description = "Find who is the older"] pub async fn older(ctx: &Context, msg: &Message, args: Args) -> CommandResult { if let Err(e) = _older(ctx, msg, args).await { - eprintln!("Error in older : {:?}", e); + error!("Error in older : {:?}", e); } Ok(()) } @@ -215,7 +216,7 @@ async fn _older(ctx: &Context, msg: &Message, mut args: Args) -> crate::commands } } } - Err(error) => eprintln!("Uh oh! Error: {}", error), + Err(error) => error!("Uh oh! Error: {}", error), } } @@ -249,7 +250,7 @@ async fn ping(ctx: &Context, msg: &Message) -> CommandResult { #[bucket = "image"] pub async fn image(ctx: &Context, msg: &Message, args: Args) -> CommandResult { if let Err(e) = _image(ctx, msg, args).await { - eprintln!("Error in image : {:?}", e); + error!("Error in image : {:?}", e); } Ok(()) } diff --git a/src/commands/music.rs b/src/commands/music.rs index 802e9dc..04f58c5 100644 --- a/src/commands/music.rs +++ b/src/commands/music.rs @@ -1,3 +1,4 @@ +use log::{error, info}; use serenity::{ client::{bridge::voice::ClientVoiceManager, Context}, framework::standard::{ @@ -163,7 +164,7 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { let source = match voice::ytdl(&url).await { Ok(source) => source, Err(why) => { - println!("Err starting source: {:?}", why); + error!("Err starting source: {:?}", why); check_msg(msg.channel_id.say(&ctx.http, "Error sourcing ffmpeg").await); @@ -228,6 +229,6 @@ async fn stop(ctx: &Context, msg: &Message) -> CommandResult { /// Checks that a message successfully sent; if not, then logs why to stderr. fn check_msg(result: SerenityResult) { if let Err(why) = result { - eprintln!("Error sending message: {:?}", why); + error!("Error sending message: {:?}", why); } } diff --git a/src/commands/roulette.rs b/src/commands/roulette.rs index fb177cd..06c0efa 100644 --- a/src/commands/roulette.rs +++ b/src/commands/roulette.rs @@ -1,4 +1,5 @@ use crate::{api, commands, debugln}; +use log::error; use rand::Rng; use serenity::{ framework::standard::{ @@ -35,7 +36,7 @@ async fn shot(ctx: &Context, msg: &Message, args: Args) -> CommandResult { let _message = args.message().trim_end(); if _message == "shot" || _message == "" { if let Err(e) = _shot(ctx, msg).await { - eprintln!("Error in shot : {:?}", e); + error!("Error in shot : {:?}", e); } } else if let Err(e) = api::send_reply( ctx, @@ -44,7 +45,7 @@ async fn shot(ctx: &Context, msg: &Message, args: Args) -> CommandResult { ) .await { - eprintln!("Error : {:?}", e); + error!("Error : {:?}", e); } Ok(()) } @@ -78,7 +79,7 @@ async fn _shot(ctx: &Context, msg: &Message) -> commands::Result<()> { #[bucket = "roulette"] async fn reload(ctx: &Context, msg: &Message) -> CommandResult { if let Err(e) = _reload(ctx, msg).await { - eprintln!("{}", e); + error!("{}", e); } Ok(()) } @@ -129,10 +130,10 @@ async fn kick(ctx: &Context, msg: &Message) -> CommandResult { if let Err(e) = api::send_reply(ctx, msg, "Error, this command cannot be used by a bot").await { - eprintln!("Error in kick : {:?}", e); + error!("Error in kick : {:?}", e); } } else if let Err(e) = _kick(ctx, msg).await { - eprintln!("Error in kick : {:?}", e); + error!("Error in kick : {:?}", e); } Ok(()) } diff --git a/src/main.rs b/src/main.rs index 71c1ec9..bbaaf3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,9 +5,10 @@ use crate::commands::{ roulette::{BulletsContainer, NonKickGuildsContainer, ROULETTE_GROUP}, }; use async_trait::async_trait; +use log::{debug, error, info}; use serde_json::Value; use serenity::{ - client::bridge::gateway::{event::ShardStageUpdateEvent, ShardManager}, + client::bridge::gateway::ShardManager, framework::standard::{ help_commands, macros::{help, hook}, @@ -48,7 +49,6 @@ impl TypeMapKey for ShardManagerContainer { } // TODO CLAP FOR CLI -// TODO USE LOG #[tokio::main] async fn main() -> IoResult<()> { log4rs::init_file("log4rs.yaml", Default::default()).unwrap(); @@ -202,11 +202,10 @@ impl Messages { }*/ } - #[async_trait] impl EventHandler for Messages { async fn ready(&self, ctx: Context, ready: Ready) { - println!("{} connected to discord", ready.user.name); + info!("{} connected to discord", ready.user.name); ctx.set_presence(Some(Activity::listening("?")), OnlineStatus::Online) .await; @@ -227,17 +226,17 @@ impl EventHandler for Messages { debugln!("ctrl-c"); let data = ctx.data.read().await; - println!("Saving data ..."); + info!("Saving data ..."); if let Err(e) = save_data(&data).await { - eprintln!("Error while saving data : {:?}", e); + error!("Error while saving data : {:?}", e); } - println!("Data saved"); + info!("Data saved"); if let Some(manager) = data.get::() { manager.lock().await.shutdown_all().await; - println!("Stopped"); + info!("Stopped"); } else { - eprintln!("There was a problem getting the shard manager"); + error!("There was a problem getting the shard manager"); } } @@ -245,14 +244,14 @@ impl EventHandler for Messages { if unsafe { LOG_ATTACHMENTS } && !new_message.attachments.is_empty() { for att in new_message.attachments { if let Err(e) = download_to_log(att).await { - eprintln!("Error while downloading to log : {:?}", e); + error!("Error while downloading to log : {:?}", e); } } } } async fn unknown(&self, _ctx: Context, name: String, raw: Value) { - println!("Unknown event : {}, {:?}", name, raw); + debug!("Unknown event : {}, {:?}", name, raw); } } @@ -282,12 +281,12 @@ async fn download_to_log(attachment: Attachment) -> commands::Result<()> { #[hook] async fn unknown_command(_ctx: &Context, _msg: &Message, unknown_command_name: &str) { - println!("Could not find command named '{}'", unknown_command_name); + debug!("Could not find command named '{}'", unknown_command_name); } #[hook] async fn normal_message(_ctx: &Context, msg: &Message) { - println!("Message is not a command '{}'", msg.content); + debug!("Message is not a command '{}'", msg.content); } #[hook] @@ -299,7 +298,7 @@ async fn dispatch_error(ctx: &Context, msg: &Message, error: DispatchError) { .channel_id .say( &ctx.http, - &format!("Try this again in {} seconds.", seconds), + &format!("Try this again in {} seconds.", seconds.as_secs()), ) .await; } @@ -323,7 +322,7 @@ async fn dispatch_error(ctx: &Context, msg: &Message, error: DispatchError) { async fn after_hook(_: &Context, _: &Message, cmd_name: &str, error: Result<(), CommandError>) { // Print out an error if it happened if let Err(why) = error { - println!("Error in {}: {:?}", cmd_name, why); + error!("Error in {}: {:?}", cmd_name, why); } }