Home > Software engineering >  How can I use async/await functions in flask with discord.py?
How can I use async/await functions in flask with discord.py?

Time:12-29

I'm trying to make a temporary api for a demo with flask. Once the request has been ran, it's supposed to send a message in a specific channel of my discord server. However, when I try to run this flask give me the error RuntimeError: Install Flask with the 'async' extra in order to use async views.

How can I make this work?

Here is my go

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)

CodePudding user response:

The only thing you have to do is install the async extra, do it with

pip install flask[async]

take a look at the documentation

  • Related