Add basic logging to the dice bot.

This commit is contained in:
projectmoon 2020-08-28 00:13:01 +00:00
parent f0a1fe53fc
commit 54a3cc0880
5 changed files with 75 additions and 8 deletions

59
Cargo.lock generated
View File

@ -95,6 +95,17 @@ dependencies = [
"autocfg",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi 0.3.9",
]
[[package]]
name = "autocfg"
version = "1.0.1"
@ -160,7 +171,9 @@ version = "0.3.0"
dependencies = [
"async-trait",
"dirs",
"env_logger",
"itertools",
"log",
"matrix-sdk",
"matrix-sdk-base 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk?rev=0.1.0)",
"matrix-sdk-common 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk?rev=0.1.0)",
@ -308,6 +321,19 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "env_logger"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -587,6 +613,15 @@ version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"
[[package]]
name = "humantime"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
dependencies = [
"quick-error",
]
[[package]]
name = "hyper"
version = "0.13.7"
@ -1141,6 +1176,12 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "quick-error"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "quote"
version = "1.0.7"
@ -1795,6 +1836,15 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "termcolor"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
dependencies = [
"winapi-util",
]
[[package]]
name = "thiserror"
version = "1.0.20"
@ -2161,6 +2211,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View File

@ -11,6 +11,8 @@ keywords = ["games", "dice", "matrix", "bot"]
categories = ["games"]
[dependencies]
log = "0.4"
env_logger = "0.7"
toml = "0.5"
nom = "5"
rand = "0.7"

View File

@ -1,5 +1,6 @@
use chronicle_dicebot::bot::run_bot;
use chronicle_dicebot::bot::Config;
use env_logger::Env;
use std::fs;
use std::path::PathBuf;
@ -15,6 +16,8 @@ fn read_config<P: Into<PathBuf>>(config_path: P) -> Result<Config, Box<dyn std::
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::from_env(Env::default().default_filter_or("chronicle_dicebot=info")).init();
let config_path = std::env::args()
.skip(1)
.next()

View File

@ -1,5 +1,6 @@
use crate::commands::parse_command;
use dirs;
use log::{error, info, warn};
use matrix_sdk::{
self,
events::{
@ -66,10 +67,10 @@ impl EventEmitter for DiceBot {
}
let room = room.read().await;
println!("Autojoining room {}", room.display_name());
info!("Autojoining room {}", room.display_name());
match self.client.join_room_by_id(&room.room_id).await {
Err(e) => println!("Could not join room: {}", e.to_string()),
Err(e) => warn!("Could not join room: {}", e.to_string()),
_ => (),
}
}
@ -110,12 +111,14 @@ impl EventEmitter for DiceBot {
NoticeMessageEventContent::html(plain, html),
));
info!("{} executed: {}", sender_username, msg_body);
//we clone here to hold the lock for as little time as possible.
let room_id = room.read().await.room_id.clone();
let result = self.client.room_send(&room_id, content, None).await;
match result {
Err(e) => println!("Error sending message: {}", e.to_string()),
Err(e) => error!("Error sending message: {}", e.to_string()),
Ok(_) => (),
}
}
@ -157,10 +160,10 @@ pub async fn run_bot(config: MatrixConfig) -> Result<(), Box<dyn std::error::Err
.login(&username, &password, None, Some("matrix dice bot"))
.await?;
println!("Logged in as {}", username);
info!("Logged in as {}", username);
if should_sync {
println!("Performing initial sync");
info!("Performing initial sync");
client.sync(SyncSettings::default()).await?;
}
@ -176,7 +179,7 @@ pub async fn run_bot(config: MatrixConfig) -> Result<(), Box<dyn std::error::Err
let settings = SyncSettings::default().token(token);
//this keeps state from the server streaming in to the dice bot via the EventEmitter trait
println!("Listening for commands");
info!("Listening for commands");
client.sync_forever(settings, |_| async {}).await;
Ok(())

View File

@ -65,9 +65,9 @@ impl Command for PoolRollCommand {
/// command was recognized.
pub fn parse_command(s: &str) -> Result<Option<Box<dyn Command>>, String> {
match parser::parse_command(s) {
//The first clause prevents bot from spamming messages to itself
//after executing a previous command.
Ok((input, result)) => match (input, &result) {
//This clause prevents bot from spamming messages to itself
//after executing a previous command.
("", Some(_)) | (_, None) => Ok(result),
_ => Err(format!("{}: malformed dice expression", s)),
},