DiscordEventLog/src/main.py
2023-11-25 01:24:05 -05:00

52 lines
1.6 KiB
Python

import os
from flask import Flask, render_template, redirect, url_for
from flask_discord import DiscordOAuth2Session, requires_authorization, AccessDenied, 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"] = 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("/logout/")
def logout():
discord.revoke()
return redirect(url_for(".index"))
@app.route("/callback/")
def callback():
discord.callback()
return redirect(url_for(".index"))
@app.errorhandler(Unauthorized)
@app.errorhandler(AccessDenied)
def redirect_unauthorized(e):
return redirect(url_for(".index"))
@app.route("/")
def index():
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)
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)