簡體   English   中英

如何使用上下文在同一行中定義的上下文設置默認值?

[英]How to set a default using context with context defined in same line?

我正在創建一個 discord 機器人,並且我正在嘗試使其在成員說出未提及用戶的命令時默認為他們自己。 它不能內聯工作,我需要 arg 在同一行。

我試圖將默認值放在同一行,但我需要 ctx ,並且 ctx 是在同一行中定義的,所以它不起作用。

@bot.command(name="joined", description="Shows precisely when a member of the server joined", aliases=["entry", "jointime", "join_time"], pass_context=True)
async def joined(**ctx**, member: discord.Member = **ctx.message.author.name**):
    """Says when a member joined."""
    await ctx.send('{0.name} joined in {0.joined_at}'.format(member))

當你說,加入或進入時,我預料到了這一點。 沒有用戶提及,從上下文中獲取用戶並使用它。 但是,它會向我拋出錯誤消息“ctx 未定義”。

您可以標記參數Optional ,然后在協程的主體中檢查它是否為None

from discord import Member
from typing import Optional

@bot.command(name="joined", description="Shows precisely when a member of the server joined", aliases=["entry", "jointime", "join_time"], pass_context=True)
async def joined(ctx, member: Optional[Member]):
    """Says when a member joined."""
    member = member or ctx.author
    await ctx.send('{0.name} joined in {0.joined_at}'.format(member))

暫無
暫無

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

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