async def convert(ctx: HTTPRequestContext, snowflake: int): if ctx.request.method == "GET": logging.info(f"Received request to view info on bot {snowflake}") snowflake = int(snowflake) resp = dict((await server.get_botdata(snowflake))[0]) return as_json(resp, code=200) else: try: if "Authorization" not in ctx.request.headers: return HTTPException("Failed to provide token!", # Token was omitted from the headers response=Response("Failed to fetch info!", status=401)) token = ctx.request.headers["Authorization"] # The user token snowflake = int(snowflake) # The bot snowflake req = f"""SELECT * FROM userdata WHERE token = '{token.replace("'", "''")}';""" async with server.pool.acquire() as connection: response = await connection.fetch(req) # Get bots and webhook / gather type if response: bots, type = response[0]["bots"], response[0]["type"] if snowflake not in bots: # That bot is not associated with that token return HTTPException("That snowflake is not valid!", Response("Failed to fetch info!", status=401)) async with server.pool.acquire() as connection: name = await connection.fetchval( f"""SELECT name FROM botdata WHERE id = {snowflake};""" ) # Get the bot's name url = await connection.fetchval( f"""SELECT url FROM botdata WHERE name = '{ctx.request.form["to_bot"].replace("'", "''")}';""" ) # Get the URL of the bot we're sending to if url is None: # That bot is not in our database! return HTTPException("That is an invalid bot!", response=Response("Failed to fetch info!", status=400)) payload = { "from_bot": name, "amount": ctx.request.form["amount"], "to_bot": ctx.request.form["to_bot"], "server_id": ctx.request.form["server_id"] } dumped = json.dumps(payload, indent=4) logging.info(f"Received request to convert {ctx.request.form['amount']} from {name} " f"to {ctx.request.form['to_bot']} on server {ctx.request.form['server_id']}") if type is 0: # If using webhooks try: await server.session.post(url, json=dumped) # Post the payload to the other bot's URL except Exception as e: return HTTPException("An error occurred forwarding to the bot!", response=Response(e, status=500)) return as_json(payload, code=200) else: # If we don't get a response from the given token, the token doesn't exist return HTTPException("Invalid token!", response=Response("Failed to fetch info!", status=401)) except: # Generic error catching, always gives 400 cause how could it be _my_ issue? return HTTPException("An error occurred!", response=Response("Failed to fetch info!", status=400))
async def getguild(ctx: HTTPRequestContext, guild: int): req = f"""SELECT info FROM servdata WHERE UUID = {guild}""" async with server.bot.db._conn.acquire() as connection: response = await connection.fetchval(req) if response: return as_json(json.decode(response), code=200) return Response(status=403)
async def search_handler(ctx): """ Returns json respresentiaton of a google reverse image search query """ img_url = '' for url in ctx.request.form: img_url = url # Check whether the image exists and is actually a proper image image_resp = await get_resp_obj(img_url) if image_resp is None: return as_json({'error': 'Image does not exist'}) if not is_image(image_resp): return as_json({'error': 'URL does not contain a proper image'}) # Search for the image via reverse google image search google_html = await get_resp_html(SEARCH_URI.format(img_url)) if google_html is None: return as_json({'error': 'Google has blocked this IP\nRe-captcha may be required'}) # Try to decode HTML into JSON response try: return as_json(rp.parse_results(google_html)) except Exception as e: return as_json({'error': f'Soup parsing error: {e}'}) return as_json(resp_json)
async def mydata(ctx: HTTPRequestContext): # if "token" not in ctx.request.args: # return redirect("/register", code=302) token = ctx.request.args['token'] api_resp = await server.session.get("https://discordapp.com/api/users/@me", headers={ "Authorization": f"Bearer {token}", }) js = await api_resp.json() if "code" in js: return Response(js["message"], status=js["code"]) async with server.pool.acquire() as connection: exists = await connection.fetch( f"""SELECT * FROM userdata WHERE user_id = {js['id']};""" ) if exists: logging.info(f"Received request to view user info for {js['id']}") js = { "user_id": js["id"], "bots": exists[0]["bots"], } else: logging.info(f"Creating new database entry for user {js['id']}") token = secrets.token_urlsafe(48) await connection.fetch( f"""INSERT INTO userdata VALUES ( {js["id"]}, ARRAY[]::bigint[], '{token}', 0 );""" ) js = { "user_id": js["id"], "bots": [], "token": token, } return as_json(js, code=200)