首页 > 解决方案 > 应用于 Discord 机器人的 API 需要多个参数

问题描述

我一直在用 Discord.py 制作一个 Discord 机器人,并将多个 API 应用于该机器人。特别是在这个(https://lyricsovh.docs.apiary.io/#)中,API 要求用户同时输入艺术家/乐队名称和歌曲名称。我以前的 API 没有任何问题,但在这种情况下,我完全迷路了。如您所见,变量“搜索”应包含艺术家和歌曲。我怎样才能让它工作?这是我的代码。

@commands.command()
    async def lyrics(self,ctx,*,search):
        r = requests.get(f'https://api.lyrics.ovh/v1{search}')
        if r.status_code == 200:
            l_response = json.loads(r.content)
            try:
                lyric = l_response["lyrics"]
                await ctx.send(f'Here are the lyrics:\n{lyric}')
            except:
                await ctx.send(f'Lyrics not found.')

标签: python-3.xbotsdiscord.py

解决方案


@commands.command()
    async def lyrics(self,ctx,*,artist, title):
        r = requests.get('https://api.lyrics.ovh/v1/{}/{}'.format(artist, title))
        if r.status_code == 200:
            l_response = json.loads(r.content)
            try:
                lyric = l_response["lyrics"]
                await ctx.send(f'Here are the lyrics:\n{lyric}')
            except:
                await ctx.send(f'Lyrics not found.')

推荐阅读