diff --git a/internal/chat_thesaurus.py b/internal/chat_thesaurus.py index 87b35d0..4ab31a6 100644 --- a/internal/chat_thesaurus.py +++ b/internal/chat_thesaurus.py @@ -48,25 +48,25 @@ async def chat_thesaurus(messages, ws=None): try: # 查询开关 bot_switch = Database().db_handler.bot_switch(messages['group_id']) - if bot_switch is None or len(bot_switch) == 0: + if bot_switch is None: if arg[0] == '/on' and is_admin: Database().db_handler.bot_switch(messages['group_id'], 1) text = "Bot started successfully." return text except NameError: - bot_switch = '0' + bot_switch = False if config['debug']: import traceback traceback.print_exc() pass - if bot_switch == '0': + if bot_switch is False: if arg[0] == '/on' and is_admin: Database().db_handler.bot_switch(messages['group_id'], 1) text = "Bot started successfully." else: text = None return text - elif bot_switch == '1': + elif bot_switch is True: if arg[0] == '/on' and is_admin: text = "Bot is running." elif arg[0] == '/off' and is_admin: @@ -224,7 +224,6 @@ async def chat_thesaurus(messages, ws=None): text = None else: text = None - print_pink(f"text内容:{text}") return text @@ -279,6 +278,8 @@ async def while_msg(ws): for message in text: await send_message(ws, messages, message, False) except Exception: - print(f"Error: {Exception}") - import traceback - traceback.print_exc() \ No newline at end of file + if config['debug']: + import traceback + traceback.print_exc() + else: + pass diff --git a/internal/database/postgresql_handler.py b/internal/database/postgresql_handler.py index e7ced31..bc9d3d2 100644 --- a/internal/database/postgresql_handler.py +++ b/internal/database/postgresql_handler.py @@ -2,6 +2,8 @@ import json import traceback from datetime import datetime +from xmlrpc.client import Boolean + import psycopg2 from psycopg2 import pool from psycopg2.extras import RealDictCursor @@ -53,25 +55,32 @@ def bot_switch(self, group_id='0', switch=None): cursor = conn.cursor(cursor_factory=RealDictCursor) if switch is not None: - # Insert SQL record + # Insert or update SQL record date = datetime.now() cursor.execute( "INSERT INTO switch (group_id, switch, time) VALUES (%s, %s, %s) ON CONFLICT (group_id) DO UPDATE SET switch = EXCLUDED.switch, time = EXCLUDED.time", (group_id, switch, date) ) + conn.commit() # Commit transaction + cursor.close() + self.pool.putconn(conn) # Return connection to pool + return True else: cursor.execute( - "SELECT * FROM switch WHERE group_id = %s", + "SELECT switch FROM switch WHERE group_id = %s", (group_id,) ) - return cursor.fetchall() - - conn.commit() # Commit transaction - cursor.close() - self.pool.putconn(conn) # Return connection to pool + result = cursor.fetchone() + cursor.close() + self.pool.putconn(conn) # Return connection to pool + if result: + return result['switch'] == '1' + else: + return False except Exception: if config['debug']: traceback.print_exc() + return False def image_exists(self, sha256_hash, md5_hash): try: