我正在使用 discord.py 開發一個機器人,但我有一個無法解決的錯誤。我不認為后者來自 discord.py 而是來自 python(我是這方面的新手)
我的問題:我在“if”中測驗第一個條件。如果這是真的,我會用另一個“if”測驗其他條件。另一方面,如果第一個“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)
它回傳我: 

uj5u.com熱心網友回復:
你這里有錯誤的邏輯:
if categorie == "moderation" or "Moderation" or "mod":
此條件始終為 True。將其替換為
if categorie in ("moderation", "Moderation", "mod"):
也為您的其他if運營商做這件事。
但我建議您使用discord.py 中的HelpCommandclass創建幫助命令。它更簡單,也是創建幫助命令的最佳方式。你可以在這里閱讀它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/373031.html
上一篇:滿足條件時處理兩種情況?
