From dc46d7bc7a137b72e4e595e59b039f09d9abef2a Mon Sep 17 00:00:00 2001 From: milselarch Date: Fri, 22 Nov 2024 23:48:30 +0800 Subject: [PATCH] fix: clear other chat contexts --- database/callback_context_state.py | 10 +++++++++- logs/log_events.txt | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/database/callback_context_state.py b/database/callback_context_state.py index 52890942..2e3f15e4 100644 --- a/database/callback_context_state.py +++ b/database/callback_context_state.py @@ -106,11 +106,19 @@ def get_context_type(self) -> ChatContextStateTypes: def save_state(self) -> CallbackContextState: user_id = self.get_user_id() chat_id = self.get_chat_id() + context_type = self.get_context_type() with database_proxy.atomic(): + # delete other chat contexts in the same chat + CallbackContextState.delete().where( + (CallbackContextState.user == user_id) & + (CallbackContextState.chat_id == chat_id) & + (CallbackContextState.context_type != context_type) + ).execute() + # get existing chat context and update it context_state, _ = CallbackContextState.build_from_fields( user_id=user_id, chat_id=chat_id, - context_type=self.get_context_type() + context_type=context_type ).get_or_create() context_state.update_state(self) diff --git a/logs/log_events.txt b/logs/log_events.txt index d4bbfb3e..a8039d5c 100644 --- a/logs/log_events.txt +++ b/logs/log_events.txt @@ -1098,3 +1098,29 @@ task: >> 2024-11-22 21:53:04,671 - asyncio - ERROR - Task was destroyed but it is pending! task: wait_for=> +2024-11-22 23:45:57,745 - __main__ - WARNING - <<< INITIALIZING >>> +2024-11-22 23:46:46,663 - telegram.ext.Application - ERROR - No error handlers are registered, logging exception. +Traceback (most recent call last): + File "/home/milselarch/projects/SUTD/voting-bot/venv/lib/python3.12/site-packages/telegram/ext/_application.py", line 1335, in process_update + await coroutine + File "/home/milselarch/projects/SUTD/voting-bot/venv/lib/python3.12/site-packages/telegram/ext/_handlers/basehandler.py", line 158, in handle_update + return await self.callback(update, context) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/home/milselarch/projects/SUTD/voting-bot/tele_helpers.py", line 190, in caller + return await func(modified_tele_update, *args, **kwargs) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/home/milselarch/projects/SUTD/voting-bot/bot.py", line 378, in create_group_poll + return await self.create_poll( + ^^^^^^^^^^^^^^^^^^^^^^^ + File "/home/milselarch/projects/SUTD/voting-bot/bot.py", line 430, in create_poll + ).save_state() + ^^^^^^^^^^^^ + File "/home/milselarch/projects/SUTD/voting-bot/database/callback_context_state.py", line 115, in save_state + {CallbackContextState.user == user_id} & + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +TypeError: unhashable type: 'Expression' +2024-11-22 23:47:06,551 - asyncio - ERROR - Task was destroyed but it is pending! +task: wait_for=> +2024-11-22 23:47:07,783 - __main__ - WARNING - <<< INITIALIZING >>> +2024-11-22 23:48:04,976 - asyncio - ERROR - Task was destroyed but it is pending! +task: wait_for=>