forked from projectmoon/tenebrous-dicebot
Add matrix client to context.
This commit is contained in:
parent
f352c90b6b
commit
f46b914239
|
@ -12,7 +12,15 @@ async fn main() -> Result<(), BotError> {
|
|||
Err(e) => return Err(e),
|
||||
};
|
||||
|
||||
let context = Context::new(&db, "roomid", "localuser", &input);
|
||||
let context = Context {
|
||||
db: db,
|
||||
matrix_client: &matrix_sdk::Client::new("http://example.com")
|
||||
.expect("Could not create matrix client"),
|
||||
room_id: "roomid",
|
||||
username: "@localuser:example.com",
|
||||
message_body: &input,
|
||||
};
|
||||
|
||||
println!("{}", command.execute(&context).await.plain());
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -127,7 +127,14 @@ impl DiceBot {
|
|||
let commands = msg_body.trim().lines().filter(|line| line.starts_with("!"));
|
||||
|
||||
for command in commands {
|
||||
let ctx = Context::new(&self.db, &room_id.as_str(), &sender_username, &command);
|
||||
let ctx = Context {
|
||||
db: self.db.clone(),
|
||||
matrix_client: &self.client,
|
||||
room_id: room_id.as_str(),
|
||||
username: &sender_username,
|
||||
message_body: &command,
|
||||
};
|
||||
|
||||
let cmd_result = execute_command(&ctx).await;
|
||||
results.push(cmd_result);
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use super::DiceBot;
|
||||
use crate::db::Database;
|
||||
use crate::error::BotError;
|
||||
use async_trait::async_trait;
|
||||
|
@ -12,8 +13,6 @@ use matrix_sdk::{
|
|||
identifiers::RoomId,
|
||||
Client, EventEmitter, SyncRoom,
|
||||
};
|
||||
//use matrix_sdk_common_macros::async_trait;
|
||||
use super::DiceBot;
|
||||
use std::clone::Clone;
|
||||
use std::ops::Sub;
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
|
|
@ -461,7 +461,13 @@ mod tests {
|
|||
#[tokio::test]
|
||||
async fn rejects_large_expression_test() {
|
||||
let db = Database::new_temp().unwrap();
|
||||
let ctx = Context::new(&db, "roomid", "username", "message");
|
||||
let ctx = Context {
|
||||
db: db,
|
||||
matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(),
|
||||
room_id: "roomid",
|
||||
username: "username",
|
||||
message_body: "message",
|
||||
};
|
||||
|
||||
let mut amounts = vec![];
|
||||
|
||||
|
@ -486,7 +492,13 @@ mod tests {
|
|||
#[tokio::test]
|
||||
async fn converts_to_chance_die_test() {
|
||||
let db = Database::new_temp().unwrap();
|
||||
let ctx = Context::new(&db, "roomid", "username", "message");
|
||||
let ctx = Context {
|
||||
db: db,
|
||||
matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(),
|
||||
room_id: "roomid",
|
||||
username: "username",
|
||||
message_body: "message",
|
||||
};
|
||||
|
||||
let mut amounts = vec![];
|
||||
|
||||
|
@ -508,7 +520,13 @@ mod tests {
|
|||
#[tokio::test]
|
||||
async fn can_resolve_variables_test() {
|
||||
let db = Database::new_temp().unwrap();
|
||||
let ctx = Context::new(&db, "roomid", "username", "message");
|
||||
let ctx = Context {
|
||||
db: db.clone(),
|
||||
matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(),
|
||||
room_id: "roomid",
|
||||
username: "username",
|
||||
message_body: "message",
|
||||
};
|
||||
let user_and_room = crate::db::variables::UserAndRoom(&ctx.username, &ctx.room_id);
|
||||
|
||||
db.variables
|
||||
|
|
|
@ -80,7 +80,13 @@ mod tests {
|
|||
#[tokio::test]
|
||||
async fn unrecognized_command() {
|
||||
let db = crate::db::Database::new_temp().unwrap();
|
||||
let ctx = Context::new(&db, "myroomid", "@testuser:example.com", "!notacommand");
|
||||
let ctx = Context {
|
||||
db: db,
|
||||
matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(),
|
||||
room_id: "myroomid",
|
||||
username: "myusername",
|
||||
message_body: "!notacommand",
|
||||
};
|
||||
let result = execute_command(&ctx).await;
|
||||
assert!(result.plain.contains("Error"));
|
||||
}
|
||||
|
|
|
@ -1,27 +1,13 @@
|
|||
use crate::db::Database;
|
||||
use matrix_sdk::Client;
|
||||
|
||||
/// A context carried through the system providing access to things
|
||||
/// like the database.
|
||||
#[derive(Clone)]
|
||||
pub struct Context<'a> {
|
||||
pub db: Database,
|
||||
pub matrix_client: &'a Client,
|
||||
pub room_id: &'a str,
|
||||
pub username: &'a str,
|
||||
pub message_body: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> Context<'a> {
|
||||
pub fn new(
|
||||
db: &Database,
|
||||
room_id: &'a str,
|
||||
username: &'a str,
|
||||
message_body: &'a str,
|
||||
) -> Context<'a> {
|
||||
Context {
|
||||
db: db.clone(),
|
||||
room_id: room_id,
|
||||
username: username,
|
||||
message_body: message_body,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -380,7 +380,14 @@ mod tests {
|
|||
};
|
||||
|
||||
let db = Database::new_temp().unwrap();
|
||||
let ctx = Context::new(&db, "roomid", "username", "message");
|
||||
let ctx = Context {
|
||||
db: db,
|
||||
matrix_client: &matrix_sdk::Client::new("https://example.com").unwrap(),
|
||||
room_id: "roomid",
|
||||
username: "username",
|
||||
message_body: "message",
|
||||
};
|
||||
|
||||
let roll_with_ctx = DiceRollWithContext(&roll, &ctx);
|
||||
let result = regular_roll(&roll_with_ctx).await;
|
||||
assert!(result.is_err());
|
||||
|
|
Loading…
Reference in New Issue