如何在命令中包含多个参数 (discord.py)

时间:2021-04-15 04:42:02

标签: discord.py

我无法得到很多参数,我正在尝试制作一个嵌入生成器,这样我就可以立即发送嵌入,而无需一遍又一遍地调试。

@client.command()
async def embed(ctx, arg1, arg2, arg3, arg4, arg5):
     embed=discord.Embed(title="{}".format(arg1), description="{}".format(arg2), color=0xff0000)
     embed.set_author(name="{}".format(arg3))
     embed.add_field(name="{}".format(arg4), value="{}".format(arg5), inline=True)
     embed.set_footer(text=f"Requested by {ctx.author.name}")
     await ctx.send(embed=embed)

1 个答案:

答案 0 :(得分:0)

使用命令,您可以拥有任意数量的位置参数。每个由空格分隔的值将作为位置参数传递给命令函数。 (在调用命令时,引用输入以传递包含空格的参数。)命令函数只能采用一个仅关键字参数。在调用时,任何未作为位置参数传递给函数的输入都会作为关键字参数传递。


@client.command()
async def embed(ctx, title, description, author, name, value):
    embed = discord.Embed(title=title, description=desc)
    embed.set_author(name=author)
    embed.add_field(name=name, value=value, inline=True)
    embed.set_footer(text=f"Requested by {ctx.author.name}")
    await ctx.channel.send(embed=embed)

对于您的命令(只有位置参数),一切都应该正常工作。示例调用:

!embed Title "This is a sample description" Author Name "Field value which corresponds to 'name'"

对于此调用,embed() 的参数如下:

  • title="Title"
  • description="This is a sample description"
  • author="Author"
  • name="Name"
  • value="Field value which corresponds to 'name'"

注意所有位置参数必须在调用时传递,否则会抛出错误。这可以通过为每个参数设置默认值来避免(例如 async def embed(ctx, title="Embed Title", ...)


查看 discord.ext.commands framework 的文档。