Reputation: 69
I am writing a bot for discord and there is a command ($ bone) in which the bot writes the following: "select a number from 1 - 9" and after that the user should answer and here's how I can read the line in which he wrote the number?
Upvotes: 0
Views: 61
Reputation: 911
As alluded to in the comments, we will use a coroutine called wait_for, that waits for events like message
: https://discordpy.readthedocs.io/en/latest/api.html#discord.Client.wait_for
Because these are the same events we might hook into with bot.event
, we will be able to process the returned message.
@bot.command()
async def bone(ctx):
await ctx.send("Select a number 1-9")
def my_check_func(message): #A checking function: When we return True, consider our message "passed".
if message.author == ctx.author: #Make sure we're only responding to the original invoker
return True
else:
return False
response = await bot.wait_for("message", check=my_check_func) #This waits for the first message event that passes the check function.
#This execution process now freezes until a message that passes that check is sent,
#but other bot interactions can still happen.
c = response.content #equal to message.content, because response IS a message
if c.isdigit() and 1 <= int(c) <= 9: #if our response is a number equal to 1, 9, or between those...
await ctx.send("You sent: {}".format(response.content))
else:
await ctx.send("That's not a number between 1 and 9!")
Real example interaction to prove it works:
Cook1
$bone
Tutbot
Select a number 1-9
Cook1
4
Tutbot
You sent: 4
Cook1
$bone
Tutbot
Select a number 1-9
Cook1
no
Tutbot
That's not a number between 1 and 9!
Upvotes: 1