python - 如何从单个命令中获取多个值?
问题描述
我正在制作一个不和谐的机器人,当使用机器人命令搜索时,它会返回卡片的图像。我能够发出命令来获取一张卡片,但我希望能够使用一个命令搜索多张卡片。我怎样才能做到这一点?
这是我的单一搜索代码:
@bot.command(brief = "Fetch a card.", description = "Use this command to search for a card.")
async def card(ctx, *, search):
try:
result = scrython.cards.Named(fuzzy=search)
except scrython.ScryfallError as e:
error = (str(e.error_details['details']))
await ctx.send(error)
else:
result = result.image_uris(image_type = 'large')
await ctx.send(result)
解决方案
discord.py 支持贪婪的位置参数语法(VAR_POSITIONAL)*searches
,然后添加require_var_positional=True
以指定用户需要输入至少一个搜索。
@bot.command(require_var_positional=True)
async def card(ctx, *searches):
... # searches will be a tuple of search terms
示例(前缀!
):
!card first
然后searches = ('first', )
!card first second
然后searches = ('first', 'second')
!card
错误!card one "two words" "three words here"
然后searches = ('one', 'two words', 'three words here')
推荐阅读
- pandas - 如何使用与 hadoop spark parquet 相同的标头保存带有 pandas 的镶木地板?
- image - 向 Vuetify v-card 添加渐变:img
- string - Scala得到一个子字符串
- javascript - 如何为 vanillajs 待办事项列表应用程序创建删除功能?
- python - Tensorflow fit ValueError:形状不匹配:标签的形状(收到(16640,))应该等于除最后一个维度之外的logits的形状
- python - 如何处理参数是字符串但我们输入字节类型
- javascript - React - 如何使用路线导航到下一个弹出视图?
- outlook - Outlook 无法使用 pywin32 解析来自 html 的多个表
- javascript - 赛普拉斯测试运行程序通过在 circleCI 中带有信号 SIGSEGV 的退出事件意外退出
- android - 任务 ':app:processDebugResources' 执行失败。在 mac m1 上