簡體   English   中英

有沒有辦法從其他鍵內的鍵獲取值?

[英]Is there any way of getting values from keys inside other keys?

(首先發帖抱歉,如果我做錯了)所以我正在為我和我的朋友使用discord.py制作機器人(不和諧)(因為python是有史以來最簡單的代碼)而且我遇到了這個。 我需要從鍵INSIDE OTHER鍵獲取值。 我該怎么做呢?

所以我試圖將res更改為res.text和res.json以及res.content,我只能找到“數據”但不能找到我需要的“id”,“name”或“description”。

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import requests, json
import asyncio

Client = discord.Client()
client = commands.Bot(command_prefix='?')
@client.event
async def on_ready():
    print('started')

@client.command()
async def findfriends(ctx,userid):
        res = requests.get("https://friends.roblox.com/v1/users/"+userid+"/friends")
        var = json.loads(res.text)
        def a(a):
                ID = a['id']
                return ID
        def b(b):
                Name = b['name']
                return Name
        def c(c):
                description = c['description']
                return description
        data = var['data'] #I can get this working
        print(data)
        #cv = data['name'] #but this wont work
        #id = a(var)       #nor this
        #name = b(var)     #nor this
        #desc = c(var)     #nor this
        #await ctx.send("\nID: " + id + "\nName: " + name + "\nDesc: " + desc) # this is just sending the message

client.run(BOT TOKEN HERE) #yes i did indeed add it but just for the question i removed it

正如我在代碼中所說,我只能使“數據”工作而不是id,name或desc。 對於id name和desc,它只會拋出一個錯誤

Ignoring exception in command findfriends:
Traceback (most recent call last):
  File "C:\Users\Calculator\PycharmProjects\ryhrthrthrhrebnfbngfbfg\venv\lib\site-packages\discord\ext\commands\core.py", line 79, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:/Users/Calculator/PycharmProjects/ryhrthrthrhrebnfbngfbfg/a.py", line 277, in findfriends
    id = a(var)       #nor this
  File "C:/Users/Calculator/PycharmProjects/ryhrthrthrhrebnfbngfbfg/a.py", line 266, in a
    ID = a['id']
KeyError: 'id'

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

Traceback (most recent call last):
  File "C:\Users\Calculator\PycharmProjects\ryhrthrthrhrebnfbngfbfg\venv\lib\site-packages\discord\ext\commands\bot.py", line 863, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Calculator\PycharmProjects\ryhrthrthrhrebnfbngfbfg\venv\lib\site-packages\discord\ext\commands\core.py", line 728, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Calculator\PycharmProjects\ryhrthrthrhrebnfbngfbfg\venv\lib\site-packages\discord\ext\commands\core.py", line 88, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'id'

Ignoring exception in command findfriends:
Traceback (most recent call last):
  File "C:\Users\Calculator\PycharmProjects\ryhrthrthrhrebnfbngfbfg\venv\lib\site-packages\discord\ext\commands\core.py", line 79, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:/Users/Calculator/PycharmProjects/ryhrthrthrhrebnfbngfbfg/a.py", line 274, in findfriends
    data = var['data']['id'] #I can get this working
TypeError: list indices must be integers or slices, not str

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

Traceback (most recent call last):
  File "C:\Users\Calculator\PycharmProjects\ryhrthrthrhrebnfbngfbfg\venv\lib\site-packages\discord\ext\commands\bot.py", line 863, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Calculator\PycharmProjects\ryhrthrthrhrebnfbngfbfg\venv\lib\site-packages\discord\ext\commands\core.py", line 728, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Calculator\PycharmProjects\ryhrthrthrhrebnfbngfbfg\venv\lib\site-packages\discord\ext\commands\core.py", line 88, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: list indices must be integers or slices, not str

https://friends.roblox.com/v1/users/<userid>/friends端點返回用戶擁有的所有朋友的列表,其大小可以不同。

使用var = json.loads(res.text)您將響應文本加載到json對象中,該對象包含您使用data = var['data']訪問的密鑰data 新的data變量現在包含一個列表對象,這就是cv = data['name']無法工作的原因,因為列表對象不將字符串作為鍵,使用整數訪問它們。

您需要遍歷列表以獲取有關朋友的所有信息。 下面的代碼遍歷列表,為列表中的每個項目提取信息,並在完成所有項目后發送信息消息。

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import requests, json
import asyncio

client = commands.Bot(command_prefix='?')
@client.event
async def on_ready():
    print('started')

@client.command()
async def findfriends(ctx,userid):
    res = requests.get("https://friends.roblox.com/v1/users/"+userid+"/friends")
    var = json.loads(res.text)
    data = var['data']
    print(data)

    friends_msg = 'Friends information:'
    for friend in data:
        id = friend['id']
        name = friend['name']
        desc = friend['description']
        friends_msg = friends_msg + "\nID: " + id + "\nName: " + name + "\nDesc: " + desc

    await ctx.send(friends_msg)

client.run(BOT TOKEN HERE)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM