Discord.py静音命令

时间:2020-07-08 13:25:44

标签: python command mute

所以我完成了静音命令,然后我在一个人身上对其进行了测试,并且该命令起作用了。但是,当涉及到与其他人静音时,事实并非如此。我将一个人静音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")

没有错误。

1 个答案:

答案 0 :(得分:1)

您正在使用time.sleep(wait),这将暂停所有其他代码,直到wait时间段结束。当您使用time.sleep时,python不接受任何其他输入,因为它在睡眠时仍然很“忙”。

您应该研究coroutines来解决此问题。

这篇文章很好地说明了您要实现的目标:I need help making a discord py temp mute command in discord py

认为,此修改应该有效:

#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")