mirror of
https://github.com/revanced/revanced-discord-bot.git
synced 2025-04-29 14:04:28 +02:00
feat: update logger and other small changes
This commit is contained in:
parent
3a6d81ba1d
commit
cee17a11eb
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
/target
|
||||
configuration.json
|
||||
.env
|
||||
.env
|
||||
*.log
|
||||
|
85
Cargo.lock
generated
85
Cargo.lock
generated
@ -17,6 +17,12 @@ dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.58"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bb07d2053ccdbe10e2af2995a2f116c1330396493dc1269f6a91d0ae82e19704"
|
||||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.56"
|
||||
@ -152,6 +158,26 @@ dependencies = [
|
||||
"crypto-common",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs"
|
||||
version = "4.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
|
||||
dependencies = [
|
||||
"dirs-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs-sys"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"redox_users",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dotenv"
|
||||
version = "0.15.0"
|
||||
@ -621,6 +647,26 @@ dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.2.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_users"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"redox_syscall",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.6.0"
|
||||
@ -683,7 +729,9 @@ dependencies = [
|
||||
name = "revanced-discord-bot"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"chrono",
|
||||
"dirs",
|
||||
"dotenv",
|
||||
"log",
|
||||
"regex",
|
||||
@ -691,6 +739,7 @@ dependencies = [
|
||||
"serde_json",
|
||||
"serde_regex",
|
||||
"serenity",
|
||||
"simplelog",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
@ -847,6 +896,17 @@ dependencies = [
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "simplelog"
|
||||
version = "0.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "48dfff04aade74dd495b007c831cd6f4e0cee19c344dd9dc0884c0289b70a786"
|
||||
dependencies = [
|
||||
"log",
|
||||
"termcolor",
|
||||
"time 0.3.11",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "slab"
|
||||
version = "0.4.6"
|
||||
@ -880,6 +940,15 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termcolor"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.31"
|
||||
@ -921,8 +990,15 @@ dependencies = [
|
||||
"libc",
|
||||
"num_threads",
|
||||
"serde",
|
||||
"time-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time-macros"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42657b1a6f4d817cda8e7a0ace261fe0cc946cf3a80314390b22cc61ae080792"
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec"
|
||||
version = "1.6.0"
|
||||
@ -1264,6 +1340,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",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
|
@ -19,10 +19,13 @@ panic = "abort"
|
||||
[dependencies]
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
dotenv = "0.15"
|
||||
serde_json = "1.0"
|
||||
serde_json = "1.0"
|
||||
tokio = { version = "1.0", features = ["rt-multi-thread"] }
|
||||
log = "0.4"
|
||||
log = { version = "0.4", features = ["max_level_debug", "release_max_level_warn"] }
|
||||
regex = "1.0"
|
||||
serde_regex = "1.1"
|
||||
chrono = "0.4"
|
||||
serenity = { git = "https://github.com/serenity-rs/serenity.git", default_features = false, features = ["client", "gateway", "rustls_backend", "model"] }
|
||||
serenity = { git = "https://github.com/serenity-rs/serenity.git", default_features = false, features = ["client", "gateway", "rustls_backend", "model"] }
|
||||
simplelog = "0.12.0"
|
||||
anyhow = "1.0.58"
|
||||
dirs = "4.0.0"
|
||||
|
18
src/logger.rs
Normal file
18
src/logger.rs
Normal file
@ -0,0 +1,18 @@
|
||||
use std::fs::File;
|
||||
|
||||
use log::LevelFilter;
|
||||
use simplelog::{ColorChoice, CombinedLogger, Config, TermLogger, TerminalMode, WriteLogger};
|
||||
|
||||
pub fn init() -> anyhow::Result<()> {
|
||||
CombinedLogger::init(vec![
|
||||
TermLogger::new(
|
||||
LevelFilter::Debug,
|
||||
Config::default(),
|
||||
TerminalMode::Mixed,
|
||||
ColorChoice::Auto,
|
||||
),
|
||||
WriteLogger::new(LevelFilter::Info, Config::default(), File::create("logs.log")?),
|
||||
])?;
|
||||
|
||||
Ok(())
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
use log::{Level, Metadata, Record};
|
||||
|
||||
pub struct SimpleLogger;
|
||||
|
||||
impl log::Log for SimpleLogger {
|
||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
||||
metadata.level() <= Level::Info
|
||||
}
|
||||
|
||||
fn log(&self, record: &Record) {
|
||||
if self.enabled(record.metadata()) {
|
||||
println!("{} - {}", record.level(), record.args());
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
@ -1 +0,0 @@
|
||||
pub mod logging;
|
24
src/main.rs
24
src/main.rs
@ -1,9 +1,8 @@
|
||||
use std::env;
|
||||
use std::sync::Arc;
|
||||
use std::{env, process};
|
||||
|
||||
use chrono::{DateTime, Duration, NaiveDateTime, Utc};
|
||||
use log::{error, info, trace, LevelFilter};
|
||||
use logger::logging::SimpleLogger;
|
||||
use log::{error, info, trace};
|
||||
use model::application::Configuration;
|
||||
use regex::Regex;
|
||||
use serenity::client::{Context, EventHandler};
|
||||
@ -13,11 +12,10 @@ use serenity::model::prelude::command::Command;
|
||||
use serenity::model::prelude::interaction::{Interaction, InteractionResponseType, MessageFlags};
|
||||
use serenity::prelude::{GatewayIntents, RwLock, TypeMapKey};
|
||||
use serenity::{async_trait, Client};
|
||||
|
||||
mod logger;
|
||||
mod model;
|
||||
|
||||
static LOGGER: SimpleLogger = SimpleLogger;
|
||||
|
||||
struct BotConfiguration;
|
||||
|
||||
impl TypeMapKey for BotConfiguration {
|
||||
@ -111,7 +109,7 @@ impl EventHandler for Handler {
|
||||
}
|
||||
|
||||
if stop_command {
|
||||
std::process::exit(0);
|
||||
process::exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -225,9 +223,7 @@ impl EventHandler for Handler {
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Initialize the logging framework.
|
||||
log::set_logger(&LOGGER)
|
||||
.map(|()| log::set_max_level(LevelFilter::Warn))
|
||||
.expect("Could not set logger.");
|
||||
logger::init().expect("failed to init logger");
|
||||
|
||||
// Set up the configuration.
|
||||
let configuration = load_configuration();
|
||||
@ -238,7 +234,7 @@ async fn main() {
|
||||
Some((_, value)) => value,
|
||||
None => {
|
||||
error!("Environment variable DISCORD_AUTHORIZATION_TOKEN unset.");
|
||||
std::process::exit(1);
|
||||
process::exit(1);
|
||||
},
|
||||
};
|
||||
|
||||
@ -255,9 +251,7 @@ async fn main() {
|
||||
client.data.write().await.insert::<BotConfiguration>(Arc::new(RwLock::new(configuration)));
|
||||
|
||||
// Start the Discord bot.
|
||||
if let Err(why) = client.start().await {
|
||||
error!("{:?}", why);
|
||||
} else {
|
||||
info!("Client started.");
|
||||
}
|
||||
client.start().await.expect("failed to start discord bot");
|
||||
|
||||
info!("Client started.");
|
||||
}
|
||||
|
@ -1,6 +1,10 @@
|
||||
use std::fs::File;
|
||||
use std::io::{Error, Read, Write};
|
||||
use std::{
|
||||
fs::{self, File},
|
||||
io::{Read, Result, Write},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use dirs::config_dir;
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@ -11,26 +15,45 @@ pub struct Configuration {
|
||||
pub message_responses: Vec<MessageResponse>,
|
||||
}
|
||||
|
||||
const CONFIG_PATH: &str = "configuration.json";
|
||||
|
||||
impl Configuration {
|
||||
fn save(&self) -> Result<(), Error> {
|
||||
let mut file = File::create("configuration.json")?;
|
||||
fn save(&self) -> Result<()> {
|
||||
let sys_config_dir = config_dir().expect("find config dir");
|
||||
|
||||
fs::create_dir_all(format!("{}/revanced-discord-bot", sys_config_dir.to_string_lossy()))
|
||||
.expect("create config dir");
|
||||
|
||||
let mut file = File::create(CONFIG_PATH)?;
|
||||
let json = serde_json::to_string_pretty(&self)?;
|
||||
file.write_all(json.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load() -> Result<Configuration, Error> {
|
||||
let mut file = match File::open("configuration.json") {
|
||||
Ok(file) => file,
|
||||
Err(_) => {
|
||||
let configuration = Configuration::default();
|
||||
configuration.save()?;
|
||||
return Ok(configuration);
|
||||
},
|
||||
pub fn load() -> Result<Configuration> {
|
||||
let sys_config_dir = config_dir().expect("Can not find the configuration directory.");
|
||||
let sys_config =
|
||||
format!("{}/revanced-discord-bot/{CONFIG_PATH}", sys_config_dir.to_string_lossy());
|
||||
|
||||
// config file in current dir
|
||||
let mut file = if Path::new(CONFIG_PATH).exists() {
|
||||
File::open(CONFIG_PATH)?
|
||||
}
|
||||
// config file in system dir (on *nix: `~/.config/revanced-discord-bot/`)
|
||||
else if Path::new(&sys_config).exists() {
|
||||
File::open(sys_config)?
|
||||
}
|
||||
// create defalt config
|
||||
else {
|
||||
let default_config = Configuration::default();
|
||||
default_config.save()?;
|
||||
|
||||
File::open(sys_config)?
|
||||
};
|
||||
|
||||
let mut buf = String::new();
|
||||
file.read_to_string(&mut buf)?;
|
||||
|
||||
Ok(serde_json::from_str(&buf)?)
|
||||
}
|
||||
}
|
||||
@ -106,16 +129,14 @@ pub struct Author {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Includes {
|
||||
pub channels: Vec<u64>,
|
||||
#[serde(rename = "match")]
|
||||
#[serde(with = "serde_regex")]
|
||||
#[serde(rename = "match", with = "serde_regex")]
|
||||
pub match_field: Vec<Regex>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Excludes {
|
||||
pub roles: Vec<u64>,
|
||||
#[serde(rename = "match")]
|
||||
#[serde(with = "serde_regex")]
|
||||
#[serde(rename = "match", with = "serde_regex")]
|
||||
pub match_field: Vec<Regex>,
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user