forked from projectmoon/tenebrous-dicebot
Upgrade to matrix SDK 0.4.
This commit is contained in:
parent
ee3ec18e06
commit
6f09a11586
File diff suppressed because it is too large
Load Diff
|
@ -32,7 +32,7 @@ combine = "4.5"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
html2text = "0.2"
|
html2text = "0.2"
|
||||||
phf = { version = "0.8", features = ["macros"] }
|
phf = { version = "0.8", features = ["macros"] }
|
||||||
matrix-sdk = { version = "0.3" }
|
matrix-sdk = { version = "0.4.1" }
|
||||||
refinery = { version = "0.6", features = ["rusqlite"]}
|
refinery = { version = "0.6", features = ["rusqlite"]}
|
||||||
barrel = { version = "0.6", features = ["sqlite3"] }
|
barrel = { version = "0.6", features = ["sqlite3"] }
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use matrix_sdk::identifiers::room_id;
|
use matrix_sdk::ruma::identifiers::room_id;
|
||||||
use matrix_sdk::Client;
|
use matrix_sdk::Client;
|
||||||
use tenebrous_dicebot::commands;
|
use tenebrous_dicebot::commands;
|
||||||
use tenebrous_dicebot::commands::ResponseExtractor;
|
use tenebrous_dicebot::commands::ResponseExtractor;
|
||||||
|
|
|
@ -8,12 +8,8 @@ use crate::{
|
||||||
models::Account,
|
models::Account,
|
||||||
};
|
};
|
||||||
use futures::stream::{self, StreamExt};
|
use futures::stream::{self, StreamExt};
|
||||||
use matrix_sdk::{
|
use matrix_sdk::ruma::{EventId, RoomId};
|
||||||
self,
|
use matrix_sdk::{self, room::Joined, Client};
|
||||||
identifiers::{EventId, RoomId},
|
|
||||||
room::Joined,
|
|
||||||
Client,
|
|
||||||
};
|
|
||||||
use std::clone::Clone;
|
use std::clone::Clone;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
|
|
|
@ -2,18 +2,12 @@ use super::DiceBot;
|
||||||
use crate::db::sqlite::Database;
|
use crate::db::sqlite::Database;
|
||||||
use crate::db::Rooms;
|
use crate::db::Rooms;
|
||||||
use crate::error::BotError;
|
use crate::error::BotError;
|
||||||
use async_trait::async_trait;
|
|
||||||
use log::{debug, error, info, warn};
|
use log::{debug, error, info, warn};
|
||||||
use matrix_sdk::{
|
use matrix_sdk::ruma::events::room::member::MemberEventContent;
|
||||||
self,
|
use matrix_sdk::ruma::events::room::message::{MessageType, TextMessageEventContent};
|
||||||
events::{
|
use matrix_sdk::ruma::events::{StrippedStateEvent, SyncMessageEvent};
|
||||||
room::member::MemberEventContent,
|
use matrix_sdk::Client;
|
||||||
room::message::{MessageEventContent, MessageType, TextMessageEventContent},
|
use matrix_sdk::{self, room::Room, ruma::events::room::message::MessageEventContent};
|
||||||
StrippedStateEvent, SyncMessageEvent,
|
|
||||||
},
|
|
||||||
room::Room,
|
|
||||||
EventHandler,
|
|
||||||
};
|
|
||||||
use std::ops::Sub;
|
use std::ops::Sub;
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
use std::{clone::Clone, time::UNIX_EPOCH};
|
use std::{clone::Clone, time::UNIX_EPOCH};
|
||||||
|
@ -101,15 +95,10 @@ async fn should_process_event(db: &Database, room_id: &str, event_id: &str) -> b
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This event emitter listens for messages with dice rolling commands.
|
pub(super) async fn on_stripped_state_member(
|
||||||
/// Originally adapted from the matrix-rust-sdk examples.
|
event: StrippedStateEvent<MemberEventContent>,
|
||||||
#[async_trait]
|
client: Client,
|
||||||
impl EventHandler for DiceBot {
|
|
||||||
async fn on_stripped_state_member(
|
|
||||||
&self,
|
|
||||||
room: Room,
|
room: Room,
|
||||||
event: &StrippedStateEvent<MemberEventContent>,
|
|
||||||
_: Option<MemberEventContent>,
|
|
||||||
) {
|
) {
|
||||||
let room = match room {
|
let room = match room {
|
||||||
Room::Invited(invited_room) => invited_room,
|
Room::Invited(invited_room) => invited_room,
|
||||||
|
@ -125,34 +114,37 @@ impl EventHandler for DiceBot {
|
||||||
room.display_name().await.ok().unwrap_or_default()
|
room.display_name().await.ok().unwrap_or_default()
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Err(e) = self.client.join_room_by_id(&room.room_id()).await {
|
if let Err(e) = client.join_room_by_id(&room.room_id()).await {
|
||||||
warn!("Could not join room: {}", e.to_string())
|
warn!("Could not join room: {}", e.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn on_room_message(&self, room: Room, event: &SyncMessageEvent<MessageEventContent>) {
|
pub(super) async fn on_room_message(
|
||||||
|
event: SyncMessageEvent<MessageEventContent>,
|
||||||
|
room: Room,
|
||||||
|
bot: DiceBot,
|
||||||
|
) {
|
||||||
let room = match room {
|
let room = match room {
|
||||||
Room::Joined(joined_room) => joined_room,
|
Room::Joined(joined_room) => joined_room,
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
let room_id = room.room_id().as_str();
|
let room_id = room.room_id().as_str();
|
||||||
if !should_process_event(&self.db, room_id, event.event_id.as_str()).await {
|
if !should_process_event(&bot.db, room_id, event.event_id.as_str()).await {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (msg_body, sender_username) =
|
let (msg_body, sender_username) =
|
||||||
if let Ok((msg_body, sender_username)) = should_process_message(self, &event).await {
|
if let Ok((msg_body, sender_username)) = should_process_message(&bot, &event).await {
|
||||||
(msg_body, sender_username)
|
(msg_body, sender_username)
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let results = self
|
let results = bot
|
||||||
.execute_commands(&room, &sender_username, &msg_body)
|
.execute_commands(&room, &sender_username, &msg_body)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
self.handle_results(&room, &sender_username, event.event_id.clone(), results)
|
bot.handle_results(&room, &sender_username, event.event_id.clone(), results)
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -5,7 +5,11 @@ use crate::db::DbState;
|
||||||
use crate::error::BotError;
|
use crate::error::BotError;
|
||||||
use crate::state::DiceBotState;
|
use crate::state::DiceBotState;
|
||||||
use log::info;
|
use log::info;
|
||||||
use matrix_sdk::{self, identifiers::EventId, room::Joined, Client, SyncSettings};
|
use matrix_sdk::room::Room;
|
||||||
|
use matrix_sdk::ruma::events::room::message::MessageEventContent;
|
||||||
|
use matrix_sdk::ruma::events::SyncMessageEvent;
|
||||||
|
use matrix_sdk::ruma::EventId;
|
||||||
|
use matrix_sdk::{self, room::Joined, Client, SyncSettings};
|
||||||
use std::clone::Clone;
|
use std::clone::Clone;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
|
@ -18,6 +22,7 @@ const MAX_COMMANDS_PER_MESSAGE: usize = 50;
|
||||||
|
|
||||||
/// The DiceBot struct represents an active dice bot. The bot is not
|
/// The DiceBot struct represents an active dice bot. The bot is not
|
||||||
/// connected to Matrix until its run() function is called.
|
/// connected to Matrix until its run() function is called.
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct DiceBot {
|
pub struct DiceBot {
|
||||||
/// A reference to the configuration read in on application start.
|
/// A reference to the configuration read in on application start.
|
||||||
config: Arc<Config>,
|
config: Arc<Config>,
|
||||||
|
@ -79,14 +84,32 @@ impl DiceBot {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn bind_events(&self) {
|
||||||
|
//on room message: need closure to pass bot ref in.
|
||||||
|
self.client
|
||||||
|
.register_event_handler({
|
||||||
|
let bot: DiceBot = self.clone();
|
||||||
|
move |event: SyncMessageEvent<MessageEventContent>, room: Room| {
|
||||||
|
let bot = bot.clone();
|
||||||
|
async move { event_handlers::on_room_message(event, room, bot).await }
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
//auto-join handler
|
||||||
|
self.client
|
||||||
|
.register_event_handler(event_handlers::on_stripped_state_member)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
/// Logs the bot in to Matrix and listens for events until program
|
/// Logs the bot in to Matrix and listens for events until program
|
||||||
/// terminated, or a panic occurs. Originally adapted from the
|
/// terminated, or a panic occurs. Originally adapted from the
|
||||||
/// matrix-rust-sdk command bot example.
|
/// matrix-rust-sdk command bot example.
|
||||||
pub async fn run(self) -> Result<(), BotError> {
|
pub async fn run(self) -> Result<(), BotError> {
|
||||||
let client = self.client.clone();
|
let client = self.client.clone();
|
||||||
self.login(&client).await?;
|
self.login(&client).await?;
|
||||||
|
self.bind_events().await;
|
||||||
|
|
||||||
client.set_event_handler(Box::new(self)).await;
|
|
||||||
info!("Listening for commands");
|
info!("Listening for commands");
|
||||||
|
|
||||||
// TODO replace with sync_with_callback for cleaner shutdown
|
// TODO replace with sync_with_callback for cleaner shutdown
|
||||||
|
|
|
@ -332,7 +332,7 @@ mod tests {
|
||||||
macro_rules! dummy_room {
|
macro_rules! dummy_room {
|
||||||
() => {
|
() => {
|
||||||
crate::context::RoomContext {
|
crate::context::RoomContext {
|
||||||
id: &matrix_sdk::identifiers::room_id!("!fakeroomid:example.com"),
|
id: &matrix_sdk::ruma::identifiers::room_id!("!fakeroomid:example.com"),
|
||||||
display_name: "displayname".to_owned(),
|
display_name: "displayname".to_owned(),
|
||||||
secure: false,
|
secure: false,
|
||||||
}
|
}
|
||||||
|
|
|
@ -166,7 +166,7 @@ mod tests {
|
||||||
macro_rules! dummy_room {
|
macro_rules! dummy_room {
|
||||||
() => {
|
() => {
|
||||||
crate::context::RoomContext {
|
crate::context::RoomContext {
|
||||||
id: &matrix_sdk::identifiers::room_id!("!fakeroomid:example.com"),
|
id: &matrix_sdk::ruma::identifiers::room_id!("!fakeroomid:example.com"),
|
||||||
display_name: "displayname".to_owned(),
|
display_name: "displayname".to_owned(),
|
||||||
secure: false,
|
secure: false,
|
||||||
}
|
}
|
||||||
|
@ -176,7 +176,7 @@ mod tests {
|
||||||
macro_rules! secure_room {
|
macro_rules! secure_room {
|
||||||
() => {
|
() => {
|
||||||
crate::context::RoomContext {
|
crate::context::RoomContext {
|
||||||
id: &matrix_sdk::identifiers::room_id!("!fakeroomid:example.com"),
|
id: &matrix_sdk::ruma::identifiers::room_id!("!fakeroomid:example.com"),
|
||||||
display_name: "displayname".to_owned(),
|
display_name: "displayname".to_owned(),
|
||||||
secure: true,
|
secure: true,
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ use crate::matrix;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use fuse_rust::{Fuse, FuseProperty, Fuseable};
|
use fuse_rust::{Fuse, FuseProperty, Fuseable};
|
||||||
use futures::stream::{self, StreamExt, TryStreamExt};
|
use futures::stream::{self, StreamExt, TryStreamExt};
|
||||||
use matrix_sdk::{identifiers::UserId, Client};
|
use matrix_sdk::{ruma::UserId, Client};
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
/// Holds matrix room ID and display name as strings, for use with
|
/// Holds matrix room ID and display name as strings, for use with
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
use crate::db::sqlite::Database;
|
use crate::db::sqlite::Database;
|
||||||
use crate::error::BotError;
|
use crate::error::BotError;
|
||||||
use crate::models::Account;
|
use crate::models::Account;
|
||||||
use matrix_sdk::identifiers::{RoomId, UserId};
|
|
||||||
use matrix_sdk::room::Joined;
|
use matrix_sdk::room::Joined;
|
||||||
|
use matrix_sdk::ruma::identifiers::{RoomId, UserId};
|
||||||
use matrix_sdk::Client;
|
use matrix_sdk::Client;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
|
|
|
@ -431,7 +431,7 @@ mod tests {
|
||||||
macro_rules! dummy_room {
|
macro_rules! dummy_room {
|
||||||
() => {
|
() => {
|
||||||
crate::context::RoomContext {
|
crate::context::RoomContext {
|
||||||
id: &matrix_sdk::identifiers::room_id!("!fakeroomid:example.com"),
|
id: &matrix_sdk::ruma::identifiers::room_id!("!fakeroomid:example.com"),
|
||||||
display_name: "displayname".to_owned(),
|
display_name: "displayname".to_owned(),
|
||||||
secure: false,
|
secure: false,
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,9 +39,6 @@ pub enum BotError {
|
||||||
#[error("uncategorized matrix SDK error: {0}")]
|
#[error("uncategorized matrix SDK error: {0}")]
|
||||||
MatrixError(#[from] matrix_sdk::Error),
|
MatrixError(#[from] matrix_sdk::Error),
|
||||||
|
|
||||||
#[error("uncategorized matrix SDK base error: {0}")]
|
|
||||||
MatrixBaseError(#[from] matrix_sdk::BaseError),
|
|
||||||
|
|
||||||
#[error("future canceled")]
|
#[error("future canceled")]
|
||||||
FutureCanceledError,
|
FutureCanceledError,
|
||||||
|
|
||||||
|
@ -80,7 +77,7 @@ pub enum BotError {
|
||||||
TryFromIntError(#[from] std::num::TryFromIntError),
|
TryFromIntError(#[from] std::num::TryFromIntError),
|
||||||
|
|
||||||
#[error("identifier error: {0}")]
|
#[error("identifier error: {0}")]
|
||||||
IdentifierError(#[from] matrix_sdk::identifiers::Error),
|
IdentifierError(#[from] matrix_sdk::ruma::identifiers::Error),
|
||||||
|
|
||||||
#[error("password creation error: {0}")]
|
#[error("password creation error: {0}")]
|
||||||
PasswordCreationError(argon2::Error),
|
PasswordCreationError(argon2::Error),
|
||||||
|
|
|
@ -2,15 +2,12 @@ use std::path::PathBuf;
|
||||||
|
|
||||||
use futures::stream::{self, StreamExt, TryStreamExt};
|
use futures::stream::{self, StreamExt, TryStreamExt};
|
||||||
use log::error;
|
use log::error;
|
||||||
use matrix_sdk::{events::room::message::NoticeMessageEventContent, room::Joined, ClientConfig};
|
use matrix_sdk::ruma::events::room::message::{InReplyTo, MessageEventContent, Relation};
|
||||||
use matrix_sdk::{
|
use matrix_sdk::ruma::events::AnyMessageEventContent;
|
||||||
events::room::message::{InReplyTo, Relation},
|
use matrix_sdk::ruma::{EventId, RoomId, UserId};
|
||||||
events::room::message::{MessageEventContent, MessageType},
|
use matrix_sdk::Client;
|
||||||
events::AnyMessageEventContent,
|
use matrix_sdk::Error as MatrixError;
|
||||||
identifiers::EventId,
|
use matrix_sdk::{room::Joined, ClientConfig};
|
||||||
Error as MatrixError,
|
|
||||||
};
|
|
||||||
use matrix_sdk::{identifiers::RoomId, identifiers::UserId, Client};
|
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use crate::{config::Config, error::BotError};
|
use crate::{config::Config, error::BotError};
|
||||||
|
@ -95,9 +92,7 @@ pub async fn send_message(
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut content = MessageEventContent::new(MessageType::Notice(
|
let mut content = MessageEventContent::notice_html(plain.trim(), html);
|
||||||
NoticeMessageEventContent::html(plain.trim(), html),
|
|
||||||
));
|
|
||||||
|
|
||||||
content.relates_to = reply_to.map(|event_id| Relation::Reply {
|
content.relates_to = reply_to.map(|event_id| Relation::Reply {
|
||||||
in_reply_to: InReplyTo::new(event_id),
|
in_reply_to: InReplyTo::new(event_id),
|
||||||
|
|
|
@ -4,7 +4,8 @@ use crate::matrix;
|
||||||
use crate::{config::Config, db::sqlite::Database};
|
use crate::{config::Config, db::sqlite::Database};
|
||||||
use futures::stream;
|
use futures::stream;
|
||||||
use futures::{StreamExt, TryFutureExt, TryStreamExt};
|
use futures::{StreamExt, TryFutureExt, TryStreamExt};
|
||||||
use matrix_sdk::{identifiers::UserId, room::Joined, Client};
|
use matrix_sdk::ruma::UserId;
|
||||||
|
use matrix_sdk::{room::Joined, Client};
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tenebrous_rpc::protos::dicebot::{
|
use tenebrous_rpc::protos::dicebot::{
|
||||||
|
|
Loading…
Reference in New Issue