113 lines
2.9 KiB
Rust
113 lines
2.9 KiB
Rust
use std::env;
|
|||
|
|
|
||
|
|
use matrix_sdk::{
|
||
|
|
Client, Room, RoomState,
|
||
|
|
config::SyncSettings,
|
||
|
|
ruma::events::room::{
|
||
|
|
member::{MembershipState, StrippedRoomMemberEvent},
|
||
|
|
message::{
|
||
|
|
AddMentions, ForwardThread, MessageType, OriginalSyncRoomMessageEvent,
|
||
|
|
RoomMessageEventContent,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
};
|
||
|
|
|
||
|
|
use crate::commands;
|
||
|
|
|
||
|
|
async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
|
||
|
|
if room.state() != RoomState::Joined {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
let MessageType::Text(text_content) = event.content.msgtype.to_owned() else {
|
||
|
|
return;
|
||
|
|
};
|
||
|
|
|
||
|
|
let body = text_content.body.trim();
|
||
|
|
if !body.starts_with('!') {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
let mut parts = body.splitn(3, ' ');
|
||
|
|
let command = parts.next().unwrap_or("");
|
||
|
|
let arg = parts.next();
|
||
|
|
|
||
|
|
let response = match command {
|
||
|
|
"!roll" => commands::roll::run_core(arg),
|
||
|
|
"!random" => commands::random::run_core(arg),
|
||
|
|
"!about" => commands::about::run_core(),
|
||
|
|
_ => return,
|
||
|
|
};
|
||
|
|
|
||
|
|
let reply = RoomMessageEventContent::text_plain(response).make_reply_to(
|
||
|
|
&event.into_full_event(room.room_id().to_owned()),
|
||
|
|
ForwardThread::No,
|
||
|
|
AddMentions::Yes,
|
||
|
|
);
|
||
|
|
|
||
|
|
match room.send(reply).await {
|
||
|
|
Ok(_) => (),
|
||
|
|
Err(e) => {
|
||
|
|
println!("Failed to send message: {e}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async fn on_stripped_member(event: StrippedRoomMemberEvent, client: Client, room: Room) {
|
||
|
|
let user_id = match client.user_id() {
|
||
|
|
Some(u) => u,
|
||
|
|
None => {
|
||
|
|
println!("Irregular Matrix ID");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
if event.state_key != user_id {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if event.content.membership != MembershipState::Invite {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if let Err(e) = room.join().await {
|
||
|
|
println!("Failed to join room: {e}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub async fn run() {
|
||
|
|
let homeserver =
|
||
|
|
env::var("MATRIX_HOMESERVER").expect("Expected MATRIX_HOMESERVER in environment");
|
||
|
|
let username = env::var("MATRIX_USERNAME").expect("Expected MATRIX_USERNAME in environment");
|
||
|
|
let password = env::var("MATRIX_PASSWORD").expect("Expected MATRIX_PASSWORD in environment");
|
||
|
|
|
||
|
|
let client = Client::builder()
|
||
|
|
.homeserver_url(&homeserver)
|
||
|
|
.build()
|
||
|
|
.await
|
||
|
|
.expect("Failed to build Matrix client");
|
||
|
|
|
||
|
|
client
|
||
|
|
.matrix_auth()
|
||
|
|
.login_username(&username, &password)
|
||
|
|
.initial_device_display_name("caitsith")
|
||
|
|
.send()
|
||
|
|
.await
|
||
|
|
.expect("Failed to log in to Matrix");
|
||
|
|
|
||
|
|
println!("Logged in to Matrix as {username}");
|
||
|
|
|
||
|
|
let sync_response = client
|
||
|
|
.sync_once(SyncSettings::default())
|
||
|
|
.await
|
||
|
|
.expect("Matrix initial sync error");
|
||
|
|
|
||
|
|
client.add_event_handler(on_room_message);
|
||
|
|
client.add_event_handler(on_stripped_member);
|
||
|
|
|
||
|
|
client
|
||
|
|
.sync(SyncSettings::default().token(sync_response.next_batch))
|
||
|
|
.await
|
||
|
|
.expect("Matrix sync error");
|
||
|
|
}
|