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