discord.py problems with if not arg (clear command)

大城市里の小女人 提交于 2020-07-16 08:05:00

问题


I have a problem with if not arg (I skip it directly) and I understand that putting arg: int creates problems for me. Do you know a solution? I tried with many methods but I can't

Code:

@client.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def clear(ctx, arg: int):
    if not arg:
        embed = discord.Embed(
            color=discord.Colour.red()
        )
        embed.set_author(
            name="Specifica quante messaggi vuoi cancellare!",
            icon_url="https://cdn.discordapp.com/attachments/640563710104043530/730639329453670420/DuscePeppe_FRIULI.png"
        )
        await ctx.send(embed=embed, delete_after=10.0)
        return
    embed = discord.Embed(
        color=discord.Colour.green()
    )
    embed.set_author(
        name=f'Ho cancellato ufficialmente {arg} messaggi!',
        icon_url=f'{ctx.author.avatar_url}'
    )
    await ctx.channel.purge(limit=arg+1)
    await ctx.send(embed=embed, delete_after=10.0)
    embed = discord.Embed(
        color=discord.Colour.dark_gold()
    )
    embed.set_author(
        name=f'{ctx.author._user} ha cancellato {arg}',
        icon_url=f'{ctx.author.avatar_url}'
    )
    embed.add_field(
        name='Messaggi cancellati da:',
        value=f'{ctx.author._user}',
        inline=True
    )
    embed.add_field(
        name='Quantità:',
        value=f'{arg}',
        inline=True
    )
    channel = client.get_channel(729553772547932190)
    await channel.send(embed=embed)
@clear.error
async def clear_error(ctx, error):
    if isinstance(error, commands.CheckFailure):
        embed = discord.Embed(
            color=discord.Colour.red()
        )
        embed.set_author(
            name="Non ti è permesso cancellare i messaggi!",
            icon_url='https://cdn.discordapp.com/attachments/640563710104043530/730639329453670420/DuscePeppe_FRIULI.png'
        )
        await ctx.send(embed=embed, delete_after=10.0)

回答1:


If you don't specify a required argument while invoking a command, it will cause a MissingRequiredArgument error and it won't execute your function. You just have to add this error in your clear_error function like so:

@clear.error
async def clear_error(ctx, error):
    embed = discord.Embed(color=0xe74c3c)
    if isinstance(error, commands.CheckFailure):
        embed.set_author(
            name="You're not authorized to delete messages!",
            icon_url='your image'
        )  
    elif isinstance(error, commands.MissingRequiredArgument):
        embed.set_author(
            name="You need to specify how many messages you want to delete!",
            icon_url='your image'
        )
    await ctx.send(embed=embed, delete_after=10.0)

Also, you won't need your if not arg anymore



来源:https://stackoverflow.com/questions/62865285/discord-py-problems-with-if-not-arg-clear-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!