log properly

This commit is contained in:
nora 2022-03-18 20:34:34 +01:00
parent e6414abaf1
commit 1488568e12
6 changed files with 119 additions and 31 deletions

View file

@ -7,6 +7,7 @@ use serenity::model::prelude::*;
use serenity::prelude::*;
use std::collections::HashMap;
use std::time::Duration;
use tracing::info;
pub struct AutoRoleDataKey;
@ -44,7 +45,7 @@ async fn autorole(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
while let Some(answer) = &msg.author.await_reply(&ctx).timeout(Duration::from_secs(30)).await {
if let Some(emote) = answer.content.split(' ').next() {
println!("should add emote: {}", emote);
info!(%emote, "should add emote");
answer.react(http, '☑').await?;
}
}

View file

@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize};
use serenity::framework::standard::macros::hook;
use serenity::model::prelude::*;
use serenity::prelude::*;
use tracing::error;
#[derive(Debug, Serialize, Deserialize)]
pub struct ConfigFile {
@ -21,8 +22,8 @@ pub async fn normal_message(ctx: &Context, msg: &Message) {
let lowercase_content = msg.content.to_lowercase();
for (trigger, reaction) in &reactions.auto_react {
if lowercase_content.contains(trigger) {
if let Err(why) = msg.react(&ctx.http, ReactionType::Unicode(reaction.clone())).await {
eprintln!("Error reacting {}", why);
if let Err(err) = msg.react(&ctx.http, ReactionType::Unicode(reaction.clone())).await {
error!(%err, "Error reacting");
}
}
}

View file

@ -2,6 +2,7 @@ mod autorole;
mod commands;
mod general;
use anyhow::{bail, Context as _};
use std::collections::HashSet;
use std::fs;
@ -13,24 +14,33 @@ use serenity::framework::StandardFramework;
use serenity::http::Http;
use serenity::model::id::UserId;
use serenity::{async_trait, model::gateway::Ready, prelude::*};
use tracing::{info, Level};
struct Handler;
#[async_trait]
impl EventHandler for Handler {
async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
info!("{} is connected!", ready.user.name);
}
}
#[tokio::main]
async fn main() {
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt()
.with_level(true)
.with_timer(tracing_subscriber::fmt::time::time())
.with_ansi(true)
.with_thread_names(false)
.with_max_level(Level::INFO)
.init();
let file_path = std::env::var("CONFIG_PATH").unwrap_or_else(|_| "./config.json".to_string());
let file = fs::read_to_string(&file_path).unwrap_or_else(|err| {
eprintln!("Error reading config file ({file_path}): {err}");
std::process::exit(1);
});
let config = serde_json::from_str::<ConfigFile>(&file).unwrap();
let file = fs::read_to_string(&file_path)
.context("Error reading config file")
.context(file_path)?;
let config = serde_json::from_str::<ConfigFile>(&file).context("parse config")?;
let token = &config.token;
@ -42,10 +52,10 @@ async fn main() {
owners.insert(UserId(414755070161453076)); //nils
match http.get_current_user().await {
Ok(bot_id) => (owners, bot_id.id),
Err(why) => panic!("Could not access the bot id: {:?}", why),
Err(why) => bail!("Could not access the bot id: {:?}", why),
}
}
Err(why) => panic!("Could not access application info: {:?}", why),
Err(why) => bail!("Could not access application info: {:?}", why),
};
let framework = StandardFramework::new()
@ -64,7 +74,7 @@ async fn main() {
.event_handler(Handler)
.framework(framework)
.await
.expect("Err creating client");
.context("creating client")?;
{
let mut data = client.data.write().await;
@ -72,7 +82,7 @@ async fn main() {
data.insert::<ConfigFile>(config);
}
if let Err(why) = client.start().await {
println!("Client error: {:?}", why);
}
client.start().await.context("running bot")?;
Ok(())
}