【问题标题】:Discord.py + BottlepyDiscord.py + Bottlepy
【发布时间】:2020-05-06 15:18:23
【问题描述】:

我想用 Discord.py 脚本运行 Bottlepy 脚本,但只启动了 Bottlepy 脚本。

这是我的来源:

import asyncio
import bottle
import discord
from discord.ext import commands

client=commands.Bot(command_prefix=[">", "<", ".", "?", "!"])

#here where my script (not spectacular)

client.run("xxxxxXXXXxxXXetXxXXfXXxXXxXXXXxXxxxXxXX")
bottle.run(host='0.0.0.0', port=80)

【问题讨论】:

    标签: python asynchronous discord.py bottle


    【解决方案1】:

    要运行 Discord 机器人,请使用

    client.run('token')
    

    (如果您想了解 client.start() 的作用:here

    现在会发生什么,只有你的 discord 机器人会运行,而瓶子脚本永远不会被调用。要同时运行它们,我建议查看threading

    【讨论】:

    • 我知道我用 client.run("token") 运行了一个 discord.py 机器人,但是 bottlepy 是同步的,而 discordpy 是异步的。我不知道如何使用线程。
    • discord.py 的事件和函数是异步的,但是 client.run() 仍然是一个阻塞操作,执行完之后什么都没有。我已经为你提供了一个关于线程的 python 文档的链接,我真的建议你在那里阅读。
    • Discord 机器人可以简单地异步运行,而无需使用Client.run。但是,如果您之后执行的是blocking,那么您的 Discord 机器人将无法正常运行,您希望在单独的线程中运行它。
    【解决方案2】:

    Client.start 是一个协程。您需要使用事件循环来运行它。

    【讨论】:

      【解决方案3】:

      瓶子永远不会运行,因为 client.run 被阻塞了。所以你需要在不同的线程中运行瓶子。

      考虑一下

      bot.run(TOKEN)
      print("foobar") # this line will not run until the bot process ends.
      

      因此,其中一个进程需要在备用线程中取消

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多