discord.py 我检查是否有人对 wait_for 做出反应

时间:2021-07-11 11:20:09

标签: python discord discord.py

我希望当我对 ✅ 做出反应时,机器人会创建频道,当我对 ❌ 做出反应时,机器人会返回代码。但是当我做出反应时,Bot 不回复.. 8 秒后,我收到 TimeOutError。
我已经检查了很多代码,但我不明白为什么我错了.. 其余的代码工作正常,我只想添加检查。

@commands.Cog.listener()
async def on_message(self, message):

        if message.author.id == self.bot.user.id:
            return

        if isinstance(message.channel, discord.DMChannel):
            guild = self.bot.get_guild(861329995749523466)
            categorie = utils.get(guild.categories, name="Support-Test")
            channel = utils.get(categorie.channels, topic=str(message.author.id))

            if message.author != message.author.bot:
                if len(message.content) < 15:
                    emd = discord.Embed(
                        description="⛔ - Deine Nachricht muss min. 15 Zeichen enthalten.",
                        colour=0xd11515
                    )
                    await message.channel.send(embed=emd)
                    await message.add_reaction("⛔")
                    return

                if not channel:
                    embed2 = discord.Embed(
                        description=f"Möchtest du ein Support Ticket erstellen?",
                        colour=0x9cffca,
                    )
                    check = await message.channel.send(
                        embed=embed2,
                    )

                    await check.add_reaction("✅")
                    await check.add_reaction("❌")

                    def check(reaction, user):
                        return user == message.author and (str(reaction.emoji) == "✅" or "❌")

                    try:
                        reaction, user = await self.bot.wait_for('reaction_add', timeout=8.0, check=check)
                    except asyncio.TimeoutError:
                        embed = discord.Embed(
                            title=":x: Deletion cancelled",
                            colour=discord.Colour.purple(),
                            description="Message timed out"
                        )

                        await message.channel.send(embed=embed)
                    else:
                        if reaction.emoji == "❌":
                            print("Lol")
                        elif reaction.emoji == "✅":
                            channel = await categorie.create_text_channel(
                                name=f"{message.author.name}#{message.author.discriminator}",
                                topic=str(message.author.id))
                            await channel.send(f"New ModMail created by {message.author.mention}")

                embed = discord.Embed(
                    description=f"{message.content}",
                    colour=0x9cffca,
                    timestamp=datetime.utcnow()
                )

                embed.set_author(name=message.author, icon_url=message.author.avatar_url)
                embed.set_footer(text=message.author.id)

                await channel.send(embed=embed)
                await message.add_reaction("?")
    ```

1 个答案:

答案 0 :(得分:0)

这样的表达式(在 check 函数内)

(str(reaction.emoji) == "✅" or "❌")

Python 是这样解释的

(str(reaction.emoji) == "✅") or ("❌")

如果第一个表达式 (str(reaction.emoji) == "✅") 不为真,则第二个 ("❌") 为真,所以这个复合条件总是通过。

试试这个:

(str(reaction.emoji) == "✅") or (str(reaction.emoji) == "❌")

甚至更好

str(reaction.emoji) in ("✅", "❌")