如果我做-ban [user] [reason],我可以禁止用户,但如果我只做-ban [user],它就不会禁止用户。我怎样才能解决这个问题?

问题描述 投票:0回答:1

这是代码

@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

discord.py
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.