如何解决如何避免我的不和谐机器人在 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
接受一个检查参数,使用这个。
def check(m):
return m.id == ctx.author.id
msg = await self.bot.wait_for('message',timeout=30.0,check=check)
您可以阅读更多相关信息here
,可以使用以下方法读取私信,避免机器人读取自己的消息:
def check(m):
return ctx.author == m.author and isinstance(m.channel,discord.DMChannel)
这里我们首先检查回答问题的人是否也执行了命令,然后这些回答是否也在私信中发送。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。