Discord机器人接受多个单词作为参数

时间:2020-09-05 07:58:16

标签: python discord

因此,我正在制作一个能够警告用户的机器人。我想知道是否有一种方法可以使它在“ reason”参数中接受一个以上的单词,而不必写引号。

@client.command(name = "warn")
@commands.has_role("staff")
async def warn(ctx, member: discord.Member, reason = "No reason given"):
    publiclog = client.get_channel("channel id")
    embedVar = discord.Embed(title=":warning: Warning", description=f"{member} has been warned", colour = discord.Colour.orange())
    embedVar.add_field(name="Reason", value= reason, inline=True)
    embedVar.add_field(name="Warned by:", value= f"{ctx.author}", inline=True)
    embedVar.set_thumbnail(url=member.avatar_url)
    await ctx.send(f"<@{member.id}> has been warned. Reason: {reason}")
    await publiclog.send(embed=embedVar)

因此,现在,如果工作人员调用该函数并且他们写了>warn @user they were mean,则机器人只会将原因写为“他们”。有没有一种方法可以使它写出全部原因,而不必写>warn @user "they were mean"

2 个答案:

答案 0 :(得分:0)

async def warn(ctx, member: discord.Member, *, reason = "No reason given"):

我认为,如果您添加*,,它将起作用。

答案 1 :(得分:0)

@client.command(name = "warn")
@commands.has_role("staff")
async def warn(ctx, member: discord.Member, *reason = "No reason given"):
    reason = " ".join(reason)
    publiclog = client.get_channel("channel id")
    embedVar = discord.Embed(title=":warning: Warning", description=f"{member} has been warned", colour = discord.Colour.orange())
    embedVar.add_field(name="Reason", value= reason, inline=True)
    embedVar.add_field(name="Warned by:", value= f"{ctx.author}", inline=True)
    embedVar.set_thumbnail(url=member.avatar_url)
    await ctx.send(f"<@{member.id}> has been warned. Reason: {reason}")
    await publiclog.send(embed=embedVar)

添加*将接受任意数量的参数,而加入它们将使其成为字符串