How to know when Flask has received data?


This is a long one, so let me explain. I’m trying to write a discord bot in python using oauth2 in flask. Here is what I am trying to achieve in pseudocode: 1: user sends command in channel, 2: the bot then sends an embed with a link to the user that contains the oauth2 authorization, 3: the user clicks on the oauth2 and authorizes which gives the program their email address linked to their discord, 4: that data is then saved as a variable to be used later, and a dm is sent to the user containing their email address. Sounds simple.

Due to being in 2.0 so I can use views and buttons and things I’m not using cogs as they were unreliable and finicky so this is all one big code. I do have flask and the discord bot running on separate threads (discord bot being on 1, and flask being on 2).

import discord
from discord.ext import commands
from decouple import config
import sqlite3
from flask import Flask, request, redirect, render_template
import requests
from waitress import serve
import threading
#discord oauth things
SCOPE = "identify%20email"
DISCORD_LOGIN = f"{CLIENT_ID}&redirect_uri={REDIRECT_URI}&response_type=code&scope={SCOPE}&prompt=consent"
#bot init
class Bot(commands.Bot):
    def __init__(self):
        intents = discord.Intents.default()
        intents.message_content = True
        super().__init__(command_prefix=commands.when_mentioned_or('>'), intents=intents)
    async def on_ready(self):
        print(f'Logged in as {self.user} (ID: {})')
client = Bot()
#all flask
app = Flask(__name__)
@app.route("/", methods = ["get"])
def index():
    return render_template('index.html')
@app.route("/login", methods = ["get"])
def login():
    return redirect(DISCORD_LOGIN)
@app.route("/success", methods = ["get"])
def success():
    code = request.args.get("code")
    useraccesstoken = getaccesstoken(code)
    useremail = getuseremail(useraccesstoken)
    return render_template('success.html'), useremail
def getaccesstoken(code):
    payload = {
       "client_id": CLIENT_ID,
       "client_secret": CLIENT_SECRET,
       "grant_type": "authorization_code",
       "code": code,
       "redirect_uri": REDIRECT_URI,
       "scope": SCOPE
    headers = {
        "Content-Type": "application/x-www-form-urlencoded"
    accesstoken = = DISCORD_TOKEN, data = payload, headers = headers)
    json = accesstoken.json()
    return json.get("access_token")
def getuseremail(useraccesstoken):
    url = DISCORD_API+"/users/@me"
    headers = {
        "Authorization": f"Bearer {useraccesstoken}"
    userdata = requests.get(url = url, headers = headers)
    userjson = userdata.json()
    return userjson.get("email")
def web():
    serve(app, host="", port=5000)
    async def getemail(ctx):
            firstmessageembed = discord.Embed(title = "Link your Plex and your Discord", color= 
            discord.Color.from_rgb(160,131,196), description="🔗 Please click [HERE]( to get started.")
            firstmessageembed.set_author(name =, icon_url =
            firstmessageembed.set_footer(text=f'You have 30 seconds to authorize.')
            await ctx.send(embed = firstmessageembed)
            await client.wait_for(????????????????)

threading.Thread(target=web, daemon=True).start()

As you can see I have no idea what I am waiting for and I have no idea how to know when the user has submitted the oauth2.


Okay so I did a lot of experimenting. Basically you will need to thread the discord bot and then use QUART to use await async to get the data from it. Because there is really no way to send data between, you need to use a database, store the data that you get from Quart into the database, then just access it from the discord bot when you need to.

Answered By – David

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published