0

我正在制作自己的用户机器人,我试图将每个命令放在它自己的 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正在被导入,whilecommand_help被忽略,除非我注释掉 echo import,否则帮助工作

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 = '#'

有谁知道为什么它不能像我预期的那样工作?我真的坚持这个

4

1 回答 1

0

有人建议我将智能插件用于模块化系统,而不是我的解决方案,它解决了这个问题。

于 2022-01-11T06:37:26.580 回答