Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python discord.ext在正确通道中发送和设置状态时出现问题_Python_Python 3.x_Discord.py - Fatal编程技术网

Python discord.ext在正确通道中发送和设置状态时出现问题

Python discord.ext在正确通道中发送和设置状态时出现问题,python,python-3.x,discord.py,Python,Python 3.x,Discord.py,我正在使用discord.py,然后使用discord.ext模块。我有一个相当长的selenium代码在while True循环中运行。这是其中的一部分: @bot.command() async def start(ctx): driver.install_addon(r'some_extension.xpi') channel = bot.get_channel(791352165393498203) user = bot.get_u

我正在使用discord.py,然后使用discord.ext模块。我有一个相当长的selenium代码在
while True
循环中运行。这是其中的一部分:

@bot.command()
    async def start(ctx):
        driver.install_addon(r'some_extension.xpi')
        channel = bot.get_channel(791352165393498203)
        user = bot.get_user(257428782233812993)
        while True:
            await bot.change_presence(status=Status.online)
            driver.get('some_link')
            driver.maximize_window()
            time.sleep(5)
            try:
                driver.find_element_by_xpath('//*[@id="pageSize"]').click()
            except NoSuchElementException:
                await ctx.channel.send(f" We had an Exception, please go check")
                time.sleep(60)
                driver.refresh()
                time.sleep(5)
                driver.find_element_by_xpath('//*[@id="pageSize"]').click()
                time.sleep(1)
                await ctx.channel.send(f"Problem solved")
这段代码的大部分工作正常,但有两个主要问题,我没有得到一个例外

  • 首先,
    等待ctx.channel.send(f“我们有一个异常,请检查”)
    应该在我上面提到的频道中发送,但是 没有
    pycharm
    表示从未使用通道变量, 不应该是这样的

  • 其次,
    等待bot.change_presence(status=status.online)
    不会保留bot 永久显示为联机

  • 只需使用
    channel.send()
    而不使用
    ctx
    ctx
    是有关所用命令(作者、频道等)的一组信息
  • 您不需要将状态更改为联机,当您启动bot时,它会自动设置

  • 哦,我建议使用
    asyncio.sleep()
    而不是
    time.sleep()
    ,因为当你使用
    time.sleep()
    时,机器人会在这段时间停止工作,并且不会响应任何命令/事件。

    我也读过关于ansyncio的文章,如果你说它会响应其他命令,我可以试试。实际上,我需要改变我的状态,因为机器人像24/7一样运行,只有一个启动命令,几分钟后它就离线了。我将尝试channel.send(),但在更改时间后,这对meAlright来说似乎没有什么意义。sleep by asyncio.sleep联机状态正常,channel.send而不是ctx.channel.send也正常工作。谢谢