I have a error in discord.py (purge command)

我们两清 提交于 2020-08-10 23:10:56

问题


I have this Python code for discord.py rewrite:

@bot.command(pass_context=True)
async def clean(ctx):
    if ctx.author.guild_permissions.administrator:
            llimit = ctx.message.content[10:].strip()
            await ctx.channel.purge(limit=llimit)
            await ctx.send('Cleared by <@{.author.id}>'.format(ctx))
        await ctx.message.delete()
else:
    await ctx.send("You cant do that!")

But every time i get this error:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: '<=' not supported between instances of 'str' and 'int'

Can someone here help me?


回答1:


You can treat single argument callables (like int) like converters for the purpose of declaring arguments. I also changed your permissions check to be handled automatically by a commands.check

@bot.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def clean(ctx, limit: int):
        await ctx.channel.purge(limit=limit)
        await ctx.send('Cleared by {}'.format(ctx.author.mention))
        await ctx.message.delete()

@clean.error
async def clear_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You cant do that!")



回答2:


The limit argument of the purge function takes an integer as a value

Try doing something like this

@bot.command(pass_context=True)
async def clean(ctx):
    if ctx.author.guild_permissions.administrator:
            llimit = ctx.message.content[10:].strip()
            await ctx.channel.purge(limit=int(llimit))
            await ctx.send('Cleared by <@{.author.id}>'.format(ctx))
        await ctx.message.delete()
else:
    await ctx.send("You cant do that!")

I'm not entirely sure what you're trying to accomplish with content[10:].strip() but if you're trying to ignore the !clean part of your command you are using too large of a number. content[7:] would suffice



来源:https://stackoverflow.com/questions/52545638/i-have-a-error-in-discord-py-purge-command

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