Python 3.x Discord.py将参数添加到api命令

Python 3.x Discord.py将参数添加到api命令,python-3.x,discord,discord.py-rewrite,Python 3.x,Discord,Discord.py Rewrite,我需要在API的城市词典(用于搜索定义)和开放天气图(用于获取特定位置的天气)的API命令中添加参数的帮助。我了解到,其中很多都提供了查询字符串的代码(“get”,url,headers=headers,params=querystring)但我不明白如何允许像$urban yo这样的东西 @commands.command( name="urban", description="Allows the user to get a de

我需要在API的城市词典(用于搜索定义)和开放天气图(用于获取特定位置的天气)的API命令中添加参数的帮助。我了解到,其中很多都提供了查询字符串的代码(“get”,url,headers=headers,params=querystring)但我不明白如何允许像$urban yo这样的东西

@commands.command(
        name="urban",
        description="Allows the user to get a definition from Urban Dictionary",
        aliases=['urbandict']
    )
    async def urban(self, ctx):
        url = "https://mashape-community-urban-dictionary.p.rapidapi.com/define"

        headers = {
            'x-rapidapi-key': self.bot.quote_api_key,
            'x-rapidapi-host': "mashape-community-urban-dictionary.p.rapidapi.com"
        }

        async with ClientSession() as session:
            async with session.get(url, headers=headers) as response:
                r = await response.json()
                # print(r)
                embed = discord.Embed(title="Term:", description=f"{r['']}")

                embed.add_field(name="Definition:", value=f"||{r['']}||")

                embed.set_author(name=ctx.author.display_name, icon_url=ctx.message.author.avatar_url)

                await ctx.send(embed=embed)
    
查看,
querystring
必须是具有
术语
键的词汇表。
然后,要向命令添加参数,只需向函数添加一个参数,
discord.py
将自动解析命令。如果希望在单个参数中包含
$urban
之后的所有内容,则必须在
术语
参数之前添加
*

它看起来是这样的:

@commands.command()
异步def市区(自身、ctx、*、术语):
url=”https://mashape-community-urban-dictionary.p.rapidapi.com/define"
querystring={“term”:term}
标题={
“x-rapidapi-key”:self.bot.quote\u api\u key,
“x-rapidapi-host”:“mashape社区城市词典.p.rapidapi.com”
}
与ClientSession()作为会话异步:
与session.get(url,headers=headers,params=querystring)异步作为响应:
r=wait response.json()
embed=discord.embed(title=“Term:”,description=f“{r['']}”)
embed.add_字段(name=“Definition:”,value=f“| |{r['']}| |”)
embed.set\u author(name=ctx.author.display\u name,icon\u url=ctx.message.author.avatar\u url)
等待ctx.send(嵌入=嵌入)
对于开放天气图API,您需要在url中传递参数。例如,如果您想要巴黎的5天预测,您必须使用以下url:

http://api.openweathermap.org/data/2.5/forecast?q=Paris&units=metric&APPID=your_token
有关更多信息,您可以查看以下文档:


我真的非常感谢您的帮助,因为还有一个问题,我正试图通过嵌入传递结果,正如您所看到的,但它得到的是嵌入不能超过2048个字符,我已经尝试通过f“{r['list']['definition']}将列表传递到定义”但是它得到了TypeError:字符串索引必须是整数查看API,
r['list']
是一个定义列表,因此需要指定索引。例如,如果您想要第一个定义,您必须键入
r['list'][0]['definition']
非常感谢您,先生