[英]Discord.py mute command
所以我完成了我的静音命令,然后我在一个人身上进行了测试,它奏效了。 但是当涉及到对不同的人静音时,它不会。 我将一个人静音 1 分钟,将另一个人静音 10 秒。 因为我先做了 1m 静音,所以它先静音,然后我对另一个人做了 10 秒静音。 它等到一分钟静音完成后才进行 10 秒静音。如何阻止这种情况发生? 这是我的代码:
@client.command()
@commands.has_role("Mod")
async def mute(ctx, user : discord.Member, duration = 0,*, unit = None):
roleobject = discord.utils.get(ctx.message.guild.roles, id=730016083871793163)
await ctx.send(f":white_check_mark: Muted {user} for {duration}{unit}")
await user.add_roles(roleobject)
if unit == "s":
wait = 1 * duration
time.sleep(wait)
elif unit == "m":
wait = 60 * duration
time.sleep(wait)
await user.remove_roles(roleobject)
await ctx.send(f":white_check_mark: {user} was unmuted")
没有错误。
您正在使用time.sleep(wait)
,这会暂停所有其他代码,直到wait
时间结束。 当您使用time.sleep
python 时,它不接受任何其他输入,因为它仍然“忙”在睡觉。
你应该研究协程来解决这个问题。
这篇文章给出了一个很好的例子来说明你想要实现的目标: 我需要帮助在 discord py 中制作一个 discord py temp mute 命令
我认为这个编辑应该有效:
#This should be at your other imports at the top of your code
import asyncio
async def mute(ctx, user : discord.Member, duration = 0,*, unit = None):
roleobject = discord.utils.get(ctx.message.guild.roles, id=730016083871793163)
await ctx.send(f":white_check_mark: Muted {user} for {duration}{unit}")
await user.add_roles(roleobject)
if unit == "s":
wait = 1 * duration
await asyncio.sleep(wait)
elif unit == "m":
wait = 60 * duration
await asyncio.sleep(wait)
await user.remove_roles(roleobject)
await ctx.send(f":white_check_mark: {user} was unmuted")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.