首页 > 解决方案 > Docker Container - 当 Python 脚本运行时,Discord Bot 挂起

问题描述

我已经在无头 Ubuntu 服务器上运行了这个脚本,但是当我尝试在具有网络桥接的 docker 容器中运行它时,安装了 python 包的它会将机器人登录,但永远不会进入“on_ready()”函数。

def main(ticker: str):
    import discord
    import asyncio
    import yaml

    filename = 'config.yaml'
    with open(filename) as f:
        config = yaml.load(f, Loader=yaml.Loader)[ticker]

    client = discord.Client()

    

    async def send_update(price, change24Hour, abbreviation):
        status = f'{change24Hour}'
        nickname = f'{abbreviation} ${price}'
        await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=status))
        print(f"Updated name {nickname}")
        print(f"Update status {status}")

        print(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
        guilds = await client.fetch_guilds(limit=150).flatten()
        print("--------Guilds start here--------")

        for guild in guilds:
            await client.get_guild(guild.id).me.edit(nick=nickname)
            await asyncio.sleep(0.5)
            print("Updated: " + str(guild.id))
        print("---------Guilds end here---------")
        print("\n")
        del guilds, status, nickname
        await asyncio.sleep(config['updateFreq'])

    @client.event
    async def on_ready():
        print("test")
        while True:
            getData = get_price(ticker)
            price = str(getData[ticker]['usd'])
            price = scientificToFloat(price)
            change24Hour = str(getData[ticker]['usd_24h_change'])
            change24Hour = "24h: " + str(percentStrip(change24Hour)) + "%"

            await send_update(price, change24Hour, config['abbreviation'])
            del getData, price, change24Hour
    print(str(config['discordBotKey']))
    client.run(str(config['discordBotKey']))

标签: python-3.xdockerdiscord.py

解决方案


推荐阅读