Unverified Commit 77b925f2 authored by tec's avatar tec

Improve logging

parent 662f17c9
......@@ -2,3 +2,4 @@
Cargo.lock
**/*.rs.bk
src/discord_token
ucc-bot.log
......@@ -8,3 +8,5 @@ edition = "2018"
serenity = "0.6.3"
rand = "0.7.0"
lazy_static = "1.3.0"
log = "0.4.8"
simplelog = "0.6.0"
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate simplelog;
use simplelog::*;
use std::fs::File;
use serenity::{
model::{channel, channel::Message, gateway::Ready, guild::Member},
prelude::*,
......@@ -15,7 +21,7 @@ macro_rules! e {
($error: literal, $x:expr) => {
match $x {
Ok(_) => (),
Err(why) => eprintln!($error, why),
Err(why) => error!($error, why),
}
};
}
......@@ -102,11 +108,20 @@ impl EventHandler for Handler {
//
// In this case, just print what the current user's username is.
fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
info!("{} is connected!", ready.user.name);
}
}
fn main() {
CombinedLogger::init(vec![
TermLogger::new(LevelFilter::Info, Config::default(), TerminalMode::Mixed).unwrap(),
WriteLogger::new(
LevelFilter::Info,
Config::default(),
File::create("ucc-bot.log").unwrap(),
),
])
.unwrap();
// Configure the client with your Discord bot token in the environment.
let token = config::DISCORD_TOKEN;
......@@ -120,6 +135,6 @@ fn main() {
// Shards will automatically attempt to reconnect, and will perform
// exponential backoff until it reconnects.
if let Err(why) = client.start() {
println!("Client error: {:?}", why);
error!("Client error: {:?}", why);
}
}
......@@ -11,7 +11,7 @@ macro_rules! e {
($error: literal, $x:expr) => {
match $x {
Ok(_) => (),
Err(why) => eprintln!($error, why),
Err(why) => error!($error, why),
}
};
}
......@@ -24,18 +24,18 @@ pub fn new_member(ctx: &Context, mut new_member: Member) {
message.push_line("If you're not sure where to start, perhaps you could tell us about your projects, your first computer…");
message.push_line("You should also know that we follow the Freenode Channel Guidelines: https://freenode.net/changuide, and try to avoid defamatory content");
if let Err(why) = config::WELCOME_CHANNEL.say(&ctx, message.build()) {
println!("Error sending message: {:?}", why);
error!("Error sending message: {:?}", why);
}
let mut message = MessageBuilder::new();
message.push(format!("Say hi to {} in ", new_member.display_name()));
message.mention(&config::WELCOME_CHANNEL);
if let Err(why) = config::MAIN_CHANNEL.say(&ctx, message.build()) {
println!("Error sending message: {:?}", why);
error!("Error sending message: {:?}", why);
}
if let Err(why) = new_member.add_role(&ctx.http, config::UNREGISTERED_MEMBER_ROLE) {
println!("Error adding user role: {:?}", why);
error!("Error adding user role: {:?}", why);
};
}
......
......@@ -12,7 +12,7 @@ macro_rules! e {
($error: literal, $x:expr) => {
match $x {
Ok(_) => (),
Err(why) => eprintln!($error, why),
Err(why) => error!($error, why),
}
};
}
......@@ -78,9 +78,9 @@ impl Commands {
}
fn create_motion(ctx: &Context, msg: &Message, topic: &str) {
println!("{} created a motion {}", msg.author.name, topic);
info!("{} created a motion {}", msg.author.name, topic);
if let Err(why) = msg.delete(ctx) {
println!("Error deleting motion prompt: {:?}", why);
error!("Error deleting motion prompt: {:?}", why);
}
match msg.channel_id.send_message(&ctx.http, |m| {
m.embed(|embed| {
......@@ -115,14 +115,14 @@ fn create_motion(ctx: &Context, msg: &Message, topic: &str) {
m
}) {
Err(why) => {
println!("Error creating motion: {:?}", why);
error!("Error creating motion: {:?}", why);
}
Ok(_) => {}
}
}
fn create_poll(ctx: &Context, msg: &Message, topic: &str) {
println!("{} created a poll {}", msg.author.name, topic);
info!("{} created a poll {}", msg.author.name, topic);
match msg.channel_id.send_message(&ctx.http, |m| {
m.embed(|embed| {
embed.author(|a| {
......@@ -151,11 +151,11 @@ fn create_poll(ctx: &Context, msg: &Message, topic: &str) {
m
}) {
Err(why) => {
println!("Error sending message: {:?}", why);
error!("Error sending message: {:?}", why);
}
Ok(_) => {
if let Err(why) = msg.delete(ctx) {
println!("Error deleting motion prompt: {:?}", why);
error!("Error deleting motion prompt: {:?}", why);
}
}
}
......@@ -174,7 +174,7 @@ lazy_static! {
fn get_cached_motion(ctx: &Context, msg: &Message) -> MotionInfo {
let mut cached_motions = MOTIONS_CACHE.lock().unwrap();
if !cached_motions.contains_key(&msg.id) {
println!("Initialising representation of motion {:?}", msg.id);
info!("Initialising representation of motion {:?}", msg.id);
let this_motion = MotionInfo {
votes: {
let mut m = HashMap::new();
......@@ -204,7 +204,7 @@ fn set_cached_motion(id: &serenity::model::id::MessageId, motion_info: MotionInf
if let Some(motion) = MOTIONS_CACHE.lock().unwrap().get_mut(id) {
*motion = motion_info;
} else {
println!("{}", "Couldn't find motion in cache to set");
warn!("{}", "Couldn't find motion in cache to set");
}
}
......@@ -250,7 +250,7 @@ fn update_motion(
let old_embed = msg.embeds[0].clone();
let topic = old_embed.clone().title.unwrap();
println!(
info!(
" {:10} {:6} {} on {}",
user.name,
change,
......@@ -271,7 +271,7 @@ fn update_motion(
format!("{}\n_was_ {}", status, last_status_full),
true,
);
println!("Motion to {} now {}", topic, status);
info!("Motion to {} now {}", topic, status);
//
let mut message = MessageBuilder::new();
message.push_bold(topic);
......@@ -279,7 +279,7 @@ fn update_motion(
message.push_bold(status);
message.push_italic(format!(" (was {})", last_status));
if let Err(why) = config::ANNOUNCEMENT_CHANNEL.say(&ctx.http, message.build()) {
println!("Error sending message: {:?}", why);
error!("Error sending message: {:?}", why);
};
}
};
......@@ -336,7 +336,7 @@ fn update_motion(
e
})
}) {
println!("Error updating motion: {:?}", why);
error!("Error updating motion: {:?}", why);
}
}
......@@ -357,7 +357,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) {
{
if a_user.id.0 == user.id.0 {
if let Err(why) = add_reaction.delete(&ctx) {
println!("Error deleting react: {:?}", why);
error!("Error deleting react: {:?}", why);
};
return;
}
......@@ -367,7 +367,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) {
.contains(&add_reaction.emoji.as_data().as_str())
{
if let Err(why) = add_reaction.delete(&ctx) {
println!("Error deleting react: {:?}", why);
error!("Error deleting react: {:?}", why);
};
return;
}
......@@ -390,21 +390,21 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) {
.contains(&add_reaction.emoji.as_data().as_str())
{
if let Err(why) = add_reaction.delete(&ctx) {
println!("Error deleting react: {:?}", why);
error!("Error deleting react: {:?}", why);
};
return;
}
}
}
Err(why) => {
println!("Error getting user role: {:?}", why);
error!("Error getting user role: {:?}", why);
}
}
}
}
}
Err(why) => {
println!("Error processing react: {:?}", why);
error!("Error processing react: {:?}", why);
}
}
}
......@@ -427,7 +427,7 @@ pub fn reaction_remove(ctx: Context, removed_reaction: channel::Reaction) {
}
}
Err(why) => {
println!("Error getting user role: {:?}", why);
error!("Error getting user role: {:?}", why);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment