Python 使用用户提供的参数discord运行我的clear命令会导致TypeError:'<=';在';str';和';int';

Python 使用用户提供的参数discord运行我的clear命令会导致TypeError:'<=';在';str';和';int';,python,discord.py,Python,Discord.py,每当我试图运行我的代码,该代码应该清除用户以给定数字作为参数的消息时,都会给出此错误,但如果代码以给定参数为参数,则会正常工作 错误: Ignoring exception in command clear: Traceback (most recent call last): File "C:\Users\*****\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\c

每当我试图运行我的代码,该代码应该清除用户以给定数字作为参数的消息时,都会给出此错误,但如果代码以给定参数为参数,则会正常工作

错误:

    Ignoring exception in command clear:
    Traceback (most recent call last):
      File "C:\Users\*****\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
        ret = await coro(*args, **kwargs)
      File "C:\Users\*****\Desktop\bot_discord.py", line 29, in clear
        await ctx.channel.purge(limit=amount)
      File "C:\Users\*****\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\channel.py", line 372, in purge
        msg = await iterator.next()
      File "C:\Users\*****\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\iterators.py", line 262, in next
        await self.fill_messages()
      File "C:\Users\*****\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\iterators.py", line 307, in fill_messages
        if self._get_retrieve():
      File "C:\Users\*****\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\iterators.py", line 273, in _get_retrieve
        elif l <= 100:
    TypeError: '<=' not supported between instances of 'str' and 'int'
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "C:\Users\*****\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
        await ctx.command.invoke(ctx)
      File "C:\Users\*****\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
        await injected(*ctx.args, **ctx.kwargs)
      File "C:\Users\*****\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
        raise CommandInvokeError(exc) from exc
    discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: '<=' not supported between instances of 'str' and 'int'

你的代码绝对正确。但如果您输入all作为参数,它将不起作用。要解决此问题,可以使用以下方法

@client.command()
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount: int=0, all:str = ""): 
    if all == "all":
        await ctx.channel.purge(limit=10000000000000000000000000000000000)
        await ctx.send("Cleared the entire chat!")
        print("Cleared the chat!")
    else:
        await ctx.channel.purge(limit=amount)
        await ctx.send("Done!")
        print(f"Cleared {amount} messages!")
这将起作用,它没有任何必需的参数,即使您键入
!清除
它不会给出错误。祝你的机器人好运。干杯

编辑: 对于不理解错误的任何人,它告诉您不能将限制设置为字符串。因为在上面的命令中,当您执行
else
并且参数值为'abc'时,它将给出一个错误,即它无法对字符串执行整数函数

@client.command()
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount: int=0, all:str = ""): 
    if all == "all":
        await ctx.channel.purge(limit=10000000000000000000000000000000000)
        await ctx.send("Cleared the entire chat!")
        print("Cleared the chat!")
    else:
        await ctx.channel.purge(limit=amount)
        await ctx.send("Done!")
        print(f"Cleared {amount} messages!")