如果我执行-ban [user] [原因],则可以禁止用户,但是如果我仅执行-ban [user],则不会禁止用户。我怎样才能解决这个问题?

时间:2019-04-21 18:57:49

标签: discord.py

这是代码

@client.command(pass_context = True)
async def ban(ctx, user: discord.User,*, bugs: str):
    if ctx.message.author.server_permissions.administrator or ctx.message.author.id == '562000458181246982':
        embed = discord.Embed(title="", description="{} has been banned.".format(user.name), color=0x0072ff)
        embed.add_field(name="Reason", value=bugs)
        embed.set_author(name="Alpha Moderation", icon_url="https://media.discordapp.net/attachments/562005351353024525/569168417857208320/RealAlpha.png?width=355&height=356")
        utc_dt = datetime.now(timezone.utc)
        p = utc_dt.strftime('     Time - %H:%M:%S | Date - %d/%m/%Y')
        utc = str(    p)    
        txt=str(utc)
        embed.set_footer(text=txt)
        await client.say(embed=embed)
        xd = ctx.message.server.name
        embed = discord.Embed(title="", description="You have been banned from: " +xd, color=0x495c66)
        embed.add_field(name="Action", value="Ban")
        embed.add_field(name="Reason", value=bugs)
        embed.set_author(name="Alpha Moderation", icon_url="https://media.discordapp.net/attachments/562005351353024525/569168417857208320/RealAlpha.png?width=355&height=356")
        utc_dt = datetime.now(timezone.utc)
        p = utc_dt.strftime('       %H:%M:%S • %d/%m/%Y  ')
        utc = str(    p)    
        a=ctx.message.author
        txt= str(a) + " | " + str(utc)
        embed.set_footer(text="Banned by: " +txt)
        await client.send_message(user, embed=embed)
        await client.ban(user)
    else:
        embed=discord.Embed(title="Permission Denied.", description="<:alphaError:569178991349465088> You don't have permission to use this command.", color=0xEA1025)
        await client.say(embed=embed)

https://media.discordapp.net/attachments/562005351353024525/569598677925232651/unknown.png?width=581&height=345

1 个答案:

答案 0 :(得分:0)

由于Python要求将所有参数都绑定到一个值,所以当您不将值传递给bugs参数时,函数将缺少必需的参数。

要解决此问题,您可以像这样设置bugs参数的默认值:

@client.command(pass_context=True)
async def ban(ctx, user: discord.User, *, bugs: str = None):
    if bugs is None:
        # do something when reason is not provided
        # e.g. set some default reason value
        bugs = "Preemptive ban"
    # rest of your code