Add Matrix support (part 1)
This commit is contained in:
@@ -4,7 +4,9 @@ use colored::*;
|
||||
pub enum LogMessageType {
|
||||
Bot,
|
||||
Chat,
|
||||
Matrix,
|
||||
Error,
|
||||
MatrixError,
|
||||
}
|
||||
|
||||
pub fn log_error<T, E: std::fmt::Display>(result: Result<T, E>) {
|
||||
@@ -32,12 +34,24 @@ pub fn log_message(message_type: LogMessageType, message: &String) {
|
||||
message
|
||||
)
|
||||
}
|
||||
LogMessageType::Matrix => println!(
|
||||
"{} {} {}",
|
||||
current_time(),
|
||||
colored_brackets(&"MATRIX".bold().blue()),
|
||||
message.red()
|
||||
),
|
||||
LogMessageType::Error => println!(
|
||||
"{} {} {}",
|
||||
current_time(),
|
||||
colored_brackets(&"ERROR".bold().red()),
|
||||
message.red()
|
||||
),
|
||||
LogMessageType::MatrixError => println!(
|
||||
"{} {} {}",
|
||||
current_time(),
|
||||
colored_brackets(&"ERROR (Matrix)".bold().red()),
|
||||
message.red()
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
30
src/main.rs
30
src/main.rs
@@ -1,5 +1,6 @@
|
||||
mod bot;
|
||||
mod logging;
|
||||
mod matrix;
|
||||
|
||||
use azalea::pathfinder::BlockPosGoal;
|
||||
use azalea::{prelude::*, BlockPos, ClientInformation};
|
||||
@@ -34,6 +35,15 @@ struct BotConfiguration {
|
||||
cleanup_interval: u32,
|
||||
mob_expiry_time: u64,
|
||||
mob_packet_drop_level: u8,
|
||||
matrix: MatrixConfiguration,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
struct MatrixConfiguration {
|
||||
enabled: bool,
|
||||
homeserver_url: String,
|
||||
username: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
impl Default for BotConfiguration {
|
||||
@@ -55,6 +65,12 @@ impl Default for BotConfiguration {
|
||||
cleanup_interval: 300,
|
||||
mob_expiry_time: 300,
|
||||
mob_packet_drop_level: 5,
|
||||
matrix: MatrixConfiguration {
|
||||
enabled: false,
|
||||
homeserver_url: "https://matrix.example.com".to_string(),
|
||||
username: "errornowatcher".to_string(),
|
||||
password: "MyMatrixPassword".to_string(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -84,6 +100,20 @@ async fn main() {
|
||||
}
|
||||
};
|
||||
|
||||
let matrix_configuration = bot_configuration.matrix.to_owned();
|
||||
if matrix_configuration.enabled {
|
||||
match matrix::login_and_sync(
|
||||
matrix_configuration.homeserver_url,
|
||||
matrix_configuration.username,
|
||||
matrix_configuration.password,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => (),
|
||||
Err(error) => log_message(MatrixError, &format!("Unable to login: {}", error)),
|
||||
}
|
||||
}
|
||||
|
||||
loop {
|
||||
match azalea::start(azalea::Options {
|
||||
account: Account::offline(&bot_configuration.username),
|
||||
|
Reference in New Issue
Block a user