【问题标题】:How do I put multiple words in one argument discord.py如何在一个参数中放入多个单词 discord.py
【发布时间】:2019-09-04 00:33:28
【问题描述】:

我正在开发一个使用 discord.py 的机器人,我想要一个命令来设置机器人正在玩的游戏,但我不知道如何创建允许空格的参数。

我尝试提出 2 个参数,但如果你想要 一个 字,它会显示为错误。


@client.command()
async def game(gameplay):
    #do things

我希望参数“gameplay”中包含多个单词。有人可以帮忙吗?

【问题讨论】:

标签: discord.py


【解决方案1】:
@client.command()
async def game(ctx, *, args):
    ctx.send(args)

【讨论】:

  • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation 将通过展示为什么这是解决问题的好方法,并使其对有其他类似问题的未来读者更有用,从而大大提高其长期价值。请edit您的回答添加一些解释,包括您所做的假设。
【解决方案2】:
@client.command()
async def game(ctx, *args):
    # args contains all arguments written after the command i.e !game game i want to play
    # print(" ".join(args[:])) will print "game i want to play"

正如您在示例中看到的,*args 将包含在命令之后写入的所有内容。 ctx 将是上下文。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 2019-07-13
    • 2021-03-19
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多