簡體   English   中英

如何使用discord.py讓discord wait_for等待反應或消息

[英]How to get a discord wait_for to wait for either reaction or a message using discord.py

我一直在使用以下兩種方法從不和諧嵌入中獲取用戶輸入。 第一個我用來獲取基於文本的消息輸入,第二個我用來獲取反應輸入(用於對嵌入進行分頁)。 我想知道的是,我是否可以將這兩者合並以創建一種等待反應或消息輸入的方法?

#message input

try:
                    answer = await self.bot.wait_for(
                        "message",
                        timeout=60,
                        check=lambda message: message.author.id == ctx.author.id
                        and isinstance(message.channel, discord.channel.DMChannel) 
                                            
                    )

#reaction input
try:
                    reaction, user = await self.bot.wait_for(
                        "reaction_add",
                        timeout=60,
                        check=lambda reaction, user: user.id == ctx.author.id
                        and reaction.emoji in buttons
                        #and isinstance(reaction.channel, discord.channel.DMChannel),
                    )

更新:所以我試圖實現方法duckboycool 鏈接到(非常感謝人!)。 我現在遇到的問題是,當我對分頁嵌入做出反應時,它可以正常工作,反應會被記錄下來,並且嵌入會相應地更新。 但是,如果我輸入一條消息,則會出現以下錯誤:

“反應,表情符號 = 等待任務類型錯誤:無法解包不可迭代的消息對象”

這是我的代碼:

finished =0
            
        while finished == 0:
            done_tasks = None   
            check1=lambda reaction, user: user.id == ctx.author.id and reaction.emoji in buttons
            check2=lambda message: message.author.id == ctx.author.id and isinstance(message.channel, discord.channel.DMChannel)

            pending_tasks = [self.bot.wait_for('reaction_add',check=check1),self.bot.wait_for('message',check=check2)]
            done_tasks, pending_tasks = await asyncio.wait(pending_tasks, return_when=asyncio.FIRST_COMPLETED)

            #print(done_tasks)
            for task in pending_tasks:
                task.cancel()
            for task in done_tasks: 
                reaction, emoji = await task
                message = await task
                if reaction:
                    print(reaction)
                    previous_page = current

                    if reaction.emoji == u"\u23EA":
                        current = 0
                    
                    elif reaction.emoji == u"\u25C0":
                        if current > 0:
                            current -= 1
                    
                    elif reaction.emoji == u"\u25B6":
                        if current < len(pages)-1:
                            current  += 1
                    elif reaction.emoji == u"\u23E9":
                        current = len(pages)-1
                    

                    if current != previous_page:
                        await msg.edit(embed = pages[current])
                else:
                    print(message.content)

在更新后的代碼中,您需要檢查事件是哪種類型的可等待事件,以便您可以使用正確數量的值進行解包並完成預期的行為。 使用您的messagereaction_add示例,這可能看起來像這樣。

for task in done_tasks:
    taskobj = await task
    
    if isinstance(taskobj, discord.Message):
        message = taskobj

        #Message logic
    
    else:
        reaction, user = taskobj
        
        #Reaction logic

根據您使用的事件,您可能需要做不同的事情。 (這里,它依賴於被識別為不是discord.Message實例的tuple 。)

暫無
暫無

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

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