DiscordEventLog/src/main.py

61 lines
1.5 KiB
Python
Raw Normal View History

2023-11-24 17:49:55 -05:00
import os
from flask import Flask, render_template, redirect, url_for
from flask_discord import DiscordOAuth2Session, requires_authorization, Unauthorized
from config import load_config
app = Flask(__name__, template_folder="../templates")
# 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')
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = str(config['dev'])
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()
@app.route("/callback/")
def callback():
discord.callback()
return redirect(url_for(".me"))
@app.errorhandler(Unauthorized)
def redirect_unauthorized(e):
return redirect(url_for("login"))
@app.route("/me/")
@requires_authorization
def me():
user = discord.fetch_user()
return f"""
<html>
<head>
<title>{user.name}</title>
</head>
<body>
<img src='{user.avatar_url}' />
</body>
</html>"""
@app.route("/")
def index():
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)