2023-11-24 17:49:55 -05:00
|
|
|
import os
|
|
|
|
from flask import Flask, render_template, redirect, url_for
|
2023-11-25 01:24:05 -05:00
|
|
|
from flask_discord import DiscordOAuth2Session, requires_authorization, AccessDenied, Unauthorized
|
2023-11-24 17:49:55 -05:00
|
|
|
from config import load_config
|
|
|
|
|
2023-11-25 01:24:05 -05:00
|
|
|
app = Flask(__name__, template_folder="templates")
|
2023-11-24 17:49:55 -05:00
|
|
|
|
|
|
|
# This code reads the variables set in the site's 'config.json' file.
|
|
|
|
config = load_config()
|
|
|
|
|
|
|
|
app.secret_key = config['secret_key'].encode('UTF-8')
|
2023-11-25 01:24:05 -05:00
|
|
|
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = config['dev']
|
2023-11-24 17:49:55 -05:00
|
|
|
app.config["DISCORD_CLIENT_ID"] = config['discord']['client_id']
|
|
|
|
app.config["DISCORD_CLIENT_SECRET"] = config['discord']['client_secret']
|
|
|
|
app.config["DISCORD_REDIRECT_URI"] = config['webroot'] + "/callback/"
|
|
|
|
app.config["DISCORD_BOT_TOKEN"] = config['discord']['bot_token']
|
|
|
|
|
|
|
|
discord = DiscordOAuth2Session(app)
|
|
|
|
|
|
|
|
@app.route("/login/")
|
|
|
|
def login():
|
|
|
|
return discord.create_session()
|
|
|
|
|
2023-11-25 01:24:05 -05:00
|
|
|
@app.route("/logout/")
|
|
|
|
def logout():
|
|
|
|
discord.revoke()
|
|
|
|
return redirect(url_for(".index"))
|
2023-11-24 17:49:55 -05:00
|
|
|
|
|
|
|
@app.route("/callback/")
|
|
|
|
def callback():
|
|
|
|
discord.callback()
|
2023-11-25 01:24:05 -05:00
|
|
|
return redirect(url_for(".index"))
|
2023-11-24 17:49:55 -05:00
|
|
|
|
|
|
|
@app.errorhandler(Unauthorized)
|
2023-11-25 01:24:05 -05:00
|
|
|
@app.errorhandler(AccessDenied)
|
2023-11-24 17:49:55 -05:00
|
|
|
def redirect_unauthorized(e):
|
2023-11-25 01:24:05 -05:00
|
|
|
return redirect(url_for(".index"))
|
2023-11-24 17:49:55 -05:00
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def index():
|
2023-11-25 01:24:05 -05:00
|
|
|
if discord.authorized:
|
|
|
|
user = discord.fetch_user()
|
|
|
|
print(user.default_avatar_url, user.username, user.discriminator)
|
|
|
|
return render_template('index.html', name=config['name'], user=user)
|
2023-11-24 17:49:55 -05:00
|
|
|
return render_template('index.html', name=config['name'])
|
|
|
|
|
|
|
|
@app.route("/hello")
|
|
|
|
def hello():
|
|
|
|
return render_template('hello.html', name="Flask")
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(host="localhost", port=config['port'], debug=True)
|