From bd7d775990a66748730ff72d47dfcfb87c1bbe16 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Thu, 30 Mar 2023 04:37:53 +0200 Subject: [PATCH] replace command --- matrix_pygmalion_bot/core.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/matrix_pygmalion_bot/core.py b/matrix_pygmalion_bot/core.py index 77dacee..e976c84 100644 --- a/matrix_pygmalion_bot/core.py +++ b/matrix_pygmalion_bot/core.py @@ -95,7 +95,8 @@ class Callbacks(object): await self.bot.send_message(self.client, room.room_id, self.bot.greeting) return elif event.body.startswith('!!!'): - return + if len(self.bot.chat_history) < 3: + return chat_history_event_id, chat_history_item = self.bot.chat_history.popitem() # current await self.client.room_redact(room.room_id, chat_history_item.event_id, reason="user-request") chat_history_event_id, chat_history_item = self.bot.chat_history.popitem() @@ -116,6 +117,21 @@ class Callbacks(object): message = chat_history_item.message translated_message = message # don't return, we generate a new answer + elif event.body.startswith('!replace'): + if len(self.bot.chat_history) < 3: + return + chat_history_event_id, chat_history_item = self.bot.chat_history.popitem() # current + await self.client.room_redact(room.room_id, chat_history_item.event_id, reason="user-request") + chat_history_event_id, chat_history_item = self.bot.chat_history.popitem() + await self.client.room_redact(room.room_id, chat_history_item.event_id, reason="user-request") + new_answer = event.body.removeprefix('!replace').strip() + new_translated_answer = new_answer + if not (self.bot.translate is None): + new_translated_answer = translate.translate(new_answer, "en", self.bot.translate) + await self.bot.send_message(self.client, room.room_id, new_translated_answer, reply_to=chat_history_item.relates_to_event, original_message=new_answer) + else: + await self.bot.send_message(self.client, room.room_id, new_answer, reply_to=chat_history_item.relates_to_event) + return # Other commands if re.search("^(?=.*\bsend\b)(?=.*\bpicture\b).*$", message):