python-3.x - 应用于 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.')
解决方案
@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.')
推荐阅读
- parsing - 使用自定义分隔符进行 Haskell 日期解析
- apache-spark - 在 PySpark UDF 中使用 StructType 列
- workflow - Uber Cadence 工作流程版本控制
- c - 线程永远不会获得锁 (pthread_mutex_lock)
- javascript - 从开关盒中检索数据
- python - pyqt5_tools Designer.exe 不存在
- javascript - 使用 JavaScript 删除引导表行
- angular - 将常量值的子集传递给角度分量
- firebase - Flutter & Firestore:如何首先获得最新的?
- python - 如何将 numpy 数组转换为 pandas 数据框?