首页 > 解决方案 > 我在 discord.py 中有错误(清除命令)

问题描述

我有这个用于 discord.py 重写的 Python 代码:

@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!")

但每次我得到这个错误:

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

这里有人可以帮助我吗?

标签: pythonpython-3.xdiscord.py

解决方案


为了声明参数,您可以将单参数可调用对象(如int)视为转换器。我还将您的权限检查更改为由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!")

推荐阅读