Exemplo n.º 1
0
async def insert_characters(self,
                            member: discord.Member,
                            api_key,
                            type_session: str,
                            ctx=None):
    if not (hasattr(self, "bot")):
        self.bot = self

    gw2Api = Gw2Api(self.bot)

    try:
        api_req_characters = await gw2Api.call_api("characters", key=api_key)
        insert_obj = BotUtils.Object()
        insert_obj.api_key = api_key
        insert_obj.ctx = ctx
        insert_obj.gw2Api = gw2Api
        insert_obj.discord_user_id = member.id

        if type_session == "start":
            gw2CharsStartSql = Gw2CharsStartSql(self.bot)
            await gw2CharsStartSql.insert_character(insert_obj,
                                                    api_req_characters)
        else:
            gw2CharsEndSql = Gw2CharsEndSql(self.bot)
            await gw2CharsEndSql.insert_character(insert_obj,
                                                  api_req_characters)

    except Exception as e:
        return self.bot.log.error(e)
Exemplo n.º 2
0
async def get_last_session_user_stats(self, ctx, api_key):
    if not (hasattr(self, "bot")):
        self.bot = self

    gw2Api = Gw2Api(self.bot)
    user_obj = BotUtils.Object()
    user_obj.gold = 0
    user_obj.karma = 0
    user_obj.laurels = 0
    user_obj.badges_honor = 0
    user_obj.guild_commendations = 0
    user_obj.wvw_tickets = 0
    user_obj.proof_heroics = 0
    user_obj.test_heroics = 0
    user_obj.players = 0
    user_obj.yaks_scorted = 0
    user_obj.yaks = 0
    user_obj.camps = 0
    user_obj.castles = 0
    user_obj.towers = 0
    user_obj.keeps = 0

    try:
        api_req_acc = await gw2Api.call_api("account", key=api_key)
        api_req_wallet = await gw2Api.call_api("account/wallet", key=api_key)
        api_req_achiev = await gw2Api.call_api("account/achievements",
                                               key=api_key)
    except Exception as e:
        if ctx is not None:
            await BotUtils.send_info_msg(
                self, ctx, "GW2 API is currently down. Try again later...")
            return self.bot.log.error(e)

    user_obj.acc_name = api_req_acc["name"]
    user_obj.wvw_rank = api_req_acc["wvw_rank"]

    if len(api_req_wallet) > 0:
        for wallet in api_req_wallet:
            if wallet["id"] == 1:
                user_obj.gold = wallet["value"]
            if wallet["id"] == 2:
                user_obj.karma = wallet["value"]
            if wallet["id"] == 3:
                user_obj.laurels = wallet["value"]
            if wallet["id"] == 15:
                user_obj.badges_honor = wallet["value"]
            if wallet["id"] == 16:
                user_obj.guild_commendations = wallet["value"]
            if wallet["id"] == 26:
                user_obj.wvw_tickets = wallet["value"]
            if wallet["id"] == 31:
                user_obj.proof_heroics = wallet["value"]
            if wallet["id"] == 36:
                user_obj.test_heroics = wallet["value"]

    if len(api_req_achiev) > 0:
        for achiev in api_req_achiev:
            if achiev["id"] == 283:
                user_obj.players = achiev["current"]
            if achiev["id"] == 285:
                user_obj.yaks_scorted = achiev["current"]
            if achiev["id"] == 288:
                user_obj.yaks = achiev["current"]
            if achiev["id"] == 291:
                user_obj.camps = achiev["current"]
            if achiev["id"] == 294:
                user_obj.castles = achiev["current"]
            if achiev["id"] == 297:
                user_obj.towers = achiev["current"]
            if achiev["id"] == 300:
                user_obj.keeps = achiev["current"]

    return user_obj
Exemplo n.º 3
0
async def _add_key(self, ctx, api_key: str):
    #     if (isinstance(ctx.channel, discord.DMChannel)):
    #         msg = "GW2 add api command needs to be used in a server channel for proper roles to be assign!!!"
    #         await BotUtils.send_private_error_msg(self, ctx, msg)
    #         return

    gw2Configs = Gw2ConfigsSql(self.bot)
    rs_gw2_sc = await gw2Configs.get_gw2_server_configs(ctx.guild.id)
    if len(rs_gw2_sc) == 0 or (len(rs_gw2_sc) > 0
                               and rs_gw2_sc[0]["last_session"] == "N"):
        return await BotUtils.send_error_msg(
            self, ctx, "Unable to add api key.\n"
            "Last session is not active on this server.\n"
            f"To activate use: `{ctx.prefix}gw2 config lastsession on`")
    discord_user_id = ctx.message.author.id
    discord_server_id = ctx.message.guild.id

    # searching if API key already exists in bot database
    gw2KeySql = Gw2KeySql(self.bot)
    rs = await gw2KeySql.get_api_key(discord_server_id, api_key)
    if len(rs) == 0:
        gw2Api = Gw2Api(self.bot)
        is_valid_key = await gw2Api.check_api_key(api_key)
        if not isinstance(is_valid_key, dict):
            return await BotUtils.send_private_error_msg(
                self, ctx, f"{is_valid_key.args[1]}\n`{api_key}`")

        key_name = is_valid_key["name"]
        permissions = ','.join(is_valid_key["permissions"])

        try:
            # getting gw2 acc name
            api_req_acc_info = await gw2Api.call_api("account", key=api_key)
            gw2_acc_name = api_req_acc_info["name"]
            member_server_id = api_req_acc_info["world"]
        except Exception as e:
            await BotUtils.send_private_error_msg(self, ctx, e)
            return self.bot.log.error(ctx, e)

        try:
            # getting gw2 server name
            endpoint = f"worlds/{member_server_id}"
            api_req_server = await gw2Api.call_api(endpoint, key=api_key)
            gw2_server_name = api_req_server["name"]
        except Exception as e:
            await BotUtils.send_private_error_msg(self, ctx, e)
            self.bot.log.error(ctx, e)
            return

        # searching if user has 1 api key already
        rs = await gw2KeySql.get_server_user_api_key(discord_server_id,
                                                     discord_user_id)
        if len(rs) > 0:
            # update key
            updateObject = BotUtils.Object()
            updateObject.discord_user_id = discord_user_id
            updateObject.discord_server_id = discord_server_id
            updateObject.key_name = key_name
            updateObject.gw2_acc_name = gw2_acc_name
            updateObject.permissions = permissions
            updateObject.key = api_key
            updateObject.server_name = gw2_server_name
            await gw2KeySql.update_api_key(updateObject)
            msg = "Your API key was **replaced**.\n" \
                  f"Server: `{gw2_server_name}`\n" \
                  f"To get info about your new api key use: `{ctx.prefix}gw2 key info`"
            color = self.bot.gw2_settings["EmbedColor"]
            await BotUtils.send_private_msg(self, ctx, color, msg)
        else:
            # insert key
            insertObject = BotUtils.Object()
            insertObject.discord_user_id = discord_user_id
            insertObject.discord_server_id = discord_server_id
            insertObject.key_name = key_name
            insertObject.gw2_acc_name = gw2_acc_name
            insertObject.permissions = permissions
            insertObject.key = api_key
            insertObject.server_name = gw2_server_name
            await gw2KeySql.insert_api_key(insertObject)
            msg = "Your key was verified and was **added** to your discord account.\n" \
                  f"Server: `{gw2_server_name}`\n" \
                  f"To get info about your api key use: `{ctx.prefix}gw2 key info`"
            color = self.bot.gw2_settings["EmbedColor"]
            await BotUtils.send_private_msg(self, ctx, color, msg)

        # checking if the bot needs to assign gw2 server roles to user
        gw2Roles = Gw2RolesSql(self.bot)
        rs = await gw2Roles.get_gw2_server_role(ctx.message.channel.guild.id,
                                                gw2_server_name.lower())
        if len(rs) > 0:
            new_role = BotUtils.check_server_has_role(self, ctx.guild,
                                                      gw2_server_name.lower())
            if new_role is not None:
                await Gw2Utils.assignGw2GuildRoles(self, ctx,
                                                   ctx.message.author,
                                                   new_role, api_key)
        else:
            await Gw2Utils.removeAllGw2RolesFromUser(self, ctx.message.author)

    elif len(rs) == 1 and rs[0]["discord_user_id"] == discord_user_id:
        msg = "That API key is already registered by you or someone else.\n" \
              f"To get info about your api key use: `{ctx.prefix}gw2 key info`"
        await BotUtils.send_private_error_msg(self, ctx, msg)
    else:
        await BotUtils.send_private_error_msg(
            self, ctx, "That API key is already in use by someone else.")