簡體   English   中英

Discord.py wait_for 來自兩個用戶的 dm 回復

[英]Discord.py wait_for reply in dm from two users

我正在嘗試制作一個機器人,它將向兩個用戶(一個作者和另一個提到)發送一個 dm 並等待響應,然后將其發送回服務器。 該機器人能夠發送 dm 並且確實超時,但即使兩個用戶都響應了 dm,它也無法正常工作。

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)

我在這里做錯了什么?

注意:不做嘗試塊,這工作正常,兩個消息都將在每個用戶發送后發送。

我通過使用asyncio.gather函數解決了這個問題(雖然我無法讓它在 try 塊中,所以我無法捕捉到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}')

這將等到兩個用戶都做出響應,然后將他們的消息發送回使用該命令的位置。

如果有人可以幫助我弄清楚如何在 try 塊中使用它,我將不勝感激,但它不是必需的。 出現的問題是,當它在 try 塊中時,它只會 dm 第一個用戶,第一個用戶必須響應然后它將向第二個用戶發送 dm - 這是我不想要的。 希望它同時發生

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM