Isolate objects and basic bot adding to guilds

This commit is contained in:
Jeremy Zhang 2017-03-25 01:38:06 -07:00
parent 2e5f6fafbf
commit a6bc5f753e
3 changed files with 17 additions and 6 deletions

View File

@ -1,9 +1,8 @@
from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers
from titanembeds.decorators import valid_session_required, discord_users_only from titanembeds.decorators import valid_session_required, discord_users_only
from titanembeds.discordrest import DiscordREST from titanembeds.utils import discord_api, cache
from flask import Blueprint, abort, jsonify, session, request from flask import Blueprint, abort, jsonify, session, request
from sqlalchemy import and_ from sqlalchemy import and_
from werkzeug.contrib.cache import SimpleCache
import random import random
import requests import requests
import json import json
@ -11,8 +10,6 @@ import time
from config import config from config import config
api = Blueprint("api", __name__) api = Blueprint("api", __name__)
discord_api = DiscordREST(config['bot-token'])
cache = SimpleCache()
def user_unauthenticated(): def user_unauthenticated():
if 'unauthenticated' in session: if 'unauthenticated' in session:

View File

@ -2,6 +2,7 @@ from flask import Blueprint, request, redirect, jsonify, abort, session, url_for
from requests_oauthlib import OAuth2Session from requests_oauthlib import OAuth2Session
from config import config from config import config
from titanembeds.decorators import discord_users_only from titanembeds.decorators import discord_users_only
from titanembeds.utils import discord_api
user = Blueprint("user", __name__) user = Blueprint("user", __name__)
redirect_url = config['app-base-url'] + "/user/callback" redirect_url = config['app-base-url'] + "/user/callback"
@ -16,7 +17,7 @@ def make_authenticated_session(token=None, state=None, scope=None):
token=token, token=token,
state=state, state=state,
scope=scope, scope=scope,
redirect_uri=request.url_root + "user/callback", redirect_uri=url_for("user.callback", _external=True),
) )
def discordrest_from_user(endpoint): def discordrest_from_user(endpoint):
@ -54,6 +55,10 @@ def generate_avatar_url(id, av):
def generate_guild_icon_url(id, hash): def generate_guild_icon_url(id, hash):
return guild_icon_url + str(id) + "/" + str(hash) + ".jpg" return guild_icon_url + str(id) + "/" + str(hash) + ".jpg"
def generate_bot_invite_url(guild_id):
url = "https://discordapp.com/oauth2/authorize?&client_id={}&scope=bot&permissions={}&guild_id={}&response_type=code&redirect_uri={}".format(config['client-id'], '536083583', guild_id, url_for("user.dashboard", _external=True))
return url
@user.route("/login_authenticated", methods=["GET"]) @user.route("/login_authenticated", methods=["GET"])
def login_authenticated(): def login_authenticated():
session["redirect"] = request.args.get("redirect") session["redirect"] = request.args.get("redirect")
@ -101,7 +106,10 @@ def dashboard():
@user.route("/administrate_guild/<guild_id>") @user.route("/administrate_guild/<guild_id>")
@discord_users_only() @discord_users_only()
def administrate_guild(guild_id): def administrate_guild(guild_id):
return str(guild_id) guild = discord_api.get_guild(guild_id)
if guild['code'] == 403:
return redirect(generate_bot_invite_url(guild_id))
return str(guild)
@user.route('/me') @user.route('/me')
@discord_users_only() @discord_users_only()

6
titanembeds/utils.py Normal file
View File

@ -0,0 +1,6 @@
from werkzeug.contrib.cache import SimpleCache
from titanembeds.discordrest import DiscordREST
from config import config
discord_api = DiscordREST(config['bot-token'])
cache = SimpleCache()