Warm tip: This article is reproduced from serverfault.com, please click

discord.py-如何避免我的不和谐机器人在DM中计数自己的消息?

(discord.py - How do I avoid that my discord bot is counting his own messages in a DM?)

发布于 2020-12-20 16:37:56

我开发了一个通过DM侦听消息的机器人。向用户询问各种问题,他必须在30秒内回答。该机器人到目前为止已经发送了问题,但是有时它会一次发送两个问题,然后将其自身的消息作为答案。如何避免这种情况?

@commands.command(aliases=["sap"])
@commands.cooldown(1, 100, BucketType.user)
    async def sendapply(self, ctx):
        await ctx.author.send("Bitte beantworte jede Frage innerhalb von **30 Sekunden.**")

        questions = ["**Wie heißt du?**",
                     "**Erzähl uns etwas von dir.**",
                     "**Warum hast du dich beworben?**"]

        answers = []

        for i in questions:
            await ctx.author.send(i)
            try:
                msg = await self.bot.wait_for('message', timeout=30.0)
            except asyncio.TimeoutError:
                await ctx.author.send("Du hast die Frage nicht rechtzeitig beantwortet. Bitte erneut probieren.")
                return
            else:
                answers.append(msg)  # append the message object instead of the content

        channel = self.bot.get_channel(790190364522184724)
        e = discord.Embed(color=ctx.author.color)
        e.title = "Neue Bewerbung!"
        e.description = f"**Wie heißt du?:** {answers[0].content}\n **Zu dir:** {answers[1].content}\n **Warum hast du dich beworben?:** {answers[2].content}"
        e.set_footer(text=f"ID: {ctx.author.id}")
        e.timestamp = datetime.datetime.utcnow()
        await channel.send(embed=e)

我是否必须使用类似进程侦听器的方法?

Questioner
Dominik
Viewed
11
Dominik 2020-12-22 19:55:15

以下方法可用于读取私有消息,并避免漫游器读取其自己的消息:

def check(m):
  return ctx.author == m.author and isinstance(m.channel, discord.DMChannel)

在这里,我们首先检查回答问题的人员是否也已执行了命令,然后还以私有消息的形式发送了这些答案。