如何在discord.py中设置可选参数? [英] 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
.
该机器人将接受这两个条件:
The bot will accept both of these:
!对@user 1h垃圾邮件静音
和!静音@user垃圾邮件
这可能吗?
推荐答案
您可以选择异步等待,然后取消对用户静音:
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中设置可选参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文