Структура моего JSON:
{"name": {"code": "6kkUEC6XA", "invites": 0}, "name2": {"code": "HTTPsC5IX", "invites": 0}, "name3": {"code": "Z26U6AE2B", "invites": 0}}
Мне нужно сравнить значение каждого элемента "code" с аргументом команды.
Я пробовал сделать это через цикл:
@bot.command()
async def c(ctx, *, code):
with open('referal.json','r', encoding='utf-8') as f:
ref = json.load(f)
for i in ref["code"]:
if code == ref[i]['code']:
await print("YES")
else:
await print("NO")
Получаю ошибку об исключении:
Ignoring exception in command c:
Traceback (most recent call last):
File "C:\Users\HTTPs\AppData\Local\Programs\Python\Python37-32\lib\site-packag
s\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "bot.py", line 489, in c
for i in ref["code"]:
KeyError: 'code'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\HTTPs\AppData\Local\Programs\Python\Python37-32\lib\site-packag
s\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\HTTPs\AppData\Local\Programs\Python\Python37-32\lib\site-packag
s\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\HTTPs\AppData\Local\Programs\Python\Python37-32\lib\site-packag
s\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Ke
Error: 'code'
Структуры JSON я изменить не могу, и других способов я не вижу. Как можно сделать такой перебор элементов?
for i in ref:
.