2017-03-26 11:00:20 +02:00
|
|
|
from config import config
|
2017-05-09 06:38:47 +02:00
|
|
|
import json
|
2017-03-26 11:00:20 +02:00
|
|
|
from requests_oauthlib import OAuth2Session
|
2018-02-16 20:38:38 +01:00
|
|
|
from flask import session, abort, url_for, request
|
2017-12-29 18:40:32 +01:00
|
|
|
from titanembeds.utils import redis_store, make_user_cache_key
|
2018-02-16 20:37:12 +01:00
|
|
|
from flask_socketio import disconnect
|
2017-03-26 11:00:20 +02:00
|
|
|
|
|
|
|
authorize_url = "https://discordapp.com/api/oauth2/authorize"
|
|
|
|
token_url = "https://discordapp.com/api/oauth2/token"
|
|
|
|
avatar_base_url = "https://cdn.discordapp.com/avatars/"
|
|
|
|
guild_icon_url = "https://cdn.discordapp.com/icons/"
|
|
|
|
|
|
|
|
def update_user_token(discord_token):
|
2018-02-16 20:43:16 +01:00
|
|
|
session['user_keys'] = discord_token
|
2017-03-26 11:00:20 +02:00
|
|
|
|
|
|
|
def make_authenticated_session(token=None, state=None, scope=None):
|
|
|
|
return OAuth2Session(
|
|
|
|
client_id=config['client-id'],
|
|
|
|
token=token,
|
|
|
|
state=state,
|
|
|
|
scope=scope,
|
|
|
|
redirect_uri=url_for("user.callback", _external=True),
|
|
|
|
auto_refresh_kwargs={
|
|
|
|
'client_id': config['client-id'],
|
|
|
|
'client_secret': config['client-secret'],
|
|
|
|
},
|
|
|
|
auto_refresh_url=token_url,
|
|
|
|
token_updater=update_user_token,
|
|
|
|
)
|
|
|
|
|
|
|
|
def discordrest_from_user(endpoint):
|
|
|
|
token = session['user_keys']
|
|
|
|
discord = make_authenticated_session(token=token)
|
|
|
|
req = discord.get("https://discordapp.com/api/v6{}".format(endpoint))
|
|
|
|
return req
|
|
|
|
|
|
|
|
def get_current_authenticated_user():
|
|
|
|
req = discordrest_from_user("/users/@me")
|
|
|
|
if req.status_code != 200:
|
|
|
|
abort(req.status_code)
|
|
|
|
user = req.json()
|
|
|
|
return user
|
|
|
|
|
|
|
|
def user_has_permission(permission, index):
|
|
|
|
return bool((int(permission) >> index) & 1)
|
|
|
|
|
|
|
|
def get_user_guilds():
|
2017-12-29 18:40:32 +01:00
|
|
|
cache = redis_store.get("OAUTH/USERGUILDS/"+str(make_user_cache_key()))
|
2017-05-09 06:38:47 +02:00
|
|
|
if cache:
|
2017-12-29 18:40:32 +01:00
|
|
|
return cache.decode("utf-8")
|
2017-03-26 11:00:20 +02:00
|
|
|
req = discordrest_from_user("/users/@me/guilds")
|
2017-05-09 06:38:47 +02:00
|
|
|
if req.status_code != 200:
|
2018-02-16 20:56:36 +01:00
|
|
|
if hasattr(request, "sid"):
|
2018-02-16 20:37:12 +01:00
|
|
|
disconnect()
|
|
|
|
return
|
2017-05-09 06:38:47 +02:00
|
|
|
abort(req.status_code)
|
|
|
|
req = json.dumps(req.json())
|
2017-12-29 18:40:32 +01:00
|
|
|
redis_store.set("OAUTH/USERGUILDS/"+str(make_user_cache_key()), req, 250)
|
2017-03-26 11:00:20 +02:00
|
|
|
return req
|
|
|
|
|
|
|
|
def get_user_managed_servers():
|
2018-02-16 20:46:04 +01:00
|
|
|
fetched = get_user_guilds()
|
|
|
|
if not fetched:
|
|
|
|
return []
|
|
|
|
guilds = json.loads(fetched)
|
2017-03-26 11:00:20 +02:00
|
|
|
filtered = []
|
|
|
|
for guild in guilds:
|
|
|
|
permission = guild['permissions'] # Manage Server, Ban Members, Kick Members
|
|
|
|
if guild['owner'] or user_has_permission(permission, 5) or user_has_permission(permission, 2) or user_has_permission(permission, 1):
|
|
|
|
filtered.append(guild)
|
|
|
|
filtered = sorted(filtered, key=lambda guild: guild['name'])
|
|
|
|
return filtered
|
|
|
|
|
|
|
|
def get_user_managed_servers_safe():
|
|
|
|
guilds = get_user_managed_servers()
|
|
|
|
if guilds:
|
|
|
|
return guilds
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_user_managed_servers_id():
|
|
|
|
guilds = get_user_managed_servers_safe()
|
|
|
|
ids=[]
|
|
|
|
for guild in guilds:
|
|
|
|
ids.append(guild['id'])
|
|
|
|
return ids
|
|
|
|
|
|
|
|
def check_user_can_administrate_guild(guild_id):
|
|
|
|
guilds = get_user_managed_servers_id()
|
|
|
|
return guild_id in guilds
|
|
|
|
|
|
|
|
def check_user_permission(guild_id, id):
|
|
|
|
guilds = get_user_managed_servers_safe()
|
|
|
|
for guild in guilds:
|
|
|
|
if guild['id'] == guild_id:
|
|
|
|
return user_has_permission(guild['permissions'], id) or guild['owner']
|
|
|
|
return False
|
|
|
|
|
2018-03-04 09:10:15 +01:00
|
|
|
def generate_avatar_url(id, av, discrim="0000", allow_animate=False):
|
2017-12-09 18:25:03 +01:00
|
|
|
if av:
|
2018-03-04 09:18:21 +01:00
|
|
|
print(allow_animate)
|
|
|
|
print(str(av).startswith("a_"))
|
2018-03-04 09:12:02 +01:00
|
|
|
if allow_animate and str(av).startswith("a_"):
|
2018-03-04 09:10:15 +01:00
|
|
|
return avatar_base_url + str(id) + '/' + str(av) + '.gif'
|
2018-02-17 05:27:21 +01:00
|
|
|
return avatar_base_url + str(id) + '/' + str(av) + '.png'
|
2017-12-09 18:25:03 +01:00
|
|
|
else:
|
|
|
|
default_av = [0, 1, 2, 3, 4]
|
|
|
|
discrim = int(discrim)
|
|
|
|
return "https://cdn.discordapp.com/embed/avatars/{}.png".format(default_av[int(discrim) % len(default_av)])
|
2017-03-26 11:00:20 +02:00
|
|
|
|
|
|
|
def generate_guild_icon_url(id, hash):
|
2018-02-17 05:27:21 +01:00
|
|
|
return guild_icon_url + str(id) + "/" + str(hash) + ".png"
|
2017-03-26 11:00:20 +02:00
|
|
|
|
|
|
|
def generate_bot_invite_url(guild_id):
|
2017-09-21 23:47:00 +02:00
|
|
|
url = "https://discordapp.com/oauth2/authorize?&client_id={}&scope=bot&permissions={}&guild_id={}".format(config['client-id'], '641195117', guild_id)
|
2017-03-26 11:00:20 +02:00
|
|
|
return url
|