【问题标题】:discord '>' not supported between instances of 'Context' and 'int''Context' 和 'int' 的实例之间不支持 discord '>'
【发布时间】:2021-03-28 13:39:04
【问题描述】:

当我尝试运行命令时,我总是收到以下错误消息:“TypeError: '>' not supported between 'Context' and 'int'”

from discord.ext import commands

class clear(commands.Cog):

    def __init__(self, client):
        self.client = client

# Events

    @commands.Cog.listener()
    async def on_ready(self):
        print('Clear modul started')

# Command

    @commands.command()
    async def clear(ctx, amount=5, max=100, min=1):
        if (amount > max):
            await ctx.send(f"The value is too high! Enter a value `{min}-{max}` between.", delete_after=5)
        elif (amount < min):
            await ctx.send(f"The value is too small! Enter a value `{min}-{max}` between.", delete_after=5)
        else: 
            await ctx.channel.purge(limit=amount+1)
            await ctx.send(f"Törölve {amount}", delete_after=5)

def setup(client):
    client.add_cog(clear(client))```

【问题讨论】:

  • 导致此错误的调​​用是什么?我可以看到发生的情况是您传递了一个“上下文”对象作为数量。
  • 你缺少自我
  • 我不知道。该命令在 main.py 中有效,但是当我将其传输到单独的文件时,我得到了这个。

标签: python if-statement discord integer discord.py


【解决方案1】:

正如我在 cmets 中所说,您在命令中缺少 self

@commands.command()
async def clear(self, ctx, amount=5, max=100, min=1):
   ...

【讨论】:

  • 确实如此。在一个类的每个函数中,你应该传递一个“self”。
  • 并非类中的所有函数都采用 self 参数 @electromeow - classmethods、staticmethods 和 __new__ 方法不采用它
  • 我知道,但我的意思是几乎所有这些。但是感谢您的评论!
【解决方案2】:

您应该像其他答案所说的那样传递一个“自我”参数。 但我也建议您在函数的代码块中定义 min 和 max 变量,而不是在函数的参数中。 因为人们可以将最小值更改为另一个值。在这种情况下,参数将是一个字符串。您没有将它们转换为整数,因此会出错。 永远不要相信你的用户(;

【讨论】:

    猜你喜欢
    • 2020-05-19
    • 2021-08-10
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 2018-09-03
    • 2020-11-13
    • 2018-03-27
    相关资源
    最近更新 更多