Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 无法获取client.command参数以按discord.py中的键值分析API响应_Python 3.x_Discord_Discord.py_Discord.py Rewrite_Aiohttp - Fatal编程技术网

Python 3.x 无法获取client.command参数以按discord.py中的键值分析API响应

Python 3.x 无法获取client.command参数以按discord.py中的键值分析API响应,python-3.x,discord,discord.py,discord.py-rewrite,aiohttp,Python 3.x,Discord,Discord.py,Discord.py Rewrite,Aiohttp,我正在现有的bot上构建一个命令,该bot将搜索一个API,并将棒球运动员的姓名作为参数来查询json响应。我已经在测试中使所有内容都正常工作,只是在我的一生中,我不知道如何将结果限制为仅包含在discord中调用命令时传递的查询参数的结果 例如:用户将键入!卡片Adam Dunn,只有键“name”的值“Adam Dunn”将返回。目前,无论为参数键入了什么,都会发送整个结果的第一页,并且随着嵌入逻辑的运行,每个结果都会得到一个单独的嵌入,这并不理想 为了可读性,我只包含了相关的代码行,没有包

我正在现有的bot上构建一个命令,该bot将搜索一个API,并将棒球运动员的姓名作为参数来查询json响应。我已经在测试中使所有内容都正常工作,只是在我的一生中,我不知道如何将结果限制为仅包含在discord中调用命令时传递的查询参数的结果

例如:用户将键入!卡片Adam Dunn,只有键“name”的值“Adam Dunn”将返回。目前,无论为参数键入了什么,都会发送整个结果的第一页,并且随着嵌入逻辑的运行,每个结果都会得到一个单独的嵌入,这并不理想

为了可读性,我只包含了相关的代码行,没有包含结果的大量嵌入

它一定是非常简单的东西,但我想我只是盯着它看了太久而看不见它。任何帮助都将不胜感激,谢谢

以下是运行命令时的控制台输出:

以下是我目前正在使用的代码:

async def card(ctx, *, player_name: str):
    async with ctx.channel.typing():
        async with aiohttp.ClientSession() as cs:
            async with cs.get("https://website.items.json") as r:
                data = await r.json()
                listings = data["items"]
                for k in listings:
                    if player_name == k["name"]
                        print()```

我希望我对你的理解是正确的。如果用户没有给出
player\u名称
,则您将继续不搜索任何内容,如果没有给出
player\u名称
,则您希望结束搜索。如果是这样的话

player\u name:str=None
的默认值设置为
None
,然后在代码开头检查是否有

async def card(ctx, *, player_name: str=None):
    if not player_name:
        return await ctx.send('You must enter a player name')

    # if there is a name do this
    async with ctx.channel.typing():
        async with aiohttp.ClientSession() as cs:
            async with cs.get("https://theshownation.com/mlb20/apis/items.json") as r:
                data = await r.json()
                listings = data["items"]
                for k in listings:
                    if player_name == k["name"]
                        print()```
更新:

我是个白痴。工作正常,但由于我搜索的
player\u name
不在结果的第一页,因此没有显示。当使用API结果第一页上的
player_name
时,它工作正常


这是一个分页问题,而不是键值问题。

你提出了一个很好的观点,如果没有玩家名称匹配,我肯定应该返回一些内容。我当然会按照你的建议添加一些逻辑。我遇到的更大问题是使用用户输入的
player\u name
在json响应中搜索
k[“name”]