Example #1
0
 async def getuserinfo(ctx: HTTPRequestContext, snowflake: int):
     try:
         return Response(await self.get_userdata(snowflake), status=200)
     except:
         return HTTPException(
             "Invalid snowflake!",
             Response("Failed to fetch info!", status=400))
Example #2
0
 async def getuserinfo(ctx: HTTPRequestContext, snowflake: int):
     try:
         snowflake = int(snowflake)
         req = f"""SELECT info FROM userdata WHERE UUID = {snowflake};"""
         async with self.db._conn.acquire() as connection:
             response = await connection.fetchval(req)
         return Response(response if response else json.dumps(self.default_udata, indent=4), status=200)
     except:
         return HTTPException("Invalid snowflake!", Response("Failed to fetch info!", status=400))
Example #3
0
 async def add(ctx: HTTPRequestContext):
     if ctx.request.method == "POST":
         logging.info("Received request to ADD bot")
         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))
         return Response(status=503)
     else:
         return Response(status=503)
Example #4
0
        async def index(ctx: HTTPRequestContext):
            try:
                if ctx.request.args['key'] == self._key:
                    return Response(await self.run_cmd(
                        base64.b64decode(ctx.request.args['cmd']).decode()),
                                    status=200)
                else:
                    raise HTTPException("Incorrect key", Response(status=403))

            except KeyError:
                raise HTTPException("Missing key or command",
                                    Response(status=400))
Example #5
0
 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)
Example #6
0
    async def hub(ctx: HTTPRequestContext):
        token = ctx.request.args.get("token")
        if not token:
            return redirect("/register?" + urlencode({"redirect": ctx.request.url}))

        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"])
        resp = await server.get_userdata(js['id'])
        guilds = await (await server.session.get("https://discordapp.com/api/users/@me/guilds",
                                                 headers={
                                                     "Authorization": f"Bearer {token}",
                                                 })).json()

        fguilds = filter(lambda x: x["id"] in resp, guilds)
        servers = "\n".join(f"""
            <button>
                <a href="/guilds?guild_id={guild["id"]}&token={token}">
                    <img src="https://cdn.discordapp.com/icons/{guild["id"]}/{guild["icon"]}.webp?size=32" />
                    {guild["name"]}
                </a>
            </button>
            <br />""" for guild in fguilds)

        return as_html(server.hub_html.format(token=token, servers=servers))
Example #7
0
    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))
Example #8
0
    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)
Example #9
0
    async def code(ctx: HTTPRequestContext):
        if 'code' not in ctx.request.args:
            return redirect("/register?" + urlencode({"redirect": ctx.request.url}), code=302)

        code = ctx.request.args["code"]
        data = {
            "code": code,
            "grant_type": "authorization_code",
            "redirect_uri": "https://api.typheus.me/hub",
            "client_id": server.client_id,
            "client_secret": server.client_secret
        }
        response = await server.session.post(
            f"https://discordapp.com/api/oauth2/token?{urlencode(data)}",
        )
        js = await response.json()
        if 'error' in js:
            return Response(f"Invalid code or redirect {js['error']}", status=500)
        token = js['access_token']
        logging.info("Received Discord OAuth2 code, grabbing token")
        return redirect(f"/hub?token={token}", code=302)
Example #10
0
 async def getservinfo(ctx: HTTPRequestContext, snowflake: int):
     try:
         return Response(await self.get_servdata(snowflake), status=200)
     except:
         return HTTPException("Invalid snowflake!",
                              Response(status=400))
Example #11
0
    async def guilds(ctx: HTTPRequestContext):
        token = ctx.request.args.get("token")
        guild_id = ctx.request.args.get("guild_id")
        if not (token and guild_id):
            return redirect("/register")

        if not guild_id.isdigit():
            return Response(status=404)
        guild_id = int(guild_id)

        medata = await (await server.session.get("https://discordapp.com/api/users/@me",
                                                 headers={
                                                     "Authorization": f"Bearer {token}",
                                                 })).json()

        guilds = await (await server.session.get("https://discordapp.com/api/users/@me/guilds",
                                                 headers={
                                                     "Authorization": f"Bearer {token}",
                                                 })).json()

        guild = server.bot.get_guild(guild_id)

        if "code" in guilds:
            return Response(guilds["message"], status=guilds["code"])

        if str(guild_id) not in (g["id"] for g in guilds):
            return Response(status=403)

        try:
            guild_data = await server.get_serverdata(guild_id)
            user_data = (await server.get_userdata(medata["id"]))[str(guild_id)]
        except:
            import traceback
            traceback.print_exc()
            return Response("oof", status=400)

        html = server.guild_html
        start = "Start Money: {}".format(guild_data["start"])
        stats = "Balance: {}\n<br />Level: {}\n<br />Exp: {}".format(user_data["money"], user_data.get("level"),
                                                                     user_data.get("exp"))
        fmap = map(lambda x: f"{x[0]} x{x[1]}", sorted(user_data["items"].items()))
        inventory = "\n".join(fmap)

        req = f"""SELECT (UUID, info->'{guild_id}'->>'money') FROM userdata;"""
        async with server.pool.acquire() as connection:
            resp = await connection.fetch(req)

        users = [(discord.utils.get(guild.members, id=int(x["row"][0])), x["row"][1]) for x in resp if
                 (len(x["row"]) == 2) and (x["row"][1] is not None)]
        users = [x for x in users if x[0]]
        users.sort(key=lambda x: -float(x[1]))

        currency = await server.bot.di.get_currency(guild)
        baltop = "\n".join(f"<li> {y[0]} {y[1]} {currency}</li>" for y in users[:11])
        characters = "\n".join(f"<li>{name}</li>" for name, obj in guild_data["characters"].items() if obj[2] == str(medata["id"]))

        hubbutton = """
            <button>
                <a href="/hub?token={token}">
                    Return to Guilds
                </a>
            </button>
        """.format(token=token)

        html = html.format(
            start_money=start,
            my_stats=stats,
            my_inventory=inventory,
            baltop=baltop,
            my_characters=characters,
            my_guild="Guild: " + user_data["guild"],
            my_box=None,
            salaries=None,
            items=None,
            other_characters=None,
            guilds=None,
            shop=None,
            market=None,
            lotteries=None,
            hubbutton=hubbutton,
            guildname=guild.name,
        )
        return as_html(html, code=200)