如何使用上下文在同一行中定义的上下文设置默认值?

How to set a default using context with context defined in same line?(如何使用上下文在同一行中定义的上下文设置默认值?)
本文介绍了如何使用上下文在同一行中定义的上下文设置默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

这篇关于如何使用上下文在同一行中定义的上下文设置默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I make a list of all members in a discord server using discord.py?(如何使用 discord.py 列出不和谐服务器中的所有成员?)
how to change discord.py bot activity(如何更改 discord.py 机器人活动)
Issues with getting VoiceChannel.members and Guild.members to return a full list(让 VoiceChannel.members 和 Guild.members 返回完整列表的问题)
Add button components to a message (discord.py)(将按钮组件添加到消息(discord.py))
on_message() and @bot.command issue(on_message() 和@bot.command 问题)
How to edit a message in discord.py(如何在 discord.py 中编辑消息)