[英]How to know when Flask has received data?
這是一個很長的問題,所以讓我解釋一下。 我正在嘗試在燒瓶中使用 oauth2 在 python 中編寫一個不和諧的機器人。 這是我試圖在偽代碼中實現的目標:1:用戶在通道中發送命令,2:機器人然后向用戶發送包含 oauth2 授權的鏈接的嵌入,3:用戶單擊 oauth2 並授權哪個為程序提供與他們的不和諧相關聯的電子郵件地址,4:然后將該數據保存為變量以供以后使用,並向用戶發送包含其電子郵件地址的 dm。 聽起來很簡單。
由於 discord.py 在 2.0 中,所以我可以使用視圖和按鈕以及我不使用 cogs 的東西,因為它們不可靠且很挑剔,所以這都是一個大代碼。 我確實有燒瓶和不和諧機器人在不同的線程上運行(不和諧機器人在 1 上,燒瓶在 2 上)。
#imports
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
CLIENT_ID = ''
CLIENT_SECRET = ''
REDIRECT_URI = "http://127.0.0.1:5000/success"
SCOPE = "identify%20email"
DISCORD_LOGIN = f"https://discord.com/api/oauth2/authorize?client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}&response_type=code&scope={SCOPE}&prompt=consent"
DISCORD_TOKEN = "https://discord.com/api/oauth2/token"
DISCORD_API = "https://discord.com/api"
#bot init
CLIENTTOKEN = config('CLIENTTOKEN')
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: {self.user.id})')
print('------')
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 = requests.post(url = 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="127.0.0.1", port=5000)
#command
@client.command()
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](http://127.0.0.1:5000/login) to get started.")
firstmessageembed.set_author(name = ctx.message.author, icon_url = ctx.author.avatar.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()
client.run(CLIENTTOKEN)
如您所見,我不知道我在等待什么,也不知道如何知道用戶何時提交了 oauth2。
我提出了這個概念,你可以嘗試使用
好的,所以我做了很多實驗。 基本上,您需要線程化 discord 機器人,然后使用 QUART 使用 await async 從中獲取數據。 因為確實沒有辦法在兩者之間發送數據,所以您需要使用數據庫,將您從 Quart 獲取的數據存儲到數據庫中,然后在需要時從 discord bot 中訪問它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.