Add Matrix support (part 1)

This commit is contained in:
ErrorNoInternet 2023-01-27 19:58:30 +08:00
parent 3844204a04
commit 2428f4717f
Signed by untrusted user who does not match committer: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
4 changed files with 1383 additions and 20 deletions

1355
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -18,3 +18,7 @@ strum = "0.24.1"
strum_macros = "0.24.1" strum_macros = "0.24.1"
async-recursion = "1.0.0" async-recursion = "1.0.0"
rand = "0.8.5" rand = "0.8.5"
[dependencies.matrix-sdk]
path = "../../GitHub/matrix-org/matrix-rust-sdk/crates/matrix-sdk"
version = "0.6.0"

View File

@ -4,7 +4,9 @@ use colored::*;
pub enum LogMessageType { pub enum LogMessageType {
Bot, Bot,
Chat, Chat,
Matrix,
Error, Error,
MatrixError,
} }
pub fn log_error<T, E: std::fmt::Display>(result: Result<T, E>) { 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 message
) )
} }
LogMessageType::Matrix => println!(
"{} {} {}",
current_time(),
colored_brackets(&"MATRIX".bold().blue()),
message.red()
),
LogMessageType::Error => println!( LogMessageType::Error => println!(
"{} {} {}", "{} {} {}",
current_time(), current_time(),
colored_brackets(&"ERROR".bold().red()), colored_brackets(&"ERROR".bold().red()),
message.red() message.red()
), ),
LogMessageType::MatrixError => println!(
"{} {} {}",
current_time(),
colored_brackets(&"ERROR (Matrix)".bold().red()),
message.red()
),
} }
} }

View File

@ -1,5 +1,6 @@
mod bot; mod bot;
mod logging; mod logging;
mod matrix;
use azalea::pathfinder::BlockPosGoal; use azalea::pathfinder::BlockPosGoal;
use azalea::{prelude::*, BlockPos, ClientInformation}; use azalea::{prelude::*, BlockPos, ClientInformation};
@ -34,6 +35,15 @@ struct BotConfiguration {
cleanup_interval: u32, cleanup_interval: u32,
mob_expiry_time: u64, mob_expiry_time: u64,
mob_packet_drop_level: u8, 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 { impl Default for BotConfiguration {
@ -55,6 +65,12 @@ impl Default for BotConfiguration {
cleanup_interval: 300, cleanup_interval: 300,
mob_expiry_time: 300, mob_expiry_time: 300,
mob_packet_drop_level: 5, 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 { loop {
match azalea::start(azalea::Options { match azalea::start(azalea::Options {
account: Account::offline(&bot_configuration.username), account: Account::offline(&bot_configuration.username),