mirror of
https://github.com/yawaflua/Flask-Discord.git
synced 2026-02-04 10:14:15 +02:00
changed a bunch of stuff to async
This commit is contained in:
10
README.md
10
README.md
@@ -29,24 +29,24 @@ discord = DiscordOAuth2Session(app)
|
|||||||
|
|
||||||
|
|
||||||
@app.route("/login/")
|
@app.route("/login/")
|
||||||
def login():
|
async def login():
|
||||||
return discord.create_session()
|
return discord.create_session()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/callback/")
|
@app.route("/callback/")
|
||||||
def callback():
|
async def callback():
|
||||||
discord.callback()
|
await discord.callback()
|
||||||
return redirect(url_for(".me"))
|
return redirect(url_for(".me"))
|
||||||
|
|
||||||
|
|
||||||
@app.errorhandler(Unauthorized)
|
@app.errorhandler(Unauthorized)
|
||||||
def redirect_unauthorized(e):
|
async def redirect_unauthorized(e):
|
||||||
return redirect(url_for("login"))
|
return redirect(url_for("login"))
|
||||||
|
|
||||||
|
|
||||||
@app.route("/me/")
|
@app.route("/me/")
|
||||||
@requires_authorization
|
@requires_authorization
|
||||||
def me():
|
async def me():
|
||||||
user = discord.fetch_user()
|
user = discord.fetch_user()
|
||||||
return f"""
|
return f"""
|
||||||
<html>
|
<html>
|
||||||
|
|||||||
@@ -137,14 +137,14 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
|||||||
"""
|
"""
|
||||||
return session.get("DISCORD_OAUTH2_TOKEN")
|
return session.get("DISCORD_OAUTH2_TOKEN")
|
||||||
|
|
||||||
def callback(self):
|
async def callback(self):
|
||||||
"""A method which should be always called after completing authorization code grant process
|
"""A method which should be always called after completing authorization code grant process
|
||||||
usually in callback view.
|
usually in callback view.
|
||||||
It fetches the authorization token and saves it quart
|
It fetches the authorization token and saves it quart
|
||||||
`session <https://pgjones.gitlab.io/quart/reference/source/quart.sessions.html#quart.sessions.Session>`_ object.
|
`session <https://pgjones.gitlab.io/quart/reference/source/quart.sessions.html#quart.sessions.Session>`_ object.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
error = request.values.get("error")
|
error = await request.values.get("error")
|
||||||
if error:
|
if error:
|
||||||
if error == "access_denied":
|
if error == "access_denied":
|
||||||
raise exceptions.AccessDenied()
|
raise exceptions.AccessDenied()
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ HYPERLINK = '<a href="{}">{}</a>'
|
|||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
async def index():
|
||||||
if not discord.authorized:
|
if not discord.authorized:
|
||||||
return f"""
|
return f"""
|
||||||
{HYPERLINK.format(url_for(".login"), "Login")} <br />
|
{HYPERLINK.format(url_for(".login"), "Login")} <br />
|
||||||
@@ -38,34 +38,34 @@ def index():
|
|||||||
|
|
||||||
|
|
||||||
@app.route("/login/")
|
@app.route("/login/")
|
||||||
def login():
|
async def login():
|
||||||
return discord.create_session()
|
return discord.create_session()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/login-data/")
|
@app.route("/login-data/")
|
||||||
def login_with_data():
|
async def login_with_data():
|
||||||
return discord.create_session(data=dict(redirect="/me/", coupon="15off", number=15, zero=0, status=False))
|
return discord.create_session(data=dict(redirect="/me/", coupon="15off", number=15, zero=0, status=False))
|
||||||
|
|
||||||
|
|
||||||
@app.route("/invite-bot/")
|
@app.route("/invite-bot/")
|
||||||
def invite_bot():
|
async def invite_bot():
|
||||||
return discord.create_session(scope=["bot"], permissions=8, guild_id=464488012328468480, disable_guild_select=True)
|
return discord.create_session(scope=["bot"], permissions=8, guild_id=464488012328468480, disable_guild_select=True)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/invite-oauth/")
|
@app.route("/invite-oauth/")
|
||||||
def invite_oauth():
|
async def invite_oauth():
|
||||||
return discord.create_session(scope=["bot", "identify"], permissions=8)
|
return discord.create_session(scope=["bot", "identify"], permissions=8)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/callback/")
|
@app.route("/callback/")
|
||||||
def callback():
|
async def callback():
|
||||||
data = discord.callback()
|
data = await discord.callback()
|
||||||
redirect_to = data.get("redirect", "/")
|
redirect_to = data.get("redirect", "/")
|
||||||
return redirect(redirect_to)
|
return redirect(redirect_to)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/me/")
|
@app.route("/me/")
|
||||||
def me():
|
async def me():
|
||||||
user = discord.fetch_user()
|
user = discord.fetch_user()
|
||||||
return f"""
|
return f"""
|
||||||
<html>
|
<html>
|
||||||
@@ -83,19 +83,19 @@ def me():
|
|||||||
|
|
||||||
|
|
||||||
@app.route("/me/guilds/")
|
@app.route("/me/guilds/")
|
||||||
def user_guilds():
|
async def user_guilds():
|
||||||
guilds = discord.fetch_guilds()
|
guilds = discord.fetch_guilds()
|
||||||
return "<br />".join([f"[ADMIN] {g.name}" if g.permissions.administrator else g.name for g in guilds])
|
return "<br />".join([f"[ADMIN] {g.name}" if g.permissions.administrator else g.name for g in guilds])
|
||||||
|
|
||||||
|
|
||||||
@app.route("/add_to/<int:guild_id>/")
|
@app.route("/add_to/<int:guild_id>/")
|
||||||
def add_to_guild(guild_id):
|
async def add_to_guild(guild_id):
|
||||||
user = discord.fetch_user()
|
user = discord.fetch_user()
|
||||||
return user.add_to_guild(guild_id)
|
return user.add_to_guild(guild_id)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/me/connections/")
|
@app.route("/me/connections/")
|
||||||
def my_connections():
|
async def my_connections():
|
||||||
user = discord.fetch_user()
|
user = discord.fetch_user()
|
||||||
connections = discord.fetch_connections()
|
connections = discord.fetch_connections()
|
||||||
return f"""
|
return f"""
|
||||||
@@ -112,14 +112,14 @@ def my_connections():
|
|||||||
|
|
||||||
|
|
||||||
@app.route("/logout/")
|
@app.route("/logout/")
|
||||||
def logout():
|
async def logout():
|
||||||
discord.revoke()
|
discord.revoke()
|
||||||
return redirect(url_for(".index"))
|
return redirect(url_for(".index"))
|
||||||
|
|
||||||
|
|
||||||
@app.route("/secret/")
|
@app.route("/secret/")
|
||||||
@requires_authorization
|
@requires_authorization
|
||||||
def secret():
|
async def secret():
|
||||||
return os.urandom(16)
|
return os.urandom(16)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user