无法关闭aiohttp连接/ API。 (新手)

时间:2018-08-12 17:17:14

标签: python api discord.py

我一直在搜索并尝试几乎所有尝试,但都没有获得很大的成功。 问题是,当我在discord bot下运行命令时,出现“会话未关闭”错误,并在调用API时最终超时。 在超时之前,一切正常。 如果我在bot init上调用connection,我没有任何问题,但是我感觉打开与ubisoft 24/7的连接不正确。

该解决方案可能非常简单,因为我是python和API的全新用户。非常感谢所有帮助。

Bot:



async def cmd_rank(self, guild, author, username):

print("Searching for uplay user {} ".format(username))
        auth = R6data.Auth("username", "password")

        username = format(username)
        player = await auth.get_player(username, R6data.Platforms.UPLAY)
        rankdata = await player.get_rank(R6data.RankedRegions.EU)
        rank = rankdata.rank.partition(' ')[0]

        #user = discord.utils.get(guild.members, id=int(leftover_args.pop(0)))
        user = discord.utils.get(guild.members, id=author.id)
        member = self.get_guild(SERVERS['main']).get_member(author.id)
        platinum = discord.utils.get(guild.roles, id=ROLES['ps4']) 

        member_roles = discord.utils.get(member.guild.roles, id=RANKED_ROLES[rank])
        if member_roles:
            await member.add_roles(member_roles)
        print('We gave user new rank: %s'%(user))
        self.uplay_usernames[author.id] = {'uplay': '{}'.format(player.id), 'discord': '{}'.format(author.id)}
        write_json('uplay_user.json', self.uplay_usernames)
        auth.session.close
        return Response(':thumbsup: {} are now added to role {} and stored in our database' .format(author.mention, rank), delete_after=60)

连接:

@staticmethod
    def get_basic_token(email, password):
        return base64.b64encode((email + ":" + password).encode("utf-8")).decode("utf-8")

    def __init__(self, email=None, password=None, token=None, appid=None, cachetime=120, max_connect_retries=1):
        self.session = aiohttp.ClientSession()
        self.max_connect_retries = max_connect_retries

        if email is not None and password is not None:
            self.token = Auth.get_basic_token(email, password)
        elif token is not None:
            self.token = token
        else:
            raise TypeError("Argument error, requires either email/password or token to be set, neither given")

        if appid is not None:
            self.appid = appid
        else:
            self.appid = "39baebad-39e5-4552-8c25-2c9b919064e2"

        self.sessionid = ""
        self.key = ""
        self.uncertain_spaceid = ""
        self.spaceids = {
            "uplay": "5172a557-50b5-4665-b7db-e3f2e8c5041d",
            "psn": "05bfb3f7-6c21-4c42-be1f-97a33fb5cf66",
            "xbl": "98a601e5-ca91-4440-b1c5-753f601a2c90"
        }
        self.profileid = ""
        self.userid = ""
        self.genome = ""

        self.cachetime = cachetime
        self.cache={}

        self._definitions = None
        self._op_definitions = None
        self._login_cooldown = 0

    @asyncio.coroutine
    def connect(self):
        """|coro|
        Connect to ubisoft, automatically called when needed"""
        if time.time() < self._login_cooldown:
            raise FailedToConnect("login on cooldown")

        resp = yield from self.session.post("https://connect.ubi.com/ubiservices/v2/profiles/sessions", headers = {
            "Content-Type": "application/json",
            "Ubi-AppId": self.appid,
            "Authorization": "Basic " + self.token
        }, data=json.dumps({"rememberMe": True}))

        data = yield from resp.json()

        if "ticket" in data:
            self.key = data.get("ticket")
            self.sessionid = data.get("sessionId")
            self.uncertain_spaceid = data.get("spaceId")
        else:
            raise FailedToConnect

0 个答案:

没有答案