如何在带有 discord.py 的烧瓶中使用 async/await 函数?

How can I use async/await functions in flask with discord.py?

我正在尝试制作一个临时 api 用于 Flask 演示。一旦请求 运行,它应该会在我的 discord 服务器的特定频道中发送消息。但是,当我尝试 运行 这个烧瓶给我错误 RuntimeError: Install Flask with the 'async' extra in order to use async views.

我怎样才能使这个工作?

我来了

from flask import Flask, request
import discord
from discord_components import DiscordComponents, ComponentsBot, Button
import asyncio

bot = ComponentsBot(command_prefix = "!")

app = Flask(__name__)

@bot.event
async def on_ready():
    print(f"Logged in as {bot.user}!")


@app.route('/product-bought')
async def query_example():
    product = request.args.get('product')
    print(product)
    channel = bot.get_channel(925144858325319690)
    channel.send(f'New product bought: {product}')
    return(f'Your product is {product}')


if __name__ == '__main__':
    app.run(debug=True, port=5000)

您唯一需要做的就是安装 async extra,使用

pip install flask[async]

看看documentation