我在 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'

这里有人可以帮助我吗?

原文由 Shiba Luck 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 387
2 个回答

为了声明参数,您可以将单个参数可调用对象(如 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!")

原文由 Patrick Haugh 发布,翻译遵循 CC BY-SA 4.0 许可协议

这是我能做到的最简单的方法,希望对您有所帮助!

 @client.command(pass_context=True)
async def purge(ctx, limit: int):
    await ctx.message.delete()
    await ctx.channel.purge(limit=limit)

原文由 ShinyDragon96 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题