From a3cb34d4e4bea85f321ef038115092a76c4f5397 Mon Sep 17 00:00:00 2001 From: wood Date: Wed, 4 Sep 2024 15:54:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor(bot=5Fcommands):=20=E5=B0=86ADMIN=5FID?= =?UTF-8?q?=E7=9A=84=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E7=A7=BB=E8=87=B3=E5=88=9D=E5=A7=8B=E5=8C=96=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将ADMIN_ID环境变量的读取从处理命令的函数移至文件的初始化部分,以优化代码结构。此更改避免了在每次调用相关函数时都执行环境变量的读取操作。 --- src/bot_commands.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/bot_commands.py b/src/bot_commands.py index fe1e308..c7d0891 100644 --- a/src/bot_commands.py +++ b/src/bot_commands.py @@ -1,3 +1,4 @@ +import os from telethon import events from telethon.tl.types import InputPeerUser from telethon.tl.functions.bots import SetBotCommandsRequest @@ -7,7 +8,7 @@ import json KEYWORDS_FILE = '/app/data/keywords.json' WHITELIST_FILE = '/app/data/whitelist.json' - +ADMIN_ID = int(os.environ.get('ADMIN_ID')) def load_json(file_path): try: with open(file_path, 'r') as f: @@ -41,7 +42,7 @@ async def register_commands(client, admin_id): async def handle_keyword_command(event, client): sender = await event.get_sender() - if sender.id != int(os.environ.get('ADMIN_ID')): + if sender.id != ADMIN_ID: return command = event.message.text.split(maxsplit=1) @@ -84,7 +85,7 @@ async def execute_keyword_command(event, command, keyword): async def handle_whitelist_command(event, client): sender = await event.get_sender() - if sender.id != int(os.environ.get('ADMIN_ID')): + if sender.id != ADMIN_ID: return command = event.message.text.split(maxsplit=1)