forked from projectmoon/tenebrous-dicebot
Reply with executed command as quote (single commands only).
This commit is contained in:
parent
693167a581
commit
d813198cb0
28
src/bot.rs
28
src/bot.rs
|
@ -8,7 +8,11 @@ use crate::state::DiceBotState;
|
|||
use dirs;
|
||||
use futures::stream::{self, StreamExt};
|
||||
use log::info;
|
||||
use matrix_sdk::{self, identifiers::RoomId, Client, ClientConfig, JoinedRoom, SyncSettings};
|
||||
use matrix_sdk::{
|
||||
self,
|
||||
identifiers::{EventId, RoomId},
|
||||
Client, ClientConfig, JoinedRoom, SyncSettings,
|
||||
};
|
||||
use std::clone::Clone;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
@ -59,9 +63,10 @@ async fn handle_single_result(
|
|||
cmd_result: &ExecutionResult,
|
||||
respond_to: &str,
|
||||
room_id: &RoomId,
|
||||
event_id: EventId,
|
||||
) {
|
||||
let html = cmd_result.message_html(respond_to);
|
||||
matrix::send_message(client, room_id, &html).await;
|
||||
matrix::send_message(client, room_id, &html, Some(event_id)).await;
|
||||
}
|
||||
|
||||
/// Handle responding to multiple commands being executed. Will print
|
||||
|
@ -98,7 +103,7 @@ async fn handle_multiple_results(
|
|||
.replace("\n", "<br/>")
|
||||
};
|
||||
|
||||
matrix::send_message(client, room_id, &message).await;
|
||||
matrix::send_message(client, room_id, &message, None).await;
|
||||
}
|
||||
|
||||
impl DiceBot {
|
||||
|
@ -176,7 +181,13 @@ impl DiceBot {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn execute_commands(&self, room: &JoinedRoom, sender_username: &str, msg_body: &str) {
|
||||
async fn execute_commands(
|
||||
&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_id = room.room_id().clone();
|
||||
|
||||
|
@ -208,7 +219,14 @@ impl DiceBot {
|
|||
|
||||
if results.len() >= 1 {
|
||||
if results.len() == 1 {
|
||||
handle_single_result(&self.client, &results[0].1, sender_username, &room_id).await;
|
||||
handle_single_result(
|
||||
&self.client,
|
||||
&results[0].1,
|
||||
sender_username,
|
||||
&room_id,
|
||||
event_id,
|
||||
)
|
||||
.await;
|
||||
} else if results.len() > 1 {
|
||||
handle_multiple_results(&self.client, &results, sender_username, &room_id).await;
|
||||
}
|
||||
|
|
|
@ -211,7 +211,7 @@ impl EventEmitter for DiceBot {
|
|||
return;
|
||||
};
|
||||
|
||||
self.execute_commands(&room, &sender_username, &msg_body)
|
||||
self.execute_commands(&room, &sender_username, &msg_body, event.event_id.clone())
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
use log::error;
|
||||
use matrix_sdk::Error as MatrixError;
|
||||
use matrix_sdk::{
|
||||
events::room::message::{InReplyTo, Relation},
|
||||
identifiers::EventId,
|
||||
Error as MatrixError,
|
||||
};
|
||||
use matrix_sdk::{
|
||||
events::{
|
||||
room::message::{MessageEventContent::Notice, NoticeMessageEventContent},
|
||||
|
@ -38,12 +42,20 @@ pub async fn get_users_in_room(client: &Client, room_id: &RoomId) -> Vec<String>
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn send_message(client: &Client, room_id: &RoomId, message: &str) {
|
||||
pub async fn send_message(
|
||||
client: &Client,
|
||||
room_id: &RoomId,
|
||||
message: &str,
|
||||
reply_to: Option<EventId>,
|
||||
) {
|
||||
let plain = html2text::from_read(message.as_bytes(), message.len());
|
||||
let response = RoomMessage(Notice(NoticeMessageEventContent::html(
|
||||
plain.trim(),
|
||||
message,
|
||||
)));
|
||||
let mut content = NoticeMessageEventContent::html(plain.trim(), message);
|
||||
|
||||
content.relates_to = reply_to.map(|event_id| Relation::Reply {
|
||||
in_reply_to: InReplyTo { event_id },
|
||||
});
|
||||
|
||||
let response = RoomMessage(Notice(content));
|
||||
|
||||
let result = client.room_send(&room_id, response, None).await;
|
||||
if let Err(e) = result {
|
||||
|
|
Loading…
Reference in New Issue