2018-07-18 01:47:30 +02:00
|
|
|
from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, get_administrators_list, get_badges, DiscordBotsOrgTransactions
|
2018-01-05 09:52:22 +01:00
|
|
|
from titanembeds.decorators import valid_session_required, discord_users_only, abort_if_guild_disabled
|
2020-11-30 14:20:49 +01:00
|
|
|
from titanembeds.utils import serializer, check_guild_existance, guild_accepts_visitors, guild_query_unauth_users_bool, get_client_ipaddr, discord_api, rate_limiter, channel_ratelimit_key, guild_ratelimit_key, user_unauthenticated, checkUserRevoke, checkUserBanned, update_user_status, check_user_in_guild, get_guild_channels, guild_webhooks_enabled, guild_unauthcaptcha_enabled, get_member_roles, get_online_embed_user_keys, redis_store, redisqueue, get_forced_role
|
2017-04-05 06:00:44 +02:00
|
|
|
from titanembeds.oauth import user_has_permission, generate_avatar_url, check_user_can_administrate_guild
|
2018-07-09 03:37:35 +02:00
|
|
|
import titanembeds.constants as constants
|
2017-07-01 08:52:21 +02:00
|
|
|
from flask import Blueprint, abort, jsonify, session, request, url_for
|
2017-09-21 09:12:49 +02:00
|
|
|
from flask import current_app as app
|
2017-08-25 09:45:59 +02:00
|
|
|
from flask_socketio import emit
|
2017-02-10 06:10:44 +01:00
|
|
|
from sqlalchemy import and_
|
2018-02-27 08:22:58 +01:00
|
|
|
from urllib.parse import urlparse, parse_qsl, urlsplit
|
2017-02-10 06:10:44 +01:00
|
|
|
import random
|
|
|
|
import json
|
2017-03-27 04:46:06 +02:00
|
|
|
import datetime
|
2017-04-10 01:40:11 +02:00
|
|
|
import re
|
2017-09-21 09:12:49 +02:00
|
|
|
import requests
|
2017-02-10 06:10:44 +01:00
|
|
|
from config import config
|
2020-11-30 14:20:49 +01:00
|
|
|
import copy
|
2017-02-10 06:10:44 +01:00
|
|
|
|
|
|
|
api = Blueprint("api", __name__)
|
|
|
|
|
2020-11-30 14:20:49 +01:00
|
|
|
@api.after_request
|
|
|
|
def after_request(response):
|
|
|
|
if response.is_json:
|
|
|
|
session_copy = copy.deepcopy(dict(session))
|
|
|
|
data = response.get_json()
|
|
|
|
data["session"] = serializer.dumps(json.dumps(session_copy))
|
|
|
|
response.set_data(json.dumps(data))
|
|
|
|
return response
|
|
|
|
|
|
|
|
@api.before_request
|
|
|
|
def before_request():
|
|
|
|
authorization = request.headers.get("authorization", None)
|
|
|
|
if authorization:
|
|
|
|
try:
|
|
|
|
data = json.loads(serializer.loads(authorization))
|
|
|
|
session.update(data)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2017-05-29 23:35:51 +02:00
|
|
|
def parse_emoji(textToParse, guild_id):
|
2017-05-30 00:29:16 +02:00
|
|
|
guild_emojis = get_guild_emojis(guild_id)
|
|
|
|
for gemoji in guild_emojis:
|
|
|
|
emoji_name = gemoji["name"]
|
|
|
|
emoji_id = gemoji["id"]
|
2018-03-01 09:37:39 +01:00
|
|
|
emoji_animated = gemoji.get("animated", False)
|
2018-03-01 08:57:45 +01:00
|
|
|
if emoji_animated:
|
|
|
|
textToParse = textToParse.replace(":{}:".format(emoji_name), "<a:{}:{}>".format(emoji_name, emoji_id))
|
|
|
|
else:
|
|
|
|
textToParse = textToParse.replace(":{}:".format(emoji_name), "<:{}:{}>".format(emoji_name, emoji_id))
|
2017-06-10 05:43:23 +02:00
|
|
|
return textToParse
|
2017-05-29 23:35:51 +02:00
|
|
|
|
|
|
|
|
2017-08-02 13:43:51 +02:00
|
|
|
def format_post_content(guild_id, channel_id, message, dbUser):
|
2017-05-09 03:39:22 +02:00
|
|
|
illegal_post = False
|
|
|
|
illegal_reasons = []
|
2017-04-09 00:45:03 +02:00
|
|
|
message = message.replace("<", "\<")
|
2017-04-10 01:40:11 +02:00
|
|
|
message = message.replace(">", "\>")
|
2017-05-30 00:29:16 +02:00
|
|
|
message = parse_emoji(message, guild_id)
|
2017-04-10 01:40:11 +02:00
|
|
|
|
2017-05-09 03:39:22 +02:00
|
|
|
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
|
|
|
|
|
2018-03-25 06:43:39 +02:00
|
|
|
max_len = get_post_content_max_len(guild_id)
|
|
|
|
if len(message) > max_len:
|
|
|
|
illegal_post = True
|
|
|
|
illegal_reasons.append("Exceeded the following message length: {} characters".format(max_len))
|
|
|
|
|
2017-05-09 03:39:22 +02:00
|
|
|
links = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', message)
|
|
|
|
if not dbguild.chat_links and len(links) > 0:
|
|
|
|
illegal_post = True
|
|
|
|
illegal_reasons.append("Links is not allowed.")
|
|
|
|
elif dbguild.chat_links and not dbguild.bracket_links:
|
|
|
|
for link in links:
|
|
|
|
newlink = "<" + link + ">"
|
|
|
|
message = message.replace(link, newlink)
|
|
|
|
|
|
|
|
mention_pattern = re.compile(r'\[@[0-9]+\]')
|
|
|
|
all_mentions = re.findall(mention_pattern, message)
|
|
|
|
if dbguild.mentions_limit != -1 and len(all_mentions) > dbguild.mentions_limit:
|
|
|
|
illegal_post = True
|
|
|
|
illegal_reasons.append("Mentions is capped at the following limit: " + str(dbguild.mentions_limit))
|
|
|
|
for match in all_mentions:
|
2017-04-10 01:40:11 +02:00
|
|
|
mention = "<@" + match[2: len(match) - 1] + ">"
|
|
|
|
message = message.replace(match, mention, 1)
|
2017-07-01 08:52:21 +02:00
|
|
|
|
2018-07-09 03:37:35 +02:00
|
|
|
if dbguild.banned_words_enabled:
|
|
|
|
banned_words = set(json.loads(dbguild.banned_words))
|
|
|
|
if dbguild.banned_words_global_included:
|
|
|
|
banned_words = banned_words.union(set(constants.GLOBAL_BANNED_WORDS))
|
|
|
|
for word in banned_words:
|
|
|
|
word_boundaried = r"\b%s\b" % word
|
|
|
|
regex = re.compile(word_boundaried, re.IGNORECASE)
|
2020-04-30 01:44:37 +02:00
|
|
|
if regex.search(message):
|
2018-07-09 03:37:35 +02:00
|
|
|
illegal_post = True
|
|
|
|
illegal_reasons.append("The following word is prohibited: " + word)
|
|
|
|
|
2017-08-27 23:41:36 +02:00
|
|
|
if not guild_webhooks_enabled(guild_id):
|
2017-07-01 08:52:21 +02:00
|
|
|
if (session['unauthenticated']):
|
|
|
|
message = u"**[{}#{}]** {}".format(session['username'], session['user_id'], message)
|
|
|
|
else:
|
2017-08-02 13:43:51 +02:00
|
|
|
username = session['username']
|
|
|
|
if dbUser:
|
2018-07-18 01:47:30 +02:00
|
|
|
if dbUser["nick"]:
|
|
|
|
username = dbUser["nick"]
|
2017-08-02 13:43:51 +02:00
|
|
|
message = u"**<{}#{}>** {}".format(username, session['discriminator'], message) # I would like to do a @ mention, but i am worried about notify spam
|
2017-05-09 03:39:22 +02:00
|
|
|
return (message, illegal_post, illegal_reasons)
|
2017-04-05 08:25:07 +02:00
|
|
|
|
2017-05-10 10:56:37 +02:00
|
|
|
def format_everyone_mention(channel, content):
|
2017-05-10 11:01:36 +02:00
|
|
|
if not channel["mention_everyone"]:
|
|
|
|
if "@everyone" in content:
|
|
|
|
content = content.replace("@everyone", u"@\u200Beveryone")
|
|
|
|
if "@here" in content:
|
|
|
|
content = content.replace("@here", u"@\u200Bhere")
|
2017-05-10 10:56:37 +02:00
|
|
|
return content
|
|
|
|
|
2017-06-09 06:22:33 +02:00
|
|
|
def filter_guild_channel(guild_id, channel_id, force_everyone=False):
|
2018-07-22 08:46:03 +02:00
|
|
|
forced_role = get_forced_role(guild_id)
|
|
|
|
channels = get_guild_channels(guild_id, force_everyone, forced_role)
|
2017-04-08 09:34:17 +02:00
|
|
|
for chan in channels:
|
2017-05-07 03:19:20 +02:00
|
|
|
if chan["channel"]["id"] == channel_id:
|
2017-04-08 09:34:17 +02:00
|
|
|
return chan
|
|
|
|
return None
|
|
|
|
|
2017-05-24 05:41:12 +02:00
|
|
|
def get_online_discord_users(guild_id, embed):
|
2018-07-18 01:47:30 +02:00
|
|
|
apimembers = redisqueue.list_guild_members(guild_id)
|
2017-04-14 08:10:13 +02:00
|
|
|
apimembers_filtered = {}
|
|
|
|
for member in apimembers:
|
2018-07-18 02:38:37 +02:00
|
|
|
apimembers_filtered[int(member["id"])] = member
|
2018-07-21 01:10:24 +02:00
|
|
|
guild_roles = redisqueue.get_guild(guild_id)["roles"]
|
2017-04-14 08:10:13 +02:00
|
|
|
guildroles_filtered = {}
|
|
|
|
for role in guild_roles:
|
|
|
|
guildroles_filtered[role["id"]] = role
|
|
|
|
for member in embed['members']:
|
2018-01-23 23:59:34 +01:00
|
|
|
apimem = apimembers_filtered.get(int(member["id"]))
|
2017-04-14 08:10:13 +02:00
|
|
|
member["hoist-role"] = None
|
|
|
|
member["color"] = None
|
|
|
|
if apimem:
|
2018-07-18 02:38:37 +02:00
|
|
|
member["hoist-role"] = apimem["hoist-role"]
|
|
|
|
member["color"] = apimem["color"]
|
|
|
|
member["avatar"] = apimem["avatar"]
|
|
|
|
member["avatar_url"] = apimem["avatar_url"]
|
2017-03-27 04:46:06 +02:00
|
|
|
return embed['members']
|
|
|
|
|
|
|
|
def get_online_embed_users(guild_id):
|
2018-01-21 04:14:25 +01:00
|
|
|
usrs = get_online_embed_user_keys(guild_id)
|
|
|
|
unauths = db.session.query(UnauthenticatedUsers).filter(UnauthenticatedUsers.user_key.in_(usrs["UnauthenticatedUsers"]), UnauthenticatedUsers.revoked == False, UnauthenticatedUsers.guild_id == guild_id).all() if usrs["UnauthenticatedUsers"] else []
|
|
|
|
auths = db.session.query(AuthenticatedUsers).filter(AuthenticatedUsers.client_id.in_(usrs["AuthenticatedUsers"]), AuthenticatedUsers.guild_id == guild_id).all() if usrs["AuthenticatedUsers"] else []
|
2017-03-27 04:46:06 +02:00
|
|
|
users = {'unauthenticated':[], 'authenticated':[]}
|
|
|
|
for user in unauths:
|
|
|
|
meta = {
|
|
|
|
'username': user.username,
|
|
|
|
'discriminator': user.discriminator,
|
|
|
|
}
|
|
|
|
users['unauthenticated'].append(meta)
|
|
|
|
for user in auths:
|
|
|
|
client_id = user.client_id
|
2018-07-18 01:47:30 +02:00
|
|
|
usrdb = redisqueue.get_guild_member(guild_id, client_id)
|
2017-03-27 04:46:06 +02:00
|
|
|
meta = {
|
2018-07-18 01:47:30 +02:00
|
|
|
'id': str(usrdb["id"]),
|
|
|
|
'username': usrdb["username"],
|
|
|
|
'nickname': usrdb["nick"],
|
|
|
|
'discriminator': usrdb["discriminator"],
|
|
|
|
'avatar_url': generate_avatar_url(usrdb["id"], usrdb["avatar"]),
|
2017-03-27 04:46:06 +02:00
|
|
|
}
|
|
|
|
users['authenticated'].append(meta)
|
|
|
|
return users
|
|
|
|
|
2017-05-30 00:29:16 +02:00
|
|
|
def get_guild_emojis(guild_id):
|
2018-07-21 01:10:24 +02:00
|
|
|
return redisqueue.get_guild(guild_id)["emojis"]
|
2017-05-30 00:29:16 +02:00
|
|
|
|
2018-03-04 07:29:16 +01:00
|
|
|
def get_guild_roles(guild_id):
|
2018-07-21 01:10:24 +02:00
|
|
|
return redisqueue.get_guild(guild_id)["roles"]
|
2018-03-04 07:29:16 +01:00
|
|
|
|
2017-07-01 08:52:21 +02:00
|
|
|
# Returns webhook url if exists and can post w/webhooks, otherwise None
|
|
|
|
def get_channel_webhook_url(guild_id, channel_id):
|
2017-08-27 23:41:36 +02:00
|
|
|
if not guild_webhooks_enabled(guild_id):
|
|
|
|
return None
|
2018-07-21 01:10:24 +02:00
|
|
|
guild = redisqueue.get_guild(guild_id)
|
|
|
|
guild_webhooks = guild["webhooks"]
|
2017-08-27 23:41:36 +02:00
|
|
|
name = "[Titan] "
|
2017-09-14 08:46:15 +02:00
|
|
|
username = session["username"]
|
|
|
|
if len(username) > 19:
|
|
|
|
username = username[:19]
|
2017-08-27 23:41:36 +02:00
|
|
|
if user_unauthenticated():
|
2017-09-14 08:46:15 +02:00
|
|
|
name = name + username + "#" + str(session["user_id"])
|
2017-08-27 23:41:36 +02:00
|
|
|
else:
|
2017-09-14 08:46:15 +02:00
|
|
|
name = name + username + "#" + str(session["discriminator"])
|
2017-07-01 08:52:21 +02:00
|
|
|
for webhook in guild_webhooks:
|
2017-08-27 23:41:36 +02:00
|
|
|
if channel_id == webhook["channel_id"] and webhook["name"] == name:
|
2017-07-01 08:52:21 +02:00
|
|
|
return {
|
|
|
|
"id": webhook["id"],
|
2019-03-03 23:21:55 +01:00
|
|
|
"token": webhook["token"],
|
|
|
|
"name": webhook.get("name"),
|
|
|
|
"guild_id": webhook.get("guild_id"),
|
|
|
|
"channel_id": webhook.get("channel_id")
|
2017-07-01 08:52:21 +02:00
|
|
|
}
|
2017-08-27 23:41:36 +02:00
|
|
|
webhook = discord_api.create_webhook(channel_id, name)
|
2019-01-08 10:14:59 +01:00
|
|
|
if webhook and "content" in webhook:
|
|
|
|
return webhook["content"]
|
|
|
|
else:
|
|
|
|
return None
|
2019-03-03 23:21:55 +01:00
|
|
|
|
|
|
|
def delete_webhook_if_too_much(webhook):
|
|
|
|
if not webhook:
|
|
|
|
return
|
|
|
|
guild_id = webhook["guild_id"]
|
|
|
|
if guild_webhooks_enabled(guild_id):
|
|
|
|
guild = redisqueue.get_guild(guild_id)
|
|
|
|
guild_webhooks = guild["webhooks"]
|
|
|
|
total_wh_cnt = len(guild_webhooks)
|
|
|
|
titan_wh_cnt = 0
|
|
|
|
for wh in guild_webhooks:
|
|
|
|
if wh["name"].startswith("[Titan] "):
|
|
|
|
titan_wh_cnt = titan_wh_cnt + 1
|
|
|
|
if titan_wh_cnt > 0 and total_wh_cnt >= 8:
|
|
|
|
try:
|
|
|
|
discord_api.delete_webhook(webhook["id"], webhook["token"])
|
|
|
|
except:
|
|
|
|
pass # not my problem now
|
2017-07-01 08:52:21 +02:00
|
|
|
|
2018-03-19 20:04:51 +01:00
|
|
|
def get_all_users(guild_id):
|
2018-07-18 01:47:30 +02:00
|
|
|
users = redisqueue.list_guild_members(guild_id)
|
2018-03-19 20:04:51 +01:00
|
|
|
mem = []
|
|
|
|
for u in users:
|
|
|
|
mem.append({
|
2018-07-18 01:47:30 +02:00
|
|
|
"id": str(u["id"]),
|
|
|
|
"avatar": u["avatar"],
|
|
|
|
"avatar_url": generate_avatar_url(u["id"], u["avatar"], u["discriminator"], True),
|
|
|
|
"username": u["username"],
|
|
|
|
"nickname": u["nick"],
|
|
|
|
"discriminator": u["discriminator"]
|
2018-03-19 20:04:51 +01:00
|
|
|
})
|
|
|
|
return mem
|
|
|
|
|
2017-02-10 06:10:44 +01:00
|
|
|
@api.route("/fetch", methods=["GET"])
|
2017-03-15 02:18:57 +01:00
|
|
|
@valid_session_required(api=True)
|
2018-01-05 09:52:22 +01:00
|
|
|
@abort_if_guild_disabled()
|
2017-04-11 02:34:21 +02:00
|
|
|
@rate_limiter.limit("2 per 2 second", key_func = channel_ratelimit_key)
|
2017-02-10 06:10:44 +01:00
|
|
|
def fetch():
|
2017-04-05 07:10:20 +02:00
|
|
|
guild_id = request.args.get("guild_id")
|
2017-02-10 06:10:44 +01:00
|
|
|
channel_id = request.args.get('channel_id')
|
2018-07-16 05:50:31 +02:00
|
|
|
after_snowflake = request.args.get('after', 0, type=int)
|
2017-02-10 06:10:44 +01:00
|
|
|
if user_unauthenticated():
|
2017-04-05 08:29:57 +02:00
|
|
|
key = session['user_keys'][guild_id]
|
2017-02-10 06:10:44 +01:00
|
|
|
else:
|
|
|
|
key = None
|
2017-04-05 07:10:20 +02:00
|
|
|
status = update_user_status(guild_id, session['username'], key)
|
2017-04-08 09:34:17 +02:00
|
|
|
messages = {}
|
2017-02-10 06:10:44 +01:00
|
|
|
if status['banned'] or status['revoked']:
|
2017-04-05 07:10:20 +02:00
|
|
|
status_code = 403
|
2017-05-27 18:39:38 +02:00
|
|
|
if user_unauthenticated():
|
|
|
|
session['user_keys'].pop(guild_id, None)
|
2017-08-21 06:26:51 +02:00
|
|
|
session.modified = True
|
2017-02-10 06:10:44 +01:00
|
|
|
else:
|
2017-04-08 09:34:17 +02:00
|
|
|
chan = filter_guild_channel(guild_id, channel_id)
|
2017-08-09 03:31:20 +02:00
|
|
|
if not chan:
|
|
|
|
abort(404)
|
2017-09-09 23:46:00 +02:00
|
|
|
if not chan.get("read") or chan["channel"]["type"] != "text":
|
2017-04-08 09:34:17 +02:00
|
|
|
status_code = 401
|
|
|
|
else:
|
2018-07-16 05:50:31 +02:00
|
|
|
messages = redisqueue.get_channel_messages(guild_id, channel_id, after_snowflake)
|
2017-05-14 11:26:29 +02:00
|
|
|
status_code = 200
|
2017-05-07 02:19:12 +02:00
|
|
|
response = jsonify(messages=messages, status=status)
|
2017-05-14 11:26:29 +02:00
|
|
|
response.status_code = status_code
|
2017-03-27 01:37:27 +02:00
|
|
|
return response
|
2017-02-10 06:10:44 +01:00
|
|
|
|
2017-06-09 06:22:33 +02:00
|
|
|
@api.route("/fetch_visitor", methods=["GET"])
|
2018-01-05 09:52:22 +01:00
|
|
|
@abort_if_guild_disabled()
|
2017-06-09 06:22:33 +02:00
|
|
|
@rate_limiter.limit("2 per 2 second", key_func = channel_ratelimit_key)
|
|
|
|
def fetch_visitor():
|
|
|
|
guild_id = request.args.get("guild_id")
|
|
|
|
channel_id = request.args.get('channel_id')
|
2018-07-16 05:50:31 +02:00
|
|
|
after_snowflake = request.args.get('after', 0, type=int)
|
2017-06-09 06:22:33 +02:00
|
|
|
if not guild_accepts_visitors(guild_id):
|
|
|
|
abort(403)
|
|
|
|
messages = {}
|
|
|
|
chan = filter_guild_channel(guild_id, channel_id, True)
|
2017-08-09 03:31:20 +02:00
|
|
|
if not chan:
|
|
|
|
abort(404)
|
2017-09-09 23:46:00 +02:00
|
|
|
if not chan.get("read") or chan["channel"]["type"] != "text":
|
2017-06-09 06:22:33 +02:00
|
|
|
status_code = 401
|
|
|
|
else:
|
2018-07-16 05:50:31 +02:00
|
|
|
messages = redisqueue.get_channel_messages(guild_id, channel_id, after_snowflake)
|
2017-06-09 06:22:33 +02:00
|
|
|
status_code = 200
|
|
|
|
response = jsonify(messages=messages)
|
|
|
|
response.status_code = status_code
|
|
|
|
return response
|
|
|
|
|
2018-03-25 06:43:39 +02:00
|
|
|
def get_guild_specific_post_limit():
|
|
|
|
guild_id = request.form.get("guild_id", None)
|
|
|
|
try:
|
|
|
|
guild_id = int(guild_id)
|
|
|
|
except:
|
|
|
|
guild_id = None
|
|
|
|
seconds = 5
|
|
|
|
if guild_id:
|
|
|
|
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
|
|
|
|
if dbguild:
|
|
|
|
seconds = dbguild.post_timeout
|
|
|
|
return "1 per {} second".format(seconds)
|
|
|
|
|
|
|
|
def get_post_content_max_len(guild_id):
|
|
|
|
try:
|
|
|
|
guild_id = int(guild_id)
|
|
|
|
except:
|
|
|
|
guild_id = None
|
|
|
|
length = 350
|
|
|
|
if guild_id:
|
|
|
|
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
|
|
|
|
if dbguild:
|
|
|
|
length = dbguild.max_message_length
|
|
|
|
return length
|
|
|
|
|
2017-02-10 06:10:44 +01:00
|
|
|
@api.route("/post", methods=["POST"])
|
2017-03-15 02:18:57 +01:00
|
|
|
@valid_session_required(api=True)
|
2018-01-05 09:52:22 +01:00
|
|
|
@abort_if_guild_disabled()
|
2018-03-25 06:43:39 +02:00
|
|
|
@rate_limiter.limit(get_guild_specific_post_limit, key_func = channel_ratelimit_key)
|
2017-02-10 06:10:44 +01:00
|
|
|
def post():
|
2017-04-05 08:25:07 +02:00
|
|
|
guild_id = request.form.get("guild_id")
|
2017-02-10 06:10:44 +01:00
|
|
|
channel_id = request.form.get('channel_id')
|
2018-08-17 05:12:21 +02:00
|
|
|
content = request.form.get('content', "")
|
|
|
|
file = None
|
|
|
|
if "file" in request.files:
|
|
|
|
file = request.files["file"]
|
|
|
|
if file and file.filename == "":
|
|
|
|
file = None
|
2019-11-06 22:45:48 +01:00
|
|
|
rich_embed = request.form.get("richembed", None)
|
|
|
|
if rich_embed:
|
|
|
|
rich_embed = json.loads(rich_embed)
|
2017-08-02 13:43:51 +02:00
|
|
|
if "user_id" in session:
|
2018-07-18 01:47:30 +02:00
|
|
|
dbUser = redisqueue.get_guild_member(guild_id, session["user_id"])
|
2017-08-02 13:43:51 +02:00
|
|
|
else:
|
|
|
|
dbUser = None
|
2017-02-10 06:10:44 +01:00
|
|
|
if user_unauthenticated():
|
2017-04-06 06:32:38 +02:00
|
|
|
key = session['user_keys'][guild_id]
|
2017-02-10 06:10:44 +01:00
|
|
|
else:
|
|
|
|
key = None
|
2017-08-02 13:43:51 +02:00
|
|
|
content, illegal_post, illegal_reasons = format_post_content(guild_id, channel_id, content, dbUser)
|
2017-04-05 07:10:20 +02:00
|
|
|
status = update_user_status(guild_id, session['username'], key)
|
2017-04-08 09:34:17 +02:00
|
|
|
message = {}
|
2017-05-09 03:39:22 +02:00
|
|
|
if illegal_post:
|
|
|
|
status_code = 417
|
2017-02-10 06:10:44 +01:00
|
|
|
if status['banned'] or status['revoked']:
|
2017-03-27 01:37:27 +02:00
|
|
|
status_code = 401
|
|
|
|
else:
|
2017-04-08 09:34:17 +02:00
|
|
|
chan = filter_guild_channel(guild_id, channel_id)
|
2017-09-09 23:46:00 +02:00
|
|
|
if not chan.get("write") or chan["channel"]["type"] != "text":
|
2017-04-08 09:34:17 +02:00
|
|
|
status_code = 401
|
2019-11-06 22:45:48 +01:00
|
|
|
elif (file and not chan.get("attach_files")) or (rich_embed and not chan.get("embed_links")):
|
2018-08-17 05:12:21 +02:00
|
|
|
status_code = 406
|
2017-05-09 03:39:22 +02:00
|
|
|
elif not illegal_post:
|
2017-07-06 19:18:15 +02:00
|
|
|
userid = session["user_id"]
|
2017-05-10 10:56:37 +02:00
|
|
|
content = format_everyone_mention(chan, content)
|
2017-07-01 08:52:21 +02:00
|
|
|
webhook = get_channel_webhook_url(guild_id, channel_id)
|
2017-09-15 08:25:03 +02:00
|
|
|
# if userid in get_administrators_list():
|
|
|
|
# content = "(Titan Dev) " + content
|
2017-07-01 08:52:21 +02:00
|
|
|
if webhook:
|
|
|
|
if (session['unauthenticated']):
|
2019-01-16 19:00:36 +01:00
|
|
|
username = session["username"]
|
|
|
|
if len(username) > 25:
|
|
|
|
username = username[:25]
|
|
|
|
username = username + "#" + str(session["user_id"])
|
2017-09-21 09:54:19 +02:00
|
|
|
avatar = url_for('static', filename='img/titanembeds_square.png', _external=True)
|
2017-09-13 07:55:51 +02:00
|
|
|
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
|
|
|
|
if dbguild:
|
2017-09-24 03:22:07 +02:00
|
|
|
icon = dbguild.guest_icon
|
2017-09-13 07:55:51 +02:00
|
|
|
if icon:
|
|
|
|
avatar = icon
|
2017-07-01 08:52:21 +02:00
|
|
|
else:
|
2017-07-06 19:18:15 +02:00
|
|
|
username = session["username"]
|
2017-08-02 13:43:51 +02:00
|
|
|
if dbUser:
|
2018-07-18 01:47:30 +02:00
|
|
|
if dbUser["nick"]:
|
|
|
|
username = dbUser["nick"]
|
2017-09-15 08:25:03 +02:00
|
|
|
# if content.startswith("(Titan Dev) "):
|
|
|
|
# content = content[12:]
|
|
|
|
# username = "(Titan Dev) " + username
|
2019-01-16 19:00:36 +01:00
|
|
|
if len(username) > 25:
|
|
|
|
username = username[:25]
|
2017-09-15 08:25:03 +02:00
|
|
|
username = username + "#" + str(session['discriminator'])
|
2017-07-01 08:52:21 +02:00
|
|
|
avatar = session['avatar']
|
2019-11-06 22:45:48 +01:00
|
|
|
message = discord_api.execute_webhook(webhook.get("id"), webhook.get("token"), username, avatar, content, file, rich_embed)
|
2019-03-03 23:21:55 +01:00
|
|
|
delete_webhook_if_too_much(webhook)
|
2017-07-01 08:52:21 +02:00
|
|
|
else:
|
2019-11-06 22:45:48 +01:00
|
|
|
message = discord_api.create_message(channel_id, content, file, rich_embed)
|
2017-04-08 09:34:17 +02:00
|
|
|
status_code = message['code']
|
2018-06-15 02:13:09 +02:00
|
|
|
db.session.commit()
|
2017-05-09 03:39:22 +02:00
|
|
|
response = jsonify(message=message.get('content', message), status=status, illegal_reasons=illegal_reasons)
|
2017-03-27 01:37:27 +02:00
|
|
|
response.status_code = status_code
|
|
|
|
return response
|
2017-02-10 06:10:44 +01:00
|
|
|
|
2017-09-21 09:12:49 +02:00
|
|
|
def verify_captcha_request(captcha_response, ip_address):
|
|
|
|
payload = {
|
|
|
|
"secret": config["recaptcha-secret-key"],
|
|
|
|
"response": captcha_response,
|
|
|
|
"remoteip": ip_address,
|
|
|
|
}
|
|
|
|
if app.config["DEBUG"]:
|
|
|
|
del payload["remoteip"]
|
|
|
|
r = requests.post("https://www.google.com/recaptcha/api/siteverify", data=payload).json()
|
|
|
|
return r["success"]
|
|
|
|
|
2017-02-10 06:10:44 +01:00
|
|
|
@api.route("/create_unauthenticated_user", methods=["POST"])
|
2017-09-20 09:07:55 +02:00
|
|
|
@rate_limiter.limit("3 per 30 minute", key_func=guild_ratelimit_key)
|
2018-01-05 09:52:22 +01:00
|
|
|
@abort_if_guild_disabled()
|
2017-02-10 06:10:44 +01:00
|
|
|
def create_unauthenticated_user():
|
|
|
|
session['unauthenticated'] = True
|
|
|
|
username = request.form['username']
|
|
|
|
guild_id = request.form['guild_id']
|
|
|
|
ip_address = get_client_ipaddr()
|
2017-04-08 23:53:58 +02:00
|
|
|
username = username.strip()
|
2017-04-04 07:57:51 +02:00
|
|
|
if len(username) < 2 or len(username) > 32:
|
|
|
|
abort(406)
|
2017-04-14 08:12:42 +02:00
|
|
|
if not all(x.isalnum() or x.isspace() or "-" == x or "_" == x for x in username):
|
2017-04-08 23:53:58 +02:00
|
|
|
abort(406)
|
2017-03-15 02:49:59 +01:00
|
|
|
if not check_guild_existance(guild_id):
|
2017-03-27 04:46:06 +02:00
|
|
|
abort(404)
|
2017-03-27 17:58:47 +02:00
|
|
|
if not guild_query_unauth_users_bool(guild_id):
|
|
|
|
abort(401)
|
2017-11-04 03:58:26 +01:00
|
|
|
if guild_unauthcaptcha_enabled(guild_id):
|
|
|
|
captcha_response = request.form['captcha_response']
|
|
|
|
if not verify_captcha_request(captcha_response, request.remote_addr):
|
|
|
|
abort(412)
|
2018-06-15 02:13:09 +02:00
|
|
|
final_response = None
|
2017-02-10 06:10:44 +01:00
|
|
|
if not checkUserBanned(guild_id, ip_address):
|
|
|
|
session['username'] = username
|
2017-04-15 09:54:57 +02:00
|
|
|
if 'user_id' not in session or len(str(session["user_id"])) > 4:
|
2017-02-10 06:10:44 +01:00
|
|
|
session['user_id'] = random.randint(0,9999)
|
|
|
|
user = UnauthenticatedUsers(guild_id, username, session['user_id'], ip_address)
|
|
|
|
db.session.add(user)
|
|
|
|
key = user.user_key
|
|
|
|
if 'user_keys' not in session:
|
|
|
|
session['user_keys'] = {guild_id: key}
|
|
|
|
else:
|
|
|
|
session['user_keys'][guild_id] = key
|
2017-12-01 05:08:20 +01:00
|
|
|
session.permanent = False
|
2017-02-10 06:10:44 +01:00
|
|
|
status = update_user_status(guild_id, username, key)
|
2018-06-15 02:13:09 +02:00
|
|
|
final_response = jsonify(status=status)
|
2017-02-10 06:10:44 +01:00
|
|
|
else:
|
|
|
|
status = {'banned': True}
|
2017-03-27 18:12:54 +02:00
|
|
|
response = jsonify(status=status)
|
|
|
|
response.status_code = 403
|
2018-06-15 02:13:09 +02:00
|
|
|
final_response = response
|
|
|
|
db.session.commit()
|
|
|
|
return final_response
|
2017-03-15 02:18:57 +01:00
|
|
|
|
2017-08-10 23:27:08 +02:00
|
|
|
@api.route("/change_unauthenticated_username", methods=["POST"])
|
2017-09-20 09:07:55 +02:00
|
|
|
@rate_limiter.limit("1 per 10 minute", key_func=guild_ratelimit_key)
|
2018-01-05 09:52:22 +01:00
|
|
|
@abort_if_guild_disabled()
|
2017-08-10 23:27:08 +02:00
|
|
|
def change_unauthenticated_username():
|
|
|
|
username = request.form['username']
|
|
|
|
guild_id = request.form['guild_id']
|
|
|
|
ip_address = get_client_ipaddr()
|
|
|
|
username = username.strip()
|
|
|
|
if len(username) < 2 or len(username) > 32:
|
|
|
|
abort(406)
|
|
|
|
if not all(x.isalnum() or x.isspace() or "-" == x or "_" == x for x in username):
|
|
|
|
abort(406)
|
|
|
|
if not check_guild_existance(guild_id):
|
|
|
|
abort(404)
|
|
|
|
if not guild_query_unauth_users_bool(guild_id):
|
|
|
|
abort(401)
|
2018-06-15 02:13:09 +02:00
|
|
|
final_response = None
|
2017-08-10 23:27:08 +02:00
|
|
|
if not checkUserBanned(guild_id, ip_address):
|
|
|
|
if 'user_keys' not in session or guild_id not in session['user_keys'] or not session['unauthenticated']:
|
|
|
|
abort(424)
|
2017-08-25 09:45:59 +02:00
|
|
|
emitmsg = {"unauthenticated": True, "username": session["username"], "discriminator": session["user_id"]}
|
2017-08-10 23:27:08 +02:00
|
|
|
session['username'] = username
|
|
|
|
if 'user_id' not in session or len(str(session["user_id"])) > 4:
|
|
|
|
session['user_id'] = random.randint(0,9999)
|
|
|
|
user = UnauthenticatedUsers(guild_id, username, session['user_id'], ip_address)
|
|
|
|
db.session.add(user)
|
|
|
|
key = user.user_key
|
|
|
|
session['user_keys'][guild_id] = key
|
|
|
|
status = update_user_status(guild_id, username, key)
|
2017-08-25 09:45:59 +02:00
|
|
|
emit("embed_user_disconnect", emitmsg, room="GUILD_"+guild_id, namespace="/gateway")
|
2018-06-15 02:13:09 +02:00
|
|
|
final_response = jsonify(status=status)
|
2017-08-10 23:27:08 +02:00
|
|
|
else:
|
|
|
|
status = {'banned': True}
|
|
|
|
response = jsonify(status=status)
|
|
|
|
response.status_code = 403
|
2018-06-15 02:13:09 +02:00
|
|
|
final_response = response
|
|
|
|
db.session.commit()
|
|
|
|
return final_response
|
2017-08-10 23:27:08 +02:00
|
|
|
|
2017-09-24 06:17:06 +02:00
|
|
|
def get_guild_guest_icon(guild_id):
|
2017-09-24 06:51:31 +02:00
|
|
|
guest_icon = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first().guest_icon
|
|
|
|
return guest_icon if guest_icon else url_for('static', filename='img/titanembeds_square.png')
|
2017-09-24 06:17:06 +02:00
|
|
|
|
2017-06-09 06:22:33 +02:00
|
|
|
def process_query_guild(guild_id, visitor=False):
|
2018-07-22 08:46:03 +02:00
|
|
|
forced_role = get_forced_role(guild_id)
|
|
|
|
channels = get_guild_channels(guild_id, visitor, forced_role=forced_role)
|
2018-12-31 21:54:19 +01:00
|
|
|
discordmembers = [] # Discord members & embed members listed here is moved to its own api endpoint
|
|
|
|
embedmembers = {"authenticated": [], "unauthenticated": []}
|
2017-06-09 06:22:33 +02:00
|
|
|
emojis = get_guild_emojis(guild_id)
|
2018-03-04 07:29:16 +01:00
|
|
|
roles = get_guild_roles(guild_id)
|
2017-09-24 06:17:06 +02:00
|
|
|
guest_icon = get_guild_guest_icon(guild_id)
|
2017-06-09 06:22:33 +02:00
|
|
|
if visitor:
|
|
|
|
for channel in channels:
|
|
|
|
channel["write"] = False
|
2018-12-31 21:54:19 +01:00
|
|
|
return jsonify(channels=channels, discordmembers=discordmembers, embedmembers=embedmembers, emojis=emojis, roles=roles, guest_icon=guest_icon, instant_invite=None)
|
2017-06-09 06:22:33 +02:00
|
|
|
|
2017-03-15 02:18:57 +01:00
|
|
|
@api.route("/query_guild", methods=["GET"])
|
|
|
|
@valid_session_required(api=True)
|
2018-01-05 09:52:22 +01:00
|
|
|
@abort_if_guild_disabled()
|
2017-03-15 02:18:57 +01:00
|
|
|
def query_guild():
|
|
|
|
guild_id = request.args.get('guild_id')
|
2017-03-27 04:46:06 +02:00
|
|
|
if check_guild_existance(guild_id):
|
|
|
|
if check_user_in_guild(guild_id):
|
2017-06-09 06:22:33 +02:00
|
|
|
return process_query_guild(guild_id)
|
2017-03-27 17:58:47 +02:00
|
|
|
abort(403)
|
|
|
|
abort(404)
|
2017-03-25 08:52:56 +01:00
|
|
|
|
2017-06-09 06:22:33 +02:00
|
|
|
@api.route("/query_guild_visitor", methods=["GET"])
|
2018-01-05 09:52:22 +01:00
|
|
|
@abort_if_guild_disabled()
|
2017-06-09 06:22:33 +02:00
|
|
|
def query_guild_visitor():
|
|
|
|
guild_id = request.args.get('guild_id')
|
|
|
|
if check_guild_existance(guild_id):
|
|
|
|
if not guild_accepts_visitors(guild_id):
|
|
|
|
abort(403)
|
|
|
|
return process_query_guild(guild_id, True)
|
|
|
|
abort(404)
|
2018-12-31 21:54:19 +01:00
|
|
|
|
|
|
|
@api.route("/server_members", methods=["GET"])
|
|
|
|
@abort_if_guild_disabled()
|
|
|
|
@valid_session_required(api=True)
|
|
|
|
def server_members():
|
2020-11-30 14:22:47 +01:00
|
|
|
abort(404)
|
2018-12-31 21:54:19 +01:00
|
|
|
guild_id = request.args.get("guild_id", None)
|
|
|
|
if not check_guild_existance(guild_id):
|
|
|
|
abort(404)
|
|
|
|
if not check_user_in_guild(guild_id):
|
|
|
|
abort(403)
|
|
|
|
members = query_server_members(guild_id)
|
|
|
|
return jsonify(members)
|
|
|
|
|
|
|
|
@api.route("/server_members_visitor", methods=["GET"])
|
|
|
|
@abort_if_guild_disabled()
|
|
|
|
def server_members_visitor():
|
2020-11-30 14:22:47 +01:00
|
|
|
abort(404)
|
2018-12-31 21:54:19 +01:00
|
|
|
guild_id = request.args.get("guild_id", None)
|
|
|
|
if not check_guild_existance(guild_id):
|
|
|
|
abort(404)
|
|
|
|
if not guild_accepts_visitors(guild_id):
|
|
|
|
abort(403)
|
|
|
|
members = query_server_members(guild_id)
|
|
|
|
return jsonify(members)
|
|
|
|
|
|
|
|
def query_server_members(guild_id):
|
|
|
|
widget = discord_api.get_widget(guild_id)
|
|
|
|
if widget.get("success", True):
|
|
|
|
discordmembers = get_online_discord_users(guild_id, widget)
|
|
|
|
widgetenabled = True
|
|
|
|
else:
|
|
|
|
discordmembers = [{"id": 0, "color": "FFD6D6", "status": "dnd", "username": "Discord Server Widget is Currently Disabled"}]
|
|
|
|
widgetenabled = False
|
|
|
|
embedmembers = get_online_embed_users(guild_id)
|
|
|
|
return {"discordmembers": discordmembers, "embedmembers": embedmembers, "widgetenabled": widgetenabled}
|
2017-06-09 06:22:33 +02:00
|
|
|
|
2017-03-27 04:46:06 +02:00
|
|
|
@api.route("/create_authenticated_user", methods=["POST"])
|
2017-03-25 08:52:56 +01:00
|
|
|
@discord_users_only(api=True)
|
2018-01-05 09:52:22 +01:00
|
|
|
@abort_if_guild_disabled()
|
2017-03-27 04:46:06 +02:00
|
|
|
def create_authenticated_user():
|
|
|
|
guild_id = request.form.get('guild_id')
|
|
|
|
if session['unauthenticated']:
|
|
|
|
response = jsonify(error=True)
|
|
|
|
response.status_code = 401
|
|
|
|
return response
|
2017-03-25 08:52:56 +01:00
|
|
|
else:
|
2017-03-27 04:46:06 +02:00
|
|
|
if not check_guild_existance(guild_id):
|
|
|
|
abort(404)
|
2020-09-18 22:41:04 +02:00
|
|
|
if not check_user_in_guild(guild_id):
|
|
|
|
add_member = discord_api.add_guild_member(guild_id, session['user_id'], session['user_keys']['access_token'])
|
|
|
|
if not add_member["success"]:
|
|
|
|
discord_status_code = add_member["content"].get("code", 0)
|
|
|
|
if discord_status_code == 40007: # user banned from server
|
|
|
|
status = {'banned': True}
|
|
|
|
response = jsonify(status=status)
|
|
|
|
response.status_code = 403
|
|
|
|
else:
|
2017-09-20 23:20:06 +02:00
|
|
|
response = jsonify(add_member)
|
|
|
|
response.status_code = 422
|
2020-09-18 22:41:04 +02:00
|
|
|
return response
|
|
|
|
db_user = db.session.query(AuthenticatedUsers).filter(and_(AuthenticatedUsers.guild_id == guild_id, AuthenticatedUsers.client_id == session['user_id'])).first()
|
|
|
|
if not db_user:
|
|
|
|
db_user = AuthenticatedUsers(guild_id, session['user_id'])
|
|
|
|
db.session.add(db_user)
|
|
|
|
db.session.commit()
|
|
|
|
status = update_user_status(guild_id, session['username'])
|
|
|
|
return jsonify(status=status)
|
2017-12-09 19:47:22 +01:00
|
|
|
|
|
|
|
@api.route("/user/<guild_id>/<user_id>")
|
2018-08-21 02:43:23 +02:00
|
|
|
@abort_if_guild_disabled()
|
2017-12-09 19:47:22 +01:00
|
|
|
def user_info(guild_id, user_id):
|
|
|
|
usr = {
|
|
|
|
"id": None,
|
|
|
|
"username": None,
|
|
|
|
"nickname": None,
|
|
|
|
"discriminator": None,
|
|
|
|
"avatar": None,
|
|
|
|
"avatar_url": None,
|
|
|
|
"roles": [],
|
|
|
|
"badges": [],
|
|
|
|
}
|
2018-07-18 01:47:30 +02:00
|
|
|
member = redisqueue.get_guild_member(guild_id, user_id)
|
2017-12-09 19:47:22 +01:00
|
|
|
if member:
|
2018-07-18 01:47:30 +02:00
|
|
|
usr["id"] = str(member["id"])
|
|
|
|
usr["username"] = member["username"]
|
|
|
|
usr["nickname"] = member["nick"]
|
|
|
|
usr["discriminator"] = member["discriminator"]
|
|
|
|
usr["avatar"] = member["avatar"]
|
2018-03-04 09:18:21 +01:00
|
|
|
usr["avatar_url"] = generate_avatar_url(usr["id"], usr["avatar"], usr["discriminator"], True)
|
2017-12-09 19:47:22 +01:00
|
|
|
roles = get_member_roles(guild_id, user_id)
|
2018-07-21 01:10:24 +02:00
|
|
|
guild_roles = redisqueue.get_guild(guild_id)["roles"]
|
2017-12-09 19:47:22 +01:00
|
|
|
for r in roles:
|
|
|
|
for gr in guild_roles:
|
|
|
|
if gr["id"] == r:
|
|
|
|
usr["roles"].append(gr)
|
|
|
|
usr["badges"] = get_badges(user_id)
|
2018-07-18 01:47:30 +02:00
|
|
|
if redis_store.get("DiscordBotsOrgVoted/" + str(member["id"])):
|
2018-02-18 23:50:24 +01:00
|
|
|
usr["badges"].append("discordbotsorgvoted")
|
|
|
|
return jsonify(usr)
|
2018-08-20 21:43:12 +02:00
|
|
|
|
|
|
|
@api.route("/user/<guild_id>")
|
2018-08-21 02:43:23 +02:00
|
|
|
@abort_if_guild_disabled()
|
|
|
|
@valid_session_required(api=True)
|
2018-08-20 21:43:12 +02:00
|
|
|
def list_users(guild_id):
|
|
|
|
all_users = get_all_users(guild_id)
|
|
|
|
return jsonify(all_users)
|
2018-02-18 23:50:24 +01:00
|
|
|
|
2018-02-27 08:22:58 +01:00
|
|
|
@api.route("/webhook/discordbotsorg/vote", methods=["POST"])
|
2018-02-18 23:50:24 +01:00
|
|
|
def webhook_discordbotsorg_vote():
|
|
|
|
incoming = request.get_json()
|
|
|
|
client_id = incoming.get('bot')
|
2019-11-02 09:14:28 +01:00
|
|
|
if str(config["client-id"]) != str(client_id):
|
2018-02-18 23:50:24 +01:00
|
|
|
abort(401)
|
2019-11-02 09:14:28 +01:00
|
|
|
if str(request.headers.get("Authorization", "")) != str(config.get("discordbotsorg-webhook-secret", "")):
|
2018-02-27 08:22:58 +01:00
|
|
|
abort(403)
|
2019-11-02 09:14:28 +01:00
|
|
|
user_id = str(incoming.get("user"))
|
|
|
|
vote_type = str(incoming.get("type"))
|
2018-02-27 08:30:05 +01:00
|
|
|
params = dict(parse_qsl(urlsplit(incoming.get("query", "")).query))
|
2018-02-18 23:50:24 +01:00
|
|
|
if vote_type == "upvote":
|
|
|
|
redis_store.set("DiscordBotsOrgVoted/" + user_id, "voted", 86400)
|
2018-02-27 08:22:58 +01:00
|
|
|
referrer = None
|
|
|
|
if "referrer" in params:
|
|
|
|
try:
|
2018-03-19 09:35:03 +01:00
|
|
|
referrer = int(params["referrer"])
|
2018-02-27 08:22:58 +01:00
|
|
|
except:
|
|
|
|
pass
|
2018-03-19 09:28:09 +01:00
|
|
|
DBLTrans = DiscordBotsOrgTransactions(int(user_id), vote_type, referrer)
|
2018-02-27 08:22:58 +01:00
|
|
|
db.session.add(DBLTrans)
|
2018-06-15 02:13:09 +02:00
|
|
|
db.session.commit()
|
2018-03-19 09:35:03 +01:00
|
|
|
return ('', 204)
|
2018-03-24 07:03:33 +01:00
|
|
|
|
2018-08-10 06:13:56 +02:00
|
|
|
@api.route("/bot/ban", methods=["POST"])
|
|
|
|
def bot_ban():
|
2018-08-10 06:22:24 +02:00
|
|
|
if request.headers.get("Authorization", "") != config.get("app-secret", ""):
|
2018-08-10 06:13:56 +02:00
|
|
|
return jsonify(error="Authorization header does not match."), 403
|
|
|
|
incoming = request.get_json()
|
|
|
|
guild_id = incoming.get("guild_id", None)
|
|
|
|
placer_id = incoming.get("placer_id", None)
|
|
|
|
username = incoming.get("username", None)
|
|
|
|
discriminator = incoming.get("discriminator", None)
|
|
|
|
if not guild_id or not placer_id or not username:
|
|
|
|
return jsonify(error="Missing required parameters."), 400
|
|
|
|
if discriminator:
|
|
|
|
dbuser = db.session.query(UnauthenticatedUsers) \
|
2018-08-10 06:42:27 +02:00
|
|
|
.filter(UnauthenticatedUsers.guild_id == str(guild_id)) \
|
2018-08-10 06:13:56 +02:00
|
|
|
.filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \
|
|
|
|
.filter(UnauthenticatedUsers.discriminator == discriminator) \
|
|
|
|
.order_by(UnauthenticatedUsers.id.desc()).first()
|
|
|
|
else:
|
|
|
|
dbuser = db.session.query(UnauthenticatedUsers) \
|
2018-08-10 06:42:27 +02:00
|
|
|
.filter(UnauthenticatedUsers.guild_id == str(guild_id)) \
|
2018-08-10 06:13:56 +02:00
|
|
|
.filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \
|
|
|
|
.order_by(UnauthenticatedUsers.id.desc()).first()
|
|
|
|
if not dbuser:
|
|
|
|
return jsonify(error="Guest user cannot be found."), 404
|
|
|
|
dbban = db.session.query(UnauthenticatedBans) \
|
2018-08-10 06:42:27 +02:00
|
|
|
.filter(UnauthenticatedBans.guild_id == str(guild_id)) \
|
2018-08-10 06:13:56 +02:00
|
|
|
.filter(UnauthenticatedBans.last_username == dbuser.username) \
|
|
|
|
.filter(UnauthenticatedBans.last_discriminator == dbuser.discriminator).first()
|
|
|
|
if dbban is not None:
|
|
|
|
if dbban.lifter_id is None:
|
|
|
|
return jsonify(error="Guest user, **{}#{}**, has already been banned.".format(dbban.last_username, dbban.last_discriminator)), 409
|
|
|
|
db.session.delete(dbban)
|
2018-08-10 06:42:27 +02:00
|
|
|
dbban = UnauthenticatedBans(str(guild_id), dbuser.ip_address, dbuser.username, dbuser.discriminator, "", int(placer_id))
|
2018-08-10 06:13:56 +02:00
|
|
|
db.session.add(dbban)
|
|
|
|
db.session.commit()
|
|
|
|
return jsonify(success="Guest user, **{}#{}**, has successfully been added to the ban list!".format(dbban.last_username, dbban.last_discriminator))
|
2019-01-10 03:07:12 +01:00
|
|
|
|
|
|
|
@api.route("/bot/unban", methods=["POST"])
|
|
|
|
def bot_unban():
|
|
|
|
if request.headers.get("Authorization", "") != config.get("app-secret", ""):
|
|
|
|
return jsonify(error="Authorization header does not match."), 403
|
|
|
|
incoming = request.get_json()
|
|
|
|
guild_id = incoming.get("guild_id", None)
|
|
|
|
lifter_id = incoming.get("lifter_id", None)
|
|
|
|
username = incoming.get("username", None)
|
|
|
|
discriminator = incoming.get("discriminator", None)
|
|
|
|
if not guild_id or not lifter_id or not username:
|
|
|
|
return jsonify(error="Missing required parameters."), 400
|
|
|
|
if discriminator:
|
|
|
|
dbuser = db.session.query(UnauthenticatedUsers) \
|
|
|
|
.filter(UnauthenticatedUsers.guild_id == str(guild_id)) \
|
|
|
|
.filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \
|
|
|
|
.filter(UnauthenticatedUsers.discriminator == discriminator) \
|
|
|
|
.order_by(UnauthenticatedUsers.id.desc()).first()
|
|
|
|
else:
|
|
|
|
dbuser = db.session.query(UnauthenticatedUsers) \
|
|
|
|
.filter(UnauthenticatedUsers.guild_id == str(guild_id)) \
|
|
|
|
.filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \
|
|
|
|
.order_by(UnauthenticatedUsers.id.desc()).first()
|
|
|
|
if not dbuser:
|
|
|
|
return jsonify(error="Guest user cannot be found."), 404
|
|
|
|
dbban = db.session.query(UnauthenticatedBans) \
|
|
|
|
.filter(UnauthenticatedBans.guild_id == str(guild_id)) \
|
|
|
|
.filter(UnauthenticatedBans.ip_address == dbuser.ip_address).first()
|
|
|
|
if dbban is None:
|
|
|
|
return jsonify(error="Guest user **{}#{}** has not been banned.".format(dbuser.username, dbuser.discriminator)), 404
|
|
|
|
if dbban.lifter_id is not None:
|
|
|
|
return jsonify(error="Guest user **{}#{}** ban has already been removed.".format(dbuser.username, dbuser.discriminator)), 409
|
|
|
|
dbban.liftBan(int(lifter_id))
|
|
|
|
db.session.commit()
|
|
|
|
return jsonify(success="Guest user, **{}#{}**, has successfully been removed from the ban list!".format(dbuser.username, dbuser.discriminator))
|
2018-08-10 06:13:56 +02:00
|
|
|
|
|
|
|
@api.route("/bot/revoke", methods=["POST"])
|
|
|
|
def bot_revoke():
|
2018-08-10 06:22:24 +02:00
|
|
|
if request.headers.get("Authorization", "") != config.get("app-secret", ""):
|
2018-08-10 06:13:56 +02:00
|
|
|
return jsonify(error="Authorization header does not match."), 403
|
|
|
|
incoming = request.get_json()
|
|
|
|
guild_id = incoming.get("guild_id", None)
|
|
|
|
username = incoming.get("username", None)
|
|
|
|
discriminator = incoming.get("discriminator", None)
|
|
|
|
if not guild_id or not username:
|
|
|
|
return jsonify(error="Missing required parameters."), 400
|
|
|
|
if discriminator:
|
|
|
|
dbuser = db.session.query(UnauthenticatedUsers) \
|
2018-08-10 06:42:27 +02:00
|
|
|
.filter(UnauthenticatedUsers.guild_id == str(guild_id)) \
|
2018-08-10 06:13:56 +02:00
|
|
|
.filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \
|
|
|
|
.filter(UnauthenticatedUsers.discriminator == discriminator) \
|
|
|
|
.order_by(UnauthenticatedUsers.id.desc()).first()
|
|
|
|
else:
|
|
|
|
dbuser = db.session.query(UnauthenticatedUsers) \
|
2018-08-10 06:42:27 +02:00
|
|
|
.filter(UnauthenticatedUsers.guild_id == str(guild_id)) \
|
2018-08-10 06:13:56 +02:00
|
|
|
.filter(UnauthenticatedUsers.username.ilike("%" + username + "%")) \
|
|
|
|
.order_by(UnauthenticatedUsers.id.desc()).first()
|
|
|
|
if not dbuser:
|
|
|
|
return jsonify(error="Guest user cannot be found."), 404
|
|
|
|
elif dbuser.revoked:
|
|
|
|
return jsonify(error="Guest user **{}#{}** has already been kicked!".format(dbuser.username, dbuser.discriminator)), 409
|
|
|
|
dbuser.revoked = True
|
|
|
|
db.session.commit()
|
|
|
|
return jsonify(success="Successfully kicked **{}#{}**!".format(dbuser.username, dbuser.discriminator))
|
|
|
|
|
2018-08-11 08:08:31 +02:00
|
|
|
@api.route("/bot/members")
|
|
|
|
def bot_members():
|
|
|
|
if request.headers.get("Authorization", "") != config.get("app-secret", ""):
|
|
|
|
return jsonify(error="Authorization header does not match."), 403
|
|
|
|
guild_id = request.args.get("guild_id")
|
|
|
|
members = get_online_embed_users(guild_id)
|
|
|
|
return jsonify(members)
|
|
|
|
|
2018-03-24 07:03:33 +01:00
|
|
|
@api.route("/af/direct_message", methods=["POST"])
|
|
|
|
def af_direct_message_post():
|
|
|
|
cs = request.form.get('cs', None)
|
|
|
|
input = request.form.get('input')
|
|
|
|
cleverbot_url = "http://www.cleverbot.com/getreply"
|
|
|
|
payload = {'key': config["cleverbot-api-key"], 'cs': cs, 'input': input}
|
|
|
|
r = requests.get(cleverbot_url, params=payload)
|
2020-04-30 01:44:37 +02:00
|
|
|
return jsonify(r.json())
|