【问题标题】:I have a error in discord.py (purge command)我在 discord.py 中有错误(清除命令)
【发布时间】:2018-09-27 21:52:26
【问题描述】:

我有这个用于 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'

有人可以帮帮我吗?

【问题讨论】:

  • llimit 是一个字符串,你需要一个 int。 int(llimit)
  • @abccd 你能把带int的代码发给我吗?

标签: python python-3.x discord.py


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    purge 函数的limit 参数采用整数作为值

    尝试做这样的事情

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

    我不完全确定您要使用 content[10:].strip() 完成什么,但如果您尝试忽略命令的 !clean 部分,您使用的数字太大。 content[7:] 就足够了

    【讨论】:

    • 我的前缀是 shb!,唯一的错误是我忘记的 int...谢谢兄弟,请原谅任何英文错误
    【解决方案3】:

    还可以让机器人检查错误,您甚至可以这样做

    @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(f'Cleared by {ctx.author.mention}')
            await ctx.message.delete()
    
    @bot.event
    async def on_command_error(ctx, error):
        if isinstance(error, commands.MissingPermissions):
            await ctx.send("You cant do that!")
    

    同样这样它会消除每个命令的错误,这样会更好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多