如何在 discord.py 中设置可选参数?
- 作者: 用户35175328
- 来源: 51数据库
- 2023-02-10
问题描述
我尝试在线研究,但没有其他问题可以帮助我解决我的问题.
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(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0