не ожидая ценности ни от кого - PullRequest
0 голосов
/ 05 мая 2020

Я новичок в кодировании в python, и я пытаюсь создать сообщение «Player not found», если ответ api содержит «Malformed UUID»

Вот что возвращает недопустимый uuid игрока

{"success": false, "cause": "Malformed UUID"}

Я получаю эту ошибку в консоли, когда ввожу недопустимый uuid:

Traceback (most recent call last):
  File "C:\Users\Deagan'\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\Deagan'\Desktop\HyBot\bot.py", line 30, in status
    onlinestatus = hypixelcustomwrapper.get_session(name)
  File "C:\Users\Deagan'\Desktop\HyBot\hypixelcustomwrapper.py", line 34, in get_session
    data = res.json()
  File "C:\Users\Deagan'\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\models.py", line 898, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\Deagan'\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Deagan'\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Deagan'\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Deagan'\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Deagan'\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Deagan'\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Здесь это код bot.py:

@bot.command()
async def status(ctx, name):
    onlinestatus = hypixelcustomwrapper.get_session(name)


    if onlinestatus == False:
        await ctx.send("Player not online.")


    else:
        game_type = hypixelcustomwrapper.get_gametype(name)
        game_mode = hypixelcustomwrapper.get_gamemode(name)
        await ctx.send(f"Online: True" + "\n" + f"Game: {game_type}" + "\n" + f"Mode: {game_mode}")

Вот код api:

def get_session(name):
    url1 = f"https://api.mojang.com/users/profiles/minecraft/{name}"

    res = requests.get(url1)
    data = res.json()
    if data["id"] is None:
        return None
    returnUuid = (data["id"])

    url2 = f"https://api.hypixel.net/status?key={API_KEY}&uuid=" + returnUuid

    res = requests.get(url2)
    data = res.json()
    if data["session"] is None:
        return None


    onlinestatus = (data["session"]["online"])

    if onlinestatus is None:
        return None
    else:
        return onlinestatus

Как я могу исправить ошибку и как проверить текст «Malformed UUID» в ответе api?

спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...