我开发了一个通过 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)
我是否必须使用进程侦听器之类的东西?
答案 0 :(得分:0)
wait_for
接受一个检查参数,使用这个。
def check(m):
return m.id == ctx.author.id
msg = await self.bot.wait_for('message', timeout=30.0, check=check)
您可以阅读更多相关信息here
答案 1 :(得分:0)
可以使用以下方法读取私信,避免机器人读取自己的消息:
def check(m):
return ctx.author == m.author and isinstance(m.channel, discord.DMChannel)
这里我们首先检查回答问题的人是否也执行了命令,然后这些回答是否也在私信中发送。