How do I set an optional argument in discord.py?(如何在 discord.py 中设置可选参数?)
问题描述
我尝试在线研究,但没有其他问题可以帮助我解决我的问题.
I've tried to research online, but no other questions were able to help me with my issue.
这是我的场景.
我正在 discord.py 中发出静音命令.
I am making a mute command in discord.py.
我希望时间是可选的,但如果没有指定时间,我希望该参数成为原因的一部分.
I want the time to be optional, but if the time is not specified I want that argument to be part of the reason.
澄清我的意思:
@client.command()
@commands.has_permissions(kick_members=True)
async def mute(ctx, member: Member = None, time: int = None, *, reason = None):
pass
这里,如果 time
为 None,则使其成为 reason
的一部分.
Here, if time
is None, then make it part of reason
.
机器人将接受这两个:
!mute @user 1h 垃圾邮件
和!mute @user spam
这可能吗?
推荐答案
您可以选择异步等待,然后将用户取消静音:
You can optionally wait asynchronously and then unmute the user:
@bot.command()
async def mute(ctx, member: discord.Member, time: typing.Optional[int]):
await member.edit(mute=True)
if time:
await asyncio.sleep(time)
await member.edit(mute=False)
这篇关于如何在 discord.py 中设置可选参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 discord.py 中设置可选参数?


基础教程推荐
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01