No project description provided
Project description
Discord-Flask is a feature rich extension for Flask, with discord.py like functionalities.
Installation
Install Released Version
pip install Discord-Flask
To install current development version you can use following command:
pip install git+https://github.com/hunter87ff/Discord-Flask.git
Basic Example
import os
import traceback
import config
from flask import Flask, redirect, url_for, render_template
from discord_flask import Session, User, requires_authorization
from discord_flask.exceptions import Unauthorized
app = Flask(__name__)
app.guilds = {} #for cache purpose
app.secret_key = b"SECRET_KEY"
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "true" # !! Only in development environment.
discord = Session(
app=app,
bot_token="TOKEN", # make sure to access these as secret
client_id="CLIENT_ID",
client_secret="CLIENT_SECRET",
redirect_uri="http://127.0.0.1:5000/callback"
)
@app.errorhandler(Unauthorized)
def redirect_unauthorized(e):
return redirect(url_for("login"))
@app.route("/")
def home():
return """<a href="/login/">Login</a>"""
@app.route("/login/")
def login():
return discord.create_session()
@app.route("/callback/")
def callback():
try:
redirect_to = discord.callback().get("redirect", "/dashboard/")
return redirect(redirect_to)
except Exception as e:
traceback.print_exc()
return render_template("error/500.html"), 500
@app.route("/dashboard/")
@requires_authorization
def dashboard():
try:
user:User = discord.fetch_user()
_guilds_list = []
_guild_dict = {g.id:g for g in discord.fetch_guilds() if g.permissions.administrator}
app.guilds = _guild_dict
return render_template(
"dash.html",
avatar=user.avatar_url,
leng=len(_guilds_list),
guilds=_guild_dict.values(),
user=user
)
except Exception:
traceback.print_exc()
return "Something went wrong", 500
@app.route("/dashboard/<guild_id>/")
@requires_authorization
def guild_dashboard(guild_id):
if not app.guilds:
app.guilds = {g.id:g for g in discord.fetch_guilds() if g.permissions.manage_guild}
guild = discord.get_guild(guild_id)
if not guild:
return redirect(config.INVITE_URL + guild_id)
if guild.id not in app.guilds.keys():
return redirect("/dashboard")
return render_template("guild.html",guild=guild, config=config) #Change configurations accordingly
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
For an example to the working application, check test_app.py.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
discord_flask-1.0.4.tar.gz
(22.0 kB
view details)
Built Distribution
File details
Details for the file discord_flask-1.0.4.tar.gz
.
File metadata
- Download URL: discord_flask-1.0.4.tar.gz
- Upload date:
- Size: 22.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25857d56e8c6892481b525b4397b1494ebb6583c29481f3b46e76673a83eb144 |
|
MD5 | b60b6f41dbb6896c0aa12cf1b04d2197 |
|
BLAKE2b-256 | 1a421b22f6c70aca18e96617576061fe4f1c8e01c68aa1228a1be2813e528d12 |
File details
Details for the file Discord_Flask-1.0.4-py3-none-any.whl
.
File metadata
- Download URL: Discord_Flask-1.0.4-py3-none-any.whl
- Upload date:
- Size: 35.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fa440fa6056488d3e60ee5e1c626b3e8719320cb5017e60a01a854e59777f56 |
|
MD5 | a509384844bfd02a89f3427181ced9d8 |
|
BLAKE2b-256 | 3ce89c240349762012b70bd9568d5a565716088ef79ed410a9b44f2ba0ac5464 |