Upgrade Serenity

Use log
This commit is contained in:
Oupson 2020-09-05 15:54:29 +02:00
parent e4d838d1fe
commit 42e79b6f29
6 changed files with 33 additions and 30 deletions

View File

@ -10,7 +10,7 @@ edition = "2018"
music = ["serenity/voice"] music = ["serenity/voice"]
[dependencies] [dependencies]
serenity = { version = "0.9.0-rc.0" } serenity = { version = "0.9.0-rc.1" }
toml = "0.5.6" toml = "0.5.6"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
reqwest = "0.10.7" reqwest = "0.10.7"

View File

@ -10,3 +10,4 @@ root:
level: debug level: debug
appenders: appenders:
- stdout - stdout
- file-debug

View File

@ -1,5 +1,6 @@
use crate::{api, debugln, ShardManagerContainer}; use crate::{api, debugln, ShardManagerContainer};
use futures::StreamExt; use futures::StreamExt;
use log::error;
use serenity::{ use serenity::{
client::bridge::gateway::ShardId, client::bridge::gateway::ShardId,
framework::standard::{ framework::standard::{
@ -64,7 +65,7 @@ async fn latency(ctx: &Context, msg: &Message) -> CommandResult {
#[bucket = "longcode"] #[bucket = "longcode"]
pub async fn longcode(ctx: &Context, msg: &Message, args: Args) -> CommandResult { pub async fn longcode(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
if let Err(e) = _longcode(ctx, msg, args).await { if let Err(e) = _longcode(ctx, msg, args).await {
eprintln!("Error in longcode : {:?}", e); error!("Error in longcode : {:?}", e);
} }
Ok(()) Ok(())
} }
@ -164,12 +165,12 @@ async fn invite(ctx: &Context, msg: &Message) -> CommandResult {
let invite = unsafe { &crate::INVITE_URL }; let invite = unsafe { &crate::INVITE_URL };
if let Some(invite_url) = invite { if let Some(invite_url) = invite {
if let Err(e) = api::send_reply(ctx, msg, invite_url).await { 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) = } else if let Err(e) =
api::send_reply(ctx, msg, "Error : Invite URL is not specified in config").await 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(()) Ok(())
} }
@ -178,7 +179,7 @@ async fn invite(ctx: &Context, msg: &Message) -> CommandResult {
#[description = "Find who is the older"] #[description = "Find who is the older"]
pub async fn older(ctx: &Context, msg: &Message, args: Args) -> CommandResult { pub async fn older(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
if let Err(e) = _older(ctx, msg, args).await { if let Err(e) = _older(ctx, msg, args).await {
eprintln!("Error in older : {:?}", e); error!("Error in older : {:?}", e);
} }
Ok(()) 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"] #[bucket = "image"]
pub async fn image(ctx: &Context, msg: &Message, args: Args) -> CommandResult { pub async fn image(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
if let Err(e) = _image(ctx, msg, args).await { if let Err(e) = _image(ctx, msg, args).await {
eprintln!("Error in image : {:?}", e); error!("Error in image : {:?}", e);
} }
Ok(()) Ok(())
} }

View File

@ -1,3 +1,4 @@
use log::{error, info};
use serenity::{ use serenity::{
client::{bridge::voice::ClientVoiceManager, Context}, client::{bridge::voice::ClientVoiceManager, Context},
framework::standard::{ framework::standard::{
@ -163,7 +164,7 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let source = match voice::ytdl(&url).await { let source = match voice::ytdl(&url).await {
Ok(source) => source, Ok(source) => source,
Err(why) => { Err(why) => {
println!("Err starting source: {:?}", why); error!("Err starting source: {:?}", why);
check_msg(msg.channel_id.say(&ctx.http, "Error sourcing ffmpeg").await); 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. /// Checks that a message successfully sent; if not, then logs why to stderr.
fn check_msg(result: SerenityResult<Message>) { fn check_msg(result: SerenityResult<Message>) {
if let Err(why) = result { if let Err(why) = result {
eprintln!("Error sending message: {:?}", why); error!("Error sending message: {:?}", why);
} }
} }

View File

@ -1,4 +1,5 @@
use crate::{api, commands, debugln}; use crate::{api, commands, debugln};
use log::error;
use rand::Rng; use rand::Rng;
use serenity::{ use serenity::{
framework::standard::{ framework::standard::{
@ -35,7 +36,7 @@ async fn shot(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let _message = args.message().trim_end(); let _message = args.message().trim_end();
if _message == "shot" || _message == "" { if _message == "shot" || _message == "" {
if let Err(e) = _shot(ctx, msg).await { 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( } else if let Err(e) = api::send_reply(
ctx, ctx,
@ -44,7 +45,7 @@ async fn shot(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
) )
.await .await
{ {
eprintln!("Error : {:?}", e); error!("Error : {:?}", e);
} }
Ok(()) Ok(())
} }
@ -78,7 +79,7 @@ async fn _shot(ctx: &Context, msg: &Message) -> commands::Result<()> {
#[bucket = "roulette"] #[bucket = "roulette"]
async fn reload(ctx: &Context, msg: &Message) -> CommandResult { async fn reload(ctx: &Context, msg: &Message) -> CommandResult {
if let Err(e) = _reload(ctx, msg).await { if let Err(e) = _reload(ctx, msg).await {
eprintln!("{}", e); error!("{}", e);
} }
Ok(()) Ok(())
} }
@ -129,10 +130,10 @@ async fn kick(ctx: &Context, msg: &Message) -> CommandResult {
if let Err(e) = if let Err(e) =
api::send_reply(ctx, msg, "Error, this command cannot be used by a bot").await 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 { } else if let Err(e) = _kick(ctx, msg).await {
eprintln!("Error in kick : {:?}", e); error!("Error in kick : {:?}", e);
} }
Ok(()) Ok(())
} }

View File

@ -5,9 +5,10 @@ use crate::commands::{
roulette::{BulletsContainer, NonKickGuildsContainer, ROULETTE_GROUP}, roulette::{BulletsContainer, NonKickGuildsContainer, ROULETTE_GROUP},
}; };
use async_trait::async_trait; use async_trait::async_trait;
use log::{debug, error, info};
use serde_json::Value; use serde_json::Value;
use serenity::{ use serenity::{
client::bridge::gateway::{event::ShardStageUpdateEvent, ShardManager}, client::bridge::gateway::ShardManager,
framework::standard::{ framework::standard::{
help_commands, help_commands,
macros::{help, hook}, macros::{help, hook},
@ -48,7 +49,6 @@ impl TypeMapKey for ShardManagerContainer {
} }
// TODO CLAP FOR CLI // TODO CLAP FOR CLI
// TODO USE LOG
#[tokio::main] #[tokio::main]
async fn main() -> IoResult<()> { async fn main() -> IoResult<()> {
log4rs::init_file("log4rs.yaml", Default::default()).unwrap(); log4rs::init_file("log4rs.yaml", Default::default()).unwrap();
@ -202,11 +202,10 @@ impl Messages {
}*/ }*/
} }
#[async_trait] #[async_trait]
impl EventHandler for Messages { impl EventHandler for Messages {
async fn ready(&self, ctx: Context, ready: Ready) { 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) ctx.set_presence(Some(Activity::listening("?")), OnlineStatus::Online)
.await; .await;
@ -227,17 +226,17 @@ impl EventHandler for Messages {
debugln!("ctrl-c"); debugln!("ctrl-c");
let data = ctx.data.read().await; let data = ctx.data.read().await;
println!("Saving data ..."); info!("Saving data ...");
if let Err(e) = save_data(&data).await { 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::<ShardManagerContainer>() { if let Some(manager) = data.get::<ShardManagerContainer>() {
manager.lock().await.shutdown_all().await; manager.lock().await.shutdown_all().await;
println!("Stopped"); info!("Stopped");
} else { } 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() { if unsafe { LOG_ATTACHMENTS } && !new_message.attachments.is_empty() {
for att in new_message.attachments { for att in new_message.attachments {
if let Err(e) = download_to_log(att).await { 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) { 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] #[hook]
async fn unknown_command(_ctx: &Context, _msg: &Message, unknown_command_name: &str) { 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] #[hook]
async fn normal_message(_ctx: &Context, msg: &Message) { 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] #[hook]
@ -299,7 +298,7 @@ async fn dispatch_error(ctx: &Context, msg: &Message, error: DispatchError) {
.channel_id .channel_id
.say( .say(
&ctx.http, &ctx.http,
&format!("Try this again in {} seconds.", seconds), &format!("Try this again in {} seconds.", seconds.as_secs()),
) )
.await; .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>) { async fn after_hook(_: &Context, _: &Message, cmd_name: &str, error: Result<(), CommandError>) {
// Print out an error if it happened // Print out an error if it happened
if let Err(why) = error { if let Err(why) = error {
println!("Error in {}: {:?}", cmd_name, why); error!("Error in {}: {:?}", cmd_name, why);
} }
} }