Add auto-join, adapted from matrix SDK examples.

This commit is contained in:
projectmoon 2020-08-27 23:50:32 +00:00
parent 12fdc2a1c9
commit 3ef8a58237
1 changed files with 26 additions and 2 deletions

View File

@ -3,8 +3,9 @@ use dirs;
use matrix_sdk::{ use matrix_sdk::{
self, self,
events::{ events::{
room::member::MemberEventContent,
room::message::{MessageEventContent, NoticeMessageEventContent, TextMessageEventContent}, room::message::{MessageEventContent, NoticeMessageEventContent, TextMessageEventContent},
AnyMessageEventContent, SyncMessageEvent, AnyMessageEventContent, StrippedStateEvent, SyncMessageEvent,
}, },
Client, ClientConfig, EventEmitter, JsonStore, SyncRoom, SyncSettings, Client, ClientConfig, EventEmitter, JsonStore, SyncRoom, SyncSettings,
}; };
@ -48,9 +49,32 @@ impl DiceBot {
} }
/// This event emitter listens for messages with dice rolling commands. /// This event emitter listens for messages with dice rolling commands.
/// Originally adapted from the matrix-rust-sdk command bot example. /// Originally adapted from the matrix-rust-sdk examples.
#[async_trait] #[async_trait]
impl EventEmitter for DiceBot { impl EventEmitter for DiceBot {
async fn on_stripped_state_member(
&self,
room: SyncRoom,
room_member: &StrippedStateEvent<MemberEventContent>,
_: Option<MemberEventContent>,
) {
if let SyncRoom::Invited(room) = room {
if let Some(user_id) = self.client.user_id().await {
if room_member.state_key != user_id {
return;
}
}
let room = room.read().await;
println!("Autojoining room {}", room.display_name());
match self.client.join_room_by_id(&room.room_id).await {
Err(e) => println!("Could not join room: {}", e.to_string()),
_ => (),
}
}
}
async fn on_room_message(&self, room: SyncRoom, event: &SyncMessageEvent<MessageEventContent>) { async fn on_room_message(&self, room: SyncRoom, event: &SyncMessageEvent<MessageEventContent>) {
if let SyncRoom::Joined(room) = room { if let SyncRoom::Joined(room) = room {
let (msg_body, sender_username) = if let SyncMessageEvent { let (msg_body, sender_username) = if let SyncMessageEvent {