refactor(bot_commands): 将ADMIN_ID的环境变量读取移至初始化部分

将ADMIN_ID环境变量的读取从处理命令的函数移至文件的初始化部分,以优化代码结构。此更改避免了在每次调用相关函数时都执行环境变量的读取操作。
This commit is contained in:
wood 2024-09-04 15:54:07 +08:00
parent 467cf4b9f1
commit a3cb34d4e4

View File

@ -1,3 +1,4 @@
import os
from telethon import events from telethon import events
from telethon.tl.types import InputPeerUser from telethon.tl.types import InputPeerUser
from telethon.tl.functions.bots import SetBotCommandsRequest from telethon.tl.functions.bots import SetBotCommandsRequest
@ -7,7 +8,7 @@ import json
KEYWORDS_FILE = '/app/data/keywords.json' KEYWORDS_FILE = '/app/data/keywords.json'
WHITELIST_FILE = '/app/data/whitelist.json' WHITELIST_FILE = '/app/data/whitelist.json'
ADMIN_ID = int(os.environ.get('ADMIN_ID'))
def load_json(file_path): def load_json(file_path):
try: try:
with open(file_path, 'r') as f: 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): async def handle_keyword_command(event, client):
sender = await event.get_sender() sender = await event.get_sender()
if sender.id != int(os.environ.get('ADMIN_ID')): if sender.id != ADMIN_ID:
return return
command = event.message.text.split(maxsplit=1) 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): async def handle_whitelist_command(event, client):
sender = await event.get_sender() sender = await event.get_sender()
if sender.id != int(os.environ.get('ADMIN_ID')): if sender.id != ADMIN_ID:
return return
command = event.message.text.split(maxsplit=1) command = event.message.text.split(maxsplit=1)