簡體   English   中英

Discord.py wait_for message... 如何取消息內容和發消息的人?

[英]Discord.py wait_for message… How to take the message content and the person who wrote the message?

我正在尋找編寫一個關閉房間的命令,該命令使特定房間中的所有用戶靜音。 所以,我必須得到寫消息的用戶和消息內容。

現在我有這個,但在這種情況下,每條消息都會被監控:

def shut_check(msg):
   return msg.content

@client.command()
@commands.has_permissions(manage_messages=True)
async def shut_room(ctx):

   await ctx.send("Please send me the id of the room that you want to shut down.")

   content = await client.wait_for("message", check=check)

現在,我已經發送了消息的內容,但是如何驗證消息的作者是否是 ctx.author?

我有另一個請求,你能向我解釋一下pass_context=True的目的是什么:

@client.command(pass_context=True)

很簡單的邏輯,在命令里面定義check function。 此外,您當前的檢查根本沒有任何意義,它總是會返回一個類似真實的值

@client.command()
async def shut_room(ctx):
    def check(msg):
        return msg.author == ctx.author

    await ctx.send("Please send me the id of the room that you want to shut down.")

    message = await client.wait_for("message", check=check)
    content = message.content

此外,在等待消息時,它不會返回內容本身,而是返回discord.Message實例,我認為您對檢查 function 感到困惑。

解釋check參數:機器人將等待所需的事件,直到check function 返回真值。

編輯:

在命令之外定義檢查函數

def check(ctx):
    def inner(msg): # This is the actual check function, the one that holds the logic
        return msg.author == ctx.author
    return inner

# inside the command
message = await client.wait_for("message", check=check(ctx)) # Note that I'm calling it

我已經用另一個 function 包裝了檢查 function,所以我可以傳遞Context參數。

暫無
暫無

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

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