在Python中检查子类

2024-09-29 19:30:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用telegramcli,现在,我必须检查用户是否在一个特定的组中,只有这几行代码

with TelegramClient(name, api_id, api_hash) as client:
    result = client(functions.messages.CheckChatInviteRequest(hash=hash))

我用print(type(result))得到一个结果:

<class 'telethon.tl.types.ChatInvite'>如果我不在组内,这是

<class 'telethon.tl.types.ChatInviteAlready'>如果我已经在里面的话。你知道吗

现在,我想做的是:

if type(result) == telethon.tl.types.ChatInvite:
    print('You are not inside the group')

但很明显这不起作用,它给了我这个错误NameError: name 'telethon' is not defined 如何检查子类?你知道吗

谢谢:)


Tags: nameclientapi检查用户typenothashresult
1条回答
网友
1楼 · 发布于 2024-09-29 19:30:43

如果可以导入ChatInvite,请执行以下操作:

from telethon.tl.types import ChatInvite

result = get_result(...)

if isinstance(result, ChatInvite): 
    print('You are not inside the group')

相关问题 更多 >

    热门问题