Compare commits
No commits in common. "d813198cb01385a4359cb16134efaba1e835b327" and "9ab65b8943000cd357a7e3b028d9433e12c7f78b" have entirely different histories.
d813198cb0
...
9ab65b8943
29
src/bot.rs
29
src/bot.rs
|
@ -8,11 +8,7 @@ use crate::state::DiceBotState;
|
||||||
use dirs;
|
use dirs;
|
||||||
use futures::stream::{self, StreamExt};
|
use futures::stream::{self, StreamExt};
|
||||||
use log::info;
|
use log::info;
|
||||||
use matrix_sdk::{
|
use matrix_sdk::{self, identifiers::RoomId, Client, ClientConfig, JoinedRoom, SyncSettings};
|
||||||
self,
|
|
||||||
identifiers::{EventId, RoomId},
|
|
||||||
Client, ClientConfig, JoinedRoom, SyncSettings,
|
|
||||||
};
|
|
||||||
use std::clone::Clone;
|
use std::clone::Clone;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
@ -63,10 +59,9 @@ async fn handle_single_result(
|
||||||
cmd_result: &ExecutionResult,
|
cmd_result: &ExecutionResult,
|
||||||
respond_to: &str,
|
respond_to: &str,
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
event_id: EventId,
|
|
||||||
) {
|
) {
|
||||||
let html = cmd_result.message_html(respond_to);
|
let html = cmd_result.message_html(respond_to);
|
||||||
matrix::send_message(client, room_id, &html, Some(event_id)).await;
|
matrix::send_message(client, room_id, &html).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle responding to multiple commands being executed. Will print
|
/// Handle responding to multiple commands being executed. Will print
|
||||||
|
@ -103,7 +98,7 @@ async fn handle_multiple_results(
|
||||||
.replace("\n", "<br/>")
|
.replace("\n", "<br/>")
|
||||||
};
|
};
|
||||||
|
|
||||||
matrix::send_message(client, room_id, &message, None).await;
|
matrix::send_message(client, room_id, &message).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DiceBot {
|
impl DiceBot {
|
||||||
|
@ -162,7 +157,6 @@ impl DiceBot {
|
||||||
// Initial sync without event handler prevents responding to
|
// Initial sync without event handler prevents responding to
|
||||||
// messages received while bot was offline. TODO: selectively
|
// messages received while bot was offline. TODO: selectively
|
||||||
// respond to old messages? e.g. comands missed while offline.
|
// respond to old messages? e.g. comands missed while offline.
|
||||||
info!("Performing intial sync (no commands will be responded to)");
|
|
||||||
self.client.sync_once(SyncSettings::default()).await?;
|
self.client.sync_once(SyncSettings::default()).await?;
|
||||||
|
|
||||||
client.add_event_emitter(Box::new(self)).await;
|
client.add_event_emitter(Box::new(self)).await;
|
||||||
|
@ -181,13 +175,7 @@ impl DiceBot {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn execute_commands(
|
async fn execute_commands(&self, room: &JoinedRoom, sender_username: &str, msg_body: &str) {
|
||||||
&self,
|
|
||||||
room: &JoinedRoom,
|
|
||||||
sender_username: &str,
|
|
||||||
msg_body: &str,
|
|
||||||
event_id: EventId,
|
|
||||||
) {
|
|
||||||
let room_name: &str = &room.display_name().await.ok().unwrap_or_default();
|
let room_name: &str = &room.display_name().await.ok().unwrap_or_default();
|
||||||
let room_id = room.room_id().clone();
|
let room_id = room.room_id().clone();
|
||||||
|
|
||||||
|
@ -219,14 +207,7 @@ impl DiceBot {
|
||||||
|
|
||||||
if results.len() >= 1 {
|
if results.len() >= 1 {
|
||||||
if results.len() == 1 {
|
if results.len() == 1 {
|
||||||
handle_single_result(
|
handle_single_result(&self.client, &results[0].1, sender_username, &room_id).await;
|
||||||
&self.client,
|
|
||||||
&results[0].1,
|
|
||||||
sender_username,
|
|
||||||
&room_id,
|
|
||||||
event_id,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
} else if results.len() > 1 {
|
} else if results.len() > 1 {
|
||||||
handle_multiple_results(&self.client, &results, sender_username, &room_id).await;
|
handle_multiple_results(&self.client, &results, sender_username, &room_id).await;
|
||||||
}
|
}
|
||||||
|
|
|
@ -211,7 +211,7 @@ impl EventEmitter for DiceBot {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
self.execute_commands(&room, &sender_username, &msg_body, event.event_id.clone())
|
self.execute_commands(&room, &sender_username, &msg_body)
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
use log::error;
|
use log::error;
|
||||||
use matrix_sdk::{
|
use matrix_sdk::Error as MatrixError;
|
||||||
events::room::message::{InReplyTo, Relation},
|
|
||||||
identifiers::EventId,
|
|
||||||
Error as MatrixError,
|
|
||||||
};
|
|
||||||
use matrix_sdk::{
|
use matrix_sdk::{
|
||||||
events::{
|
events::{
|
||||||
room::message::{MessageEventContent::Notice, NoticeMessageEventContent},
|
room::message::{MessageEventContent::Notice, NoticeMessageEventContent},
|
||||||
|
@ -42,20 +38,12 @@ pub async fn get_users_in_room(client: &Client, room_id: &RoomId) -> Vec<String>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send_message(
|
pub async fn send_message(client: &Client, room_id: &RoomId, message: &str) {
|
||||||
client: &Client,
|
|
||||||
room_id: &RoomId,
|
|
||||||
message: &str,
|
|
||||||
reply_to: Option<EventId>,
|
|
||||||
) {
|
|
||||||
let plain = html2text::from_read(message.as_bytes(), message.len());
|
let plain = html2text::from_read(message.as_bytes(), message.len());
|
||||||
let mut content = NoticeMessageEventContent::html(plain.trim(), message);
|
let response = RoomMessage(Notice(NoticeMessageEventContent::html(
|
||||||
|
plain.trim(),
|
||||||
content.relates_to = reply_to.map(|event_id| Relation::Reply {
|
message,
|
||||||
in_reply_to: InReplyTo { event_id },
|
)));
|
||||||
});
|
|
||||||
|
|
||||||
let response = RoomMessage(Notice(content));
|
|
||||||
|
|
||||||
let result = client.room_send(&room_id, response, None).await;
|
let result = client.room_send(&room_id, response, None).await;
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
|
|
Loading…
Reference in New Issue