Reputation: 237
I'm making a Discord bot in Python, and I want to add such a function when the bot responds to the command only in certain circumstances.
For example:
Me: !quiz
Bot: Gives variants - !a; !b; !c
Me: Chooses variants and bot responds
!a
; !b
or !c
before !quiz
command is used, bot won't respond. Any ideas?Upvotes: 1
Views: 953
Reputation: 119
Going off what Mahrkeenerh said you can use wait_for() Below is an example:
@bot.command()
async def quiz(ctx):
await ctx.send("A, B or C")
try:
msg = await bot.wait_for('message', check=lambda m: m.author == ctx.author and m.channel == ctx.channel, timeout=30.0)
except asyncio.TimeoutError:
await ctx.send("You took to long...")
else:
if msg.content == "A":
await ctx.send("Correct")
elif msg.content == "B":
await ctx.send("Wrong")
elif msg.content == "C":
await ctx.send("Wrong")
else:
await ctx.send("Huh?")
We are sending a message and then waiting for a response, we are also checking to see if its the original author and the original channel. If so then we are checking if answer to see what the response is.
Upvotes: 2
Reputation: 310
You can use bot.wait_for()
to wait for user response:
@bot.command(pass_context=True)
async def quiz(ctx):
channel, author = ctx.channel, ctx.message.author
await ctx.send('Choose one:\n!a;\n!b;\n!c;')
answer = await bot.wait_for(
"message",
check=lambda x: x.content.startswith('!') and x.channel == channel and x.author == author
)
if answer.content.strip() == '!a':
# here goes your logic
Upvotes: 2
Reputation: 1121
You can use wait_for()
:
Discord.py Make bot wait for reply
Or you could have a global variable (a flag - boolean) that would mark, if you have used the !quiz
command. Then inside the answers command, check if this global variable is True
, if so, respond (and reset it back to False
), else ignore.
Upvotes: 1