Reputation: 31
I am trying to make a bot where it will send a dm to two users (one author and the other mentioned) and waits for a response, which then sends it back to the server. The bot is able to send dm and it does timeout correctly but even if both users respond to the dm, it does not work properly.
users = [user1id, user2id]
for user in users:
await user.send(f'List {rounds} character(s)')
try:
user1msg = await bot.wait_for('message', check = lambda x: x.channel == user1id.dm_channel and x.author == user1id, timeout=5)
user2msg = await bot.wait_for('message', check = lambda x: x.channel == user2id.dm_channel and x.author == user2id, timeout=5)
except asyncio.TimeoutError:
await ctx.send('One or both users did not respond in time.')
else:
await ctx.send(user1msg.content)
await ctx.send(user2msg.content)
What am I doing wrong here?
Note: Without doing a try block, this works fine and both messages will be sent after each user has sent.
Upvotes: 1
Views: 210
Reputation: 31
I figured it out by utilizing the asyncio.gather
function (Although I was not able to get it to be in a try block, so I am unable to catch the asyncio.TimeoutError
.)
user1msg, user2msg = await asyncio.gather(
bot.wait_for('message', check = lambda x: x.channel == user1id.dm_channel and x.author == user1id, timeout=45),
bot.wait_for('message', check = lambda x: x.channel == user2id.dm_channel and x.author == user2id, timeout=45))
await ctx.send(f'user1: {user1msg.content}\nuser2: {user2msg.content}')
This will wait until both users have responded and then it will send their messages back to where the command was used.
If anyone can help me figure out how to use this in a try block, I will appreciate it but it is not needed. The problem that occurs is that when it is in a try block, it will only dm the first user, the first user has to respond then it will send a dm to the second user - which is what I do not want. Want it to happen concurrently
Upvotes: 1