How to set a default using context with context defined in same line?(如何使用上下文在同一行中定义的上下文设置默认值?)
问题描述
我正在创建一个不和谐机器人,并且我正在尝试使其在成员说出没有提及用户的命令时默认为他们自己.它不能内联,我需要 arg 在同一行.
I am creating a discord bot, and I am trying to make it where if a member says a command with no user mentioned, it defaults to themself. It doesn't work inline, and I need the arg in the same line.
我尝试将默认值放在同一行,但我需要 ctx ,并且 ctx 定义在同一行中,所以它不起作用.
I tried to put the default in the same line, but I need the ctx for that, and ctx is defined in the same line, so it won't work.
@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))
我希望当您说 !joined 或 !entry 时,没有用户提及,将用户从上下文中取出并使用它.但是,它会向我抛出错误消息未定义 ctx."
I expected this, when you say !joined or !entry, with no user mention, to take the user from the context and use that instead. However, it throws me the error message "ctx is not defined."
推荐答案
可以标记参数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))
这篇关于如何使用上下文在同一行中定义的上下文设置默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用上下文在同一行中定义的上下文设置默认值?


基础教程推荐
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 包装空间模型 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 求两个直方图的卷积 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01