I'm looking to try and ask people a series of questions if they enter a certain command. I want them to enter the command, for the bot to ask the question, then for them to answer and for the bot to assign a variable to their message content. For example
Bot: 'What's your name?' Bananas Bot: 'What's your question?' How to get bananas
I.e., the bot would wait for the answer then send the next question, etc. At the end, I'd want there to be variables like Name and Question which are identical to the messages sent. Is this possible? How can I do this?
My first code attempt is below (the printing is just a placeholder command to test and see if the variables work). It doesn't work at all:
client.PreferredName=False
client.Question=False
await message.channel.send('Please enter your preferred name below:')
@client.event
async def on_message(message):
if not message.author == client.user:
client.PreferredName=message.content
await message.channel.send('Please enter your question:')
@client.event
async def on_message(message):
if not message.author == client.user:
client.Question=message.content
print(str(client.PreferredName) str(client.Question))
CodePudding user response:
You can use wait_for
to wait for user input. I created the function message
that sends your question and waits for a response, then returns it and stores it in variables (e.g. name
and question
). Instead of using the on_message
event, I created the command test
, that you can use with your prefix (e.g. !test
).
async def message(ctx, message : str, timeout: float): # this function is used to send question and wait for response
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
await ctx.send(message)
response = await client.wait_for("message", check=check, timeout=timeout)
return response.content # return's user's response
@client.command()
async def test(ctx):
try:
name = await message(ctx, "What's your name?", timeout=60.0) # you can change timeout (how long bot will wait for response - in seconds)
question = await message(ctx, "What's your question?", timeout=60.0) # this line is used to ask questions. You can add more if you want
except asyncio.TimeoutError: # checks if the time limit for a response was exceeded
await ctx.send("You were too slow to answer!")
else: # if it wasn't
await ctx.send(f'Your name is "{name}" and your question: "{question}"') # you can do something with content here
CodePudding user response:
You can do it using wait_for
function:
import asyncio
questions = ["Question 1", "Question 2", "Question 3"] # specify your questions here
@bot.event
async def poll(ctx):
answers = []
for question in questions:
await ctx.send(question)
try:
message = await bot.wait_for('message', check=lambda m: m.channel == ctx.channel and m.author == ctx.author, timeout=30)
except asyncio.TimeoutError:
break
if len(questions) != len(answers):
pass # member hasn't completed the poll
else:
pass # member has completed the poll. You can get answers using answers list
This solution is scalable because you can ask a lot of questions. Just edit questions
variable. You don't need to write new code for it.