I have developed a bot which listens to messages via DM. The user is asked various questions, which he must answer within 30 seconds. The bot sends the questions so far, but sometimes it sends two questions at once and then counts its own message as an answer. How can I avoid this?
@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)
Would I have to use something like a process listener?
The following method can be used to read the private message and avoid the bot reading its own messages:
def check(m):
return ctx.author == m.author and isinstance(m.channel, discord.DMChannel)
Here we first check that the person answering the questions has also executed the command and then that these answers are also sent in a private message.