问题描述
我正在创建一个不和谐机器人,并且我正在尝试使其在成员说出没有提及用户的命令时默认为他们自己.它不能内联,我需要 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))