def fetch(): guild_id = request.args.get("guild_id") channel_id = request.args.get('channel_id') after_snowflake = request.args.get('after', None, type=int) if user_unauthenticated(): key = session['user_keys'][guild_id] else: key = None status = update_user_status(guild_id, session['username'], key) messages = {} if status['banned'] or status['revoked']: status_code = 403 if user_unauthenticated(): session['user_keys'].pop(guild_id, None) session.modified = True else: chan = filter_guild_channel(guild_id, channel_id) if not chan: abort(404) if not chan.get("read") or chan["channel"]["type"] != "text": status_code = 401 else: messages = get_channel_messages(guild_id, channel_id, after_snowflake) status_code = 200 response = jsonify(messages=messages, status=status) response.status_code = status_code return response
def get_channel_webhook_url(guild_id, channel_id): if not guild_webhooks_enabled(guild_id): return None guild = redisqueue.get_guild(guild_id) guild_webhooks = guild["webhooks"] name = "[Titan] " username = session["username"] if len(username) > 19: username = username[:19] if user_unauthenticated(): name = name + username + "#" + str(session["user_id"]) else: name = name + username + "#" + str(session["discriminator"]) for webhook in guild_webhooks: if channel_id == webhook["channel_id"] and webhook["name"] == name: return { "id": webhook["id"], "token": webhook["token"], "name": webhook.get("name"), "guild_id": webhook.get("guild_id"), "channel_id": webhook.get("channel_id") } webhook = discord_api.create_webhook(channel_id, name) if webhook and "content" in webhook: return webhook["content"] else: return None
def post(): guild_id = request.form.get("guild_id") channel_id = request.form.get('channel_id') content = request.form.get('content') if "user_id" in session: dbUser = GuildMembers.query.filter(GuildMembers.guild_id == guild_id).filter(GuildMembers.user_id == str(session['user_id'])).first() else: dbUser = None if user_unauthenticated(): key = session['user_keys'][guild_id] else: key = None content, illegal_post, illegal_reasons = format_post_content(guild_id, channel_id, content, dbUser) status = update_user_status(guild_id, session['username'], key) message = {} if illegal_post: status_code = 417 if status['banned'] or status['revoked']: status_code = 401 else: chan = filter_guild_channel(guild_id, channel_id) if not chan.get("write") or chan["channel"]["type"] != "text": status_code = 401 elif not illegal_post: userid = session["user_id"] content = format_everyone_mention(chan, content) webhook = get_channel_webhook_url(guild_id, channel_id) # if userid in get_administrators_list(): # content = "(Titan Dev) " + content if webhook: if (session['unauthenticated']): username = session["username"] + "#" + str(session["user_id"]) avatar = url_for('static', filename='img/titanembeds_square.png', _external=True) dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() if dbguild: icon = dbguild.guest_icon if icon: avatar = icon else: username = session["username"] if dbUser: if dbUser.nickname: username = dbUser.nickname # if content.startswith("(Titan Dev) "): # content = content[12:] # username = "******" + username username = username + "#" + str(session['discriminator']) avatar = session['avatar'] message = discord_api.execute_webhook(webhook.get("id"), webhook.get("token"), username, avatar, content) else: message = discord_api.create_message(channel_id, content) status_code = message['code'] response = jsonify(message=message.get('content', message), status=status, illegal_reasons=illegal_reasons) response.status_code = status_code return response
def get_channel_webhook_url(guild_id, channel_id): if not guild_webhooks_enabled(guild_id): return None dbguild = db.session.query(Guilds).filter( Guilds.guild_id == guild_id).first() guild_webhooks = json.loads(dbguild.webhooks) name = "[Titan] " username = session["username"] if len(username) > 19: username = username[:19] if user_unauthenticated(): name = name + username + "#" + str(session["user_id"]) else: name = name + username + "#" + str(session["discriminator"]) for webhook in guild_webhooks: if channel_id == webhook["channel_id"] and webhook["name"] == name: return {"id": webhook["id"], "token": webhook["token"]} webhook = discord_api.create_webhook(channel_id, name) return webhook["content"]
def post(): guild_id = request.form.get("guild_id") channel_id = request.form.get('channel_id') content = request.form.get('content', "") file = None if "file" in request.files: file = request.files["file"] if file and file.filename == "": file = None rich_embed = request.form.get("richembed", None) if rich_embed: rich_embed = json.loads(rich_embed) if "user_id" in session: dbUser = redisqueue.get_guild_member(guild_id, session["user_id"]) else: dbUser = None if user_unauthenticated(): key = session['user_keys'][guild_id] else: key = None content, illegal_post, illegal_reasons = format_post_content( guild_id, channel_id, content, dbUser) status = update_user_status(guild_id, session['username'], key) message = {} if illegal_post: status_code = 417 if status['banned'] or status['revoked']: status_code = 401 else: chan = filter_guild_channel(guild_id, channel_id) if not chan.get("write") or chan["channel"]["type"] != "text": status_code = 401 elif (file and not chan.get("attach_files")) or ( rich_embed and not chan.get("embed_links")): status_code = 406 elif not illegal_post: userid = session["user_id"] content = format_everyone_mention(chan, content) webhook = get_channel_webhook_url(guild_id, channel_id) # if userid in get_administrators_list(): # content = "(Titan Dev) " + content if webhook: if (session['unauthenticated']): username = session["username"] if len(username) > 25: username = username[:25] username = username + "#" + str(session["user_id"]) avatar = url_for('static', filename='img/titanembeds_square.png', _external=True) dbguild = db.session.query(Guilds).filter( Guilds.guild_id == guild_id).first() if dbguild: icon = dbguild.guest_icon if icon: avatar = icon else: username = session["username"] if dbUser: if dbUser["nick"]: username = dbUser["nick"] # if content.startswith("(Titan Dev) "): # content = content[12:] # username = "******" + username if len(username) > 25: username = username[:25] username = username + "#" + str(session['discriminator']) avatar = session['avatar'] message = discord_api.execute_webhook(webhook.get("id"), webhook.get("token"), username, avatar, content, file, rich_embed) delete_webhook_if_too_much(webhook) else: message = discord_api.create_message(channel_id, content, file, rich_embed) status_code = message['code'] db.session.commit() response = jsonify(message=message.get('content', message), status=status, illegal_reasons=illegal_reasons) response.status_code = status_code return response