Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to Quart and quart-discord; uses async #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions Part 1/main.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
from flask import Flask, render_template, request, session
from oauth import Oauth
from quart import Quart, render_template, request, session
from quart_discord import DiscordOAuth2Session


app = Flask(__name__)
app = Quart(__name__)
app.config["SECRET_KEY"] = "test123"
app.config["DISCORD_CLIENT_ID"] = 1234567890 # Discord client ID.
app.config["DISCORD_CLIENT_SECRET"] = "SHHHH" # Discord client secret.
app.config["DISCORD_REDIRECT_URI"] = "" # URL to your callback endpoint.

discord = DiscordOAuth2Session(app)

@app.route("/")
def home():
return render_template("index.html",discord_url= Oauth.discord_login_url)
async def home():
return await render_template("index.html",discord_url="login url here")

@app.route("/login")
def login():
code = request.args.get("code")

at = Oauth.get_access_token(code)
session["token"] = at

user = Oauth.get_user_json(at)
user_name, user_id = user.get("username"), user.get("discriminator")

return f"Success, logged in as {user_name}#{user_id}"

async def login():
"""The function that logs the user in.

It will redirect them to your Discord OAuth2 Flow,
and they will then be redirected back to your callback
endpoint, or REDIRECT_URI.
"""
return await discord.create_session()

@app.route("/callback")
async def callback():
"""Callback.

This will handle the authentication of
the user, and create the session and cookies.
"""
await discord.callback()
user = await discord.fetch_user()
return f"Hello, {user.name}#{user.discriminator}! <img src='{user.avatar_url}'>"

@app.route('/logout')
async def logout():
"""Logs out the user by REVOKING!!! their token."""
discord.revoke()

if __name__ == "__main__":
app.run(debug=True)