首页 > 解决方案 > 如何从单个命令中获取多个值?

问题描述

我正在制作一个不和谐的机器人,当使用机器人命令搜索时,它会返回卡片的图像。我能够发出命令来获取一张卡片,但我希望能够使用一个命令搜索多张卡片。我怎样才能做到这一点?

这是我的单一搜索代码:

@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)

标签: pythondiscord.py

解决方案


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')

推荐阅读