从多个文件导入修饰函数似乎不起作用
Importing decorated functions from multiple files doesn't seem to work
我正在制作自己的用户机器人,
我试图将每个命令放在它自己的 python 文件中(为了更容易管理)但是由于某些神话原因只有一个文件(导入列表中的第一个)被导入,我试图看通过文档,甚至在 Telegram 的“Pyrogram Inn”聊天中询问,但似乎没有人回应
import asyncio
from os import path
from modules.clients.main_user import user
from modules.commands.echo import command_echo, execute_echo
from modules.commands.help import command_help
def login():
if path.exists("./config.ini"):
print('Credentials config found')
else:
print("Login at https://my.telegram.org/apps and")
api_id = int(input("enter your api_id: "))
api_hash = input("enter your api_hash: ")
with open(f'{str(__file__).replace("main.py", "")}/config.ini', 'w') as config:
config.write(f"[pyrogram] \n api_id = {api_id} \n api_hash = {api_hash}")
if __name__ == "__main__":
login()
user.run()
在上面的例子中只有 command_echo
和 execute_echo
被导入,而 command_help
被忽略,除非我注释掉 echo 导入,然后帮助工作
回显模块内容:
from pyrogram import filters
from modules.clients.main_user import user, command_prefix
chats_involved = {}
loop = False
@user.on_message(filters.command('echo', command_prefix))
async def command_echo(client, message) -> None:
"""Enable repeating of all incoming messages in chat
Args:
client ([Client]): Pyrogram client, usually passed by decorator
message ([Message]): Pyrogram message, usually passed by decorator
"""
global loop
chat_data = await user.get_chat(message.chat.id)
chat_name = f'**{chat_data.title}**'
data = str(message.text)
if "enable" in data.lower() or "true" in data.lower():
chats_involved[message.chat.id] = 1
await message.edit(f"Module **echo** was enabled in {chat_name}")
elif "disable" in data.lower() or "false" in data.lower():
chats_involved[message.chat.id] = 0
loop = False
await message.edit(f"Module **echo** was disabled in {chat_name}")
elif ("loop" in data.lower() or "kill" in data.lower()) and "YES" in data:
loop = True
await message.edit(f"**Loop** mode of **echo** is **activated**! Run, fools!")
elif "loop" in data.lower() or "kill" in data.lower():
if loop == True:
loop = not loop
await message.edit(f"**Loop** mode is very dangerous and can get you **BANNED**, to confirm activation run: ```{command_prefix}echo loop YES```")
try:
if chats_involved[message.chat.id] == 0 and loop:
await message.reply(f"Not really, you forgot to enable **echo**, genius... run: ```{command_prefix}echo true```")
except:
pass # TODO log some info or warning about chat not being in dictionary yet
print(chats_involved)
print(message.chat.id)
#print(loop)
@user.on_message()
async def execute_echo(client, message):
global loop
if message.chat.id not in chats_involved:
chats_involved[message.chat.id] = 0
if chats_involved[message.chat.id] == 1:
if message.text is not f'{command_prefix}echo':
if message.sticker is not None:
while loop:
await message.reply_sticker(message.sticker.file_id)
await message.reply_sticker(message.sticker.file_id)
elif message.text is not None:
print(loop)
while loop:
await message.reply(message.text)
await message.reply(message.text)
# await message.reply(message) # FOR DEBUG
帮助模块内容:
from pyrogram import filters
from modules.clients.main_user import user, command_prefix
commands = {
"echo": f"""
**==Repeat messages after others==**
Usage: ```{command_prefix}echo [option]```
Options:
true, enable : activate echo mode
false, disable : deactivate echo mode
loop, kill : repeats all messages it can see indefinitely,
requires further confirmation for your account's
safety but can be bypassed by confirming it ahead of time"""
}
#@user.on_message(filters.command('help', command_prefix))
@user.on_message()
async def command_help(client, message) -> None:
data = str(message.text)
for command in commands:
await message.edit("TEST TEST!")
两种情况下导入的“main_user”的内容:
from pyrogram import Client
user = Client("LuxTenebris")
command_prefix = '#'
有谁知道为什么它不能像我预期的那样工作?我真的很纠结这个
我被建议使用 Smart Plugins 作为模块化系统而不是我的解决方案,这解决了它。
我正在制作自己的用户机器人, 我试图将每个命令放在它自己的 python 文件中(为了更容易管理)但是由于某些神话原因只有一个文件(导入列表中的第一个)被导入,我试图看通过文档,甚至在 Telegram 的“Pyrogram Inn”聊天中询问,但似乎没有人回应
import asyncio
from os import path
from modules.clients.main_user import user
from modules.commands.echo import command_echo, execute_echo
from modules.commands.help import command_help
def login():
if path.exists("./config.ini"):
print('Credentials config found')
else:
print("Login at https://my.telegram.org/apps and")
api_id = int(input("enter your api_id: "))
api_hash = input("enter your api_hash: ")
with open(f'{str(__file__).replace("main.py", "")}/config.ini', 'w') as config:
config.write(f"[pyrogram] \n api_id = {api_id} \n api_hash = {api_hash}")
if __name__ == "__main__":
login()
user.run()
在上面的例子中只有 command_echo
和 execute_echo
被导入,而 command_help
被忽略,除非我注释掉 echo 导入,然后帮助工作
回显模块内容:
from pyrogram import filters
from modules.clients.main_user import user, command_prefix
chats_involved = {}
loop = False
@user.on_message(filters.command('echo', command_prefix))
async def command_echo(client, message) -> None:
"""Enable repeating of all incoming messages in chat
Args:
client ([Client]): Pyrogram client, usually passed by decorator
message ([Message]): Pyrogram message, usually passed by decorator
"""
global loop
chat_data = await user.get_chat(message.chat.id)
chat_name = f'**{chat_data.title}**'
data = str(message.text)
if "enable" in data.lower() or "true" in data.lower():
chats_involved[message.chat.id] = 1
await message.edit(f"Module **echo** was enabled in {chat_name}")
elif "disable" in data.lower() or "false" in data.lower():
chats_involved[message.chat.id] = 0
loop = False
await message.edit(f"Module **echo** was disabled in {chat_name}")
elif ("loop" in data.lower() or "kill" in data.lower()) and "YES" in data:
loop = True
await message.edit(f"**Loop** mode of **echo** is **activated**! Run, fools!")
elif "loop" in data.lower() or "kill" in data.lower():
if loop == True:
loop = not loop
await message.edit(f"**Loop** mode is very dangerous and can get you **BANNED**, to confirm activation run: ```{command_prefix}echo loop YES```")
try:
if chats_involved[message.chat.id] == 0 and loop:
await message.reply(f"Not really, you forgot to enable **echo**, genius... run: ```{command_prefix}echo true```")
except:
pass # TODO log some info or warning about chat not being in dictionary yet
print(chats_involved)
print(message.chat.id)
#print(loop)
@user.on_message()
async def execute_echo(client, message):
global loop
if message.chat.id not in chats_involved:
chats_involved[message.chat.id] = 0
if chats_involved[message.chat.id] == 1:
if message.text is not f'{command_prefix}echo':
if message.sticker is not None:
while loop:
await message.reply_sticker(message.sticker.file_id)
await message.reply_sticker(message.sticker.file_id)
elif message.text is not None:
print(loop)
while loop:
await message.reply(message.text)
await message.reply(message.text)
# await message.reply(message) # FOR DEBUG
帮助模块内容:
from pyrogram import filters
from modules.clients.main_user import user, command_prefix
commands = {
"echo": f"""
**==Repeat messages after others==**
Usage: ```{command_prefix}echo [option]```
Options:
true, enable : activate echo mode
false, disable : deactivate echo mode
loop, kill : repeats all messages it can see indefinitely,
requires further confirmation for your account's
safety but can be bypassed by confirming it ahead of time"""
}
#@user.on_message(filters.command('help', command_prefix))
@user.on_message()
async def command_help(client, message) -> None:
data = str(message.text)
for command in commands:
await message.edit("TEST TEST!")
两种情况下导入的“main_user”的内容:
from pyrogram import Client
user = Client("LuxTenebris")
command_prefix = '#'
有谁知道为什么它不能像我预期的那样工作?我真的很纠结这个
我被建议使用 Smart Plugins 作为模块化系统而不是我的解决方案,这解决了它。