【问题标题】:Discord.py mute commandDiscord.py 静音命令
【发布时间】:2020-10-28 22:15:24
【问题描述】:

所以我完成了我的静音命令,然后我在一个人身上进行了测试,它成功了。但是当涉及到对不同的人静音时,它不会。我将一个人静音 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")

没有错误。

【问题讨论】:

    标签: python command mute


    【解决方案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")    
    

    【讨论】:

    • @jwjw2069 如果这对您有帮助,请不要忘记将我的答案标记为正确。
    • 对不起,我忘记了这个网站。您的代码确实对我有帮助,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 2021-09-02
    • 2020-06-15
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多