command_bot: remove timestamp

master
Devin R 2020-04-15 08:44:29 -04:00
parent c495a50c52
commit 63368a9437
1 changed files with 5 additions and 15 deletions

View File

@ -1,8 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{env, process::exit}; use std::{env, process::exit};
use js_int::UInt;
use matrix_sdk::{ use matrix_sdk::{
self, self,
events::room::message::{MessageEvent, MessageEventContent, TextMessageEventContent}, events::room::message::{MessageEvent, MessageEventContent, TextMessageEventContent},
@ -15,36 +13,28 @@ struct CommandBot {
/// This clone of the `AsyncClient` will send requests to the server, /// This clone of the `AsyncClient` will send requests to the server,
/// while the other keeps us in sync with the server using `sync_forever`. /// while the other keeps us in sync with the server using `sync_forever`.
client: AsyncClient, client: AsyncClient,
/// A timestamp so we only respond to messages sent after the bot is running.
start_time: UInt,
} }
impl CommandBot { impl CommandBot {
pub fn new(client: AsyncClient) -> Self { pub fn new(client: AsyncClient) -> Self {
let now = SystemTime::now(); Self { client }
let timestamp = now.duration_since(UNIX_EPOCH).unwrap().as_millis();
Self {
client,
start_time: UInt::new(timestamp as u64).unwrap(),
}
} }
} }
#[async_trait::async_trait] #[async_trait::async_trait]
impl EventEmitter for CommandBot { impl EventEmitter for CommandBot {
async fn on_room_message(&self, room: Arc<RwLock<Room>>, event: &MessageEvent) { async fn on_room_message(&self, room: Arc<RwLock<Room>>, event: &MessageEvent) {
let (msg_body, timestamp) = if let MessageEvent { let msg_body = if let MessageEvent {
content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }), content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),
origin_server_ts,
.. ..
} = event } = event
{ {
(msg_body.clone(), *origin_server_ts) msg_body.clone()
} else { } else {
(String::new(), UInt::min_value()) String::new()
}; };
if msg_body.contains("!party") && timestamp > self.start_time { if msg_body.contains("!party") {
let content = MessageEventContent::Text(TextMessageEventContent { let content = MessageEventContent::Text(TextMessageEventContent {
body: "🎉🎊🥳 let's PARTY!! 🥳🎊🎉".to_string(), body: "🎉🎊🥳 let's PARTY!! 🥳🎊🎉".to_string(),
format: None, format: None,