【问题标题】:Why i've this error in my discord.py code为什么我的 discord.py 代码中有这个错误
【发布时间】:2022-01-10 11:36:51
【问题描述】:

我正在使用 discord.py 开发一个机器人,但我有一个无法解决的错误。我不认为后者来自 discord.py 而是来自 python(我是这方面的新手)

我的问题: 我在“如果”中测试第一个条件。 如果这是真的,我会用另一个“如果”来测试其他条件。 另一方面,如果第一个“if”返回 false,我用“else”测试其他条件,然后在这个条件中使用“if”。

在我的第一个“if”中,如果为真,所有后续的“if”语句都会被执行,即使它们不应该执行。我对 else 中的“if”也有同样的问题。

一个shema:

if ...:
   if ...:
        ...
   if ...:
        ...
else:
   if ...:
        ...
   if ...:
        ...

我的真实代码:

@commands.command()
async def help(self, ctx, categorie = None)
        if categorie == None:

            await ctx.send(f"Mon préfixe sur ce serveur: `{pref}`\n\nSur quelle partie du bot voulez vous de l'aide ?\n`utile` | `fun` | `recherches` | `moderation` | `creator`")

            def checkMessage(message):
                return message.author == ctx.message.author and ctx.message.channel == message.channel

            partie = await self.client.wait_for("message", timeout = 20, check = checkMessage)
            if partie.content == "moderation" or partie.content == "Moderation":
                await ctx.send(embed=modembed)
            if partie.content == "utile" or partie.content == "Utile":
                await ctx.send(embed=utileembed)
            if partie.content == "fun" or partie.content == "Fun":
                await ctx.send(embed=funembed)
            if partie.content == "recherches" or partie.content == "Recherches":
                await ctx.send(embed=recherchesembed)
            if partie.content == "creator" or "Creator":
                creatorembed.set_thumbnail(url="https://zupimages.net/up/20/52/qpa0.png")
                await ctx.send(embed=creatorembed)
        else:
            if categorie == "moderation" or "Moderation" or "mod":
                await ctx.send(embed=modembed)
            if categorie == "utile" or "Utile" or "util":
                await ctx.send(embed=utileembed)
            if categorie == "fun" or "Fun":
                await ctx.send(embed=funembed)
            if categorie == "recherches" or "recherche" or "Recherches" or "Recherche" or "rech" or "Rech":
                await ctx.send(embed=recherchesembed)
            if categorie == "creator" or "Creator":
                    creatorembed.set_thumbnail(url="https://zupimages.net/up/20/52/qpa0.png")
                    await ctx.send(embed=creatorembed)

它返回给我:

【问题讨论】:

    标签: python if-statement discord.py conditional-statements


    【解决方案1】:

    你的逻辑是错误的:

    if categorie == "moderation" or "Moderation" or "mod":
    

    此条件始终为真。替换为

    if categorie in ("moderation", "Moderation", "mod"):
    

    也为您的其他 if 运营商这样做。

    但我建议您使用 discord.py 中的HelpCommand 类创建帮助命令。它更简单,是创建帮助命令的最佳方式。你可以阅读它here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 2012-07-17
      • 1970-01-01
      相关资源
      最近更新 更多