Python telethon只运行一次,命名为';main未定义';

Python telethon只运行一次,命名为';main未定义';,python,telethon,Python,Telethon,我有一个连接频道和帖子的代码,但是每当我运行它时,它会对1-2个频道执行此操作,然后我得到一个错误: 回溯(最近一次呼叫最后一次): 文件“C:/Users/Arnoldas/PycharmProjects/pythonProject/reach.py”,第97行,在 asyncio.run(main()) NameError:未定义名称“main”。是什么导致了这个问题?我如何保持代码运行 code: from telethon.sync import TelegramClient from

我有一个连接频道和帖子的代码,但是每当我运行它时,它会对1-2个频道执行此操作,然后我得到一个错误:

回溯(最近一次呼叫最后一次): 文件“C:/Users/Arnoldas/PycharmProjects/pythonProject/reach.py”,第97行,在 asyncio.run(main()) NameError:未定义名称“main”。是什么导致了这个问题?我如何保持代码运行

code:

from telethon.sync import TelegramClient
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.errors.rpcerrorlist import FloodWaitError
import time
from dotenv import load_dotenv
import os
import asyncio
channel_list=['CocktailBar_Discussion',
'apmcoin_official',
'suprafinance',
'AuroraOfficialEN',
'HelloLambda',
'AlpacaCity',
'themorpheusnetwork',
'chiliz_io',
'BKEXEnglish',
'riodefiofficial',
'OnXFi',
'DeepOnionCoin',
'hellochromia',

'AnimeBEP20',
'daomaker',
'TheCoinEx',
'Ochain',

'pNetworkDefi',
'devprtcl',
'officialxyx',
'powerledger',
'bounce_finance',
'uncx_token',
'bounce_finance',
'concertVR1',
'obyteorg',
'telcoincommunity',
'DAAWallet',

'metahash_ENG',
'surf_finance',
'dfinity',

'xinfintalk',

'xcashglobal',
'propsproject',
'BTCPrivate',
'OIN_Finance_Eng',
'mithcash',
'hivenetwork',

'tokenown',
'trustverse_officialchannel',
'wax_io',
'TerraLunaChat',


'unit_en',
'FilecashGlobal',
'FryWorldFinance',
'originprotocol',
'LibraEcosystem',
'SpartanProtocolOrg',
'wgreenpay',
'cartesiproject',
'buymemecash',
'IGGalaxy',
'AlphaQuark',
'basedmoney',
'hybrixgroup',
'RYIUNITY',
'mftudotnet',

'MixDex',
'mithcash',
'mobius_network'
]


client = TelegramClient('session_name',
                    myid,
                    myapikey
                    )
client.start()
for values in channel_list:
    try:
        destination_channel_username=values
        entity=client.get_entity(destination_channel_username)
        client(JoinChannelRequest(channel=values))
        client.send_message(entity=entity,message='Want to learn how we make crypto hedging easy? Join ProSwap Telegram channel ProSwapCommunity @ TG')
        client.send_file(values, 'C:/Users/Arnoldas/Desktop/PROSWAP.png')
        time.sleep(20)
    except:
        continue
        time.sleep(20)

asyncio.run(main())

您尚未定义main函数

按如下方式编写代码:

from telethon.sync import TelegramClient
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.errors.rpcerrorlist import FloodWaitError
import time
from dotenv import load_dotenv
import os
import asyncio
channel_list=['CocktailBar_Discussion',
'apmcoin_official',
'suprafinance',
'AuroraOfficialEN',
'HelloLambda',
'AlpacaCity',
'themorpheusnetwork',
'chiliz_io',
'BKEXEnglish',
'riodefiofficial',
'OnXFi',
'DeepOnionCoin',
'hellochromia',

'AnimeBEP20',
'daomaker',
'TheCoinEx',
'Ochain',

'pNetworkDefi',
'devprtcl',
'officialxyx',
'powerledger',
'bounce_finance',
'uncx_token',
'bounce_finance',
'concertVR1',
'obyteorg',
'telcoincommunity',
'DAAWallet',

'metahash_ENG',
'surf_finance',
'dfinity',

'xinfintalk',

'xcashglobal',
'propsproject',
'BTCPrivate',
'OIN_Finance_Eng',
'mithcash',
'hivenetwork',

'tokenown',
'trustverse_officialchannel',
'wax_io',
'TerraLunaChat',


'unit_en',
'FilecashGlobal',
'FryWorldFinance',
'originprotocol',
'LibraEcosystem',
'SpartanProtocolOrg',
'wgreenpay',
'cartesiproject',
'buymemecash',
'IGGalaxy',
'AlphaQuark',
'basedmoney',
'hybrixgroup',
'RYIUNITY',
'mftudotnet',

'MixDex',
'mithcash',
'mobius_network'
]


client = TelegramClient('session_name',
                    myid,
                    myapikey
                    )
client.start()
async def main():
    for values in channel_list:
        try:
            destination_channel_username=values
            entity=await client.get_entity(destination_channel_username)
            await client(JoinChannelRequest(channel=values))
            await client.send_message(entity=entity,message='Want to learn how we make crypto hedging easy? Join ProSwap Telegram channel ProSwapCommunity @ TG')
            await client.send_file(values, 'C:/Users/Arnoldas/Desktop/PROSWAP.png')
            await asyncio.sleep(20)
        except:
            continue
            await asyncio.sleep(20)

asyncio.run(main())

请记住,垃圾邮件很可能会让你被禁止。我在哪里发垃圾邮件?我第一次发布这个问题RuntimeWarning:启用tracemalloc以获取对象分配tracebackUse Wait to call Methods是否可以将其添加到注释中?我是新来等待的人