This commit is contained in:
commit
7bd35f2713
11 changed files with 805 additions and 0 deletions
14
src/config.py
Normal file
14
src/config.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
import json
|
||||
import logging
|
||||
|
||||
def load_config():
|
||||
try:
|
||||
with open('./config.json', encoding="utf_8") as config_file:
|
||||
data = json.load(config_file)
|
||||
return data
|
||||
except FileNotFoundError:
|
||||
logging.fatal("The configuration file was not found.")
|
||||
return None
|
||||
except json.JSONDecodeError as e:
|
||||
logging.fatal("Error decoding the JSON file: %s", e)
|
||||
return None
|
60
src/main.py
Normal file
60
src/main.py
Normal file
|
@ -0,0 +1,60 @@
|
|||
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)
|
Loading…
Add table
Add a link
Reference in a new issue