我该如何避免我的discord机器人在DM中计算他自己的消息



我开发了一个机器人,通过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)

我是否必须使用类似流程监听器的东西?

wait_for接受一个check参数,请使用此参数。

def check(m):
return m.id == ctx.author.id
msg = await self.bot.wait_for('message', timeout=30.0, check=check)

你可以在这里阅读更多关于

以下方法可用于读取私有消息并避免机器人读取自己的消息:

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

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

最新更新