我有一个这样的入口点模块。很好,而且有效。
"""Entry Point"""
import os
import asyncio
from app.core import BotClient # subclass of `discord.ext.commands.Bot`
async def main():
try:
bot = BotClient()
await bot.start(os.getenv("BOT_TOKEN"))
except KeyboardInterrupt:
await bot.close()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
pass
但是当我将 bot
变量放入 if __name__ == "__main__":
时,机器人将不再启动。程序仍在运行并被阻止,但 Bot.on_ready
函数未触发。
"""Entry Point"""
import os
import asyncio
from app.core import BotClient # subclass of `discord.ext.commands.Bot`
async def main():
try:
await bot.start(os.getenv("BOT_TOKEN"))
except KeyboardInterrupt:
await bot.close()
if __name__ == "__main__":
try:
bot = BotClient()
asyncio.run(main())
except KeyboardInterrupt:
pass