如何在不暂停整个程序的情况下等待一定时间? (重写Discord.py。)

时间:2020-07-19 14:40:32

标签: python discord discord.py wait discord.py-rewrite

不好意思,但是在这里。

我输入我希望用户静音的时间,我将该用户静音,然后睡眠一定时间,但是当我睡眠时我无法运行任何命令,因此我假设睡眠会暂停整个过程程序,如何停止呢?还请不要用刻苦的编程人员语言来找我,我昨天开始,请尽可能简单地XD

@client.command()
async def chatmute(ctx, user: discord.Member, *, time = 5):
    role = discord.utils.get(user.guild.roles, name="-")
    await user.add_roles(role)
    role = discord.utils.get(user.guild.roles, name="Member")
    await user.remove_roles(role)
    await ctx.send(f'User has been chat muted.')
    time.sleep(time)
    role = discord.utils.get(user.guild.roles, name="Member")
    await user.add_roles(role)
    await ctx.send(f'Your mute ran out, {user.mention}')**```

1 个答案:

答案 0 :(得分:1)

通过异步功能,您可以使用asyncio库。用于编写并发代码。
因此,应该使用time.sleep而不是使用asyncio.sleep

from asyncio import sleep

@client.command()
async def chatmute(ctx, user: discord.Member, *, time = 5):
    role = discord.utils.get(user.guild.roles, name="-")
    await user.add_roles(role)
    role = discord.utils.get(user.guild.roles, name="Member")
    await user.remove_roles(role)
    await ctx.send(f'User has been chat muted.')
    sleep(time)
    role = discord.utils.get(user.guild.roles, name="Member")
    await user.add_roles(role)
    await ctx.send(f'Your mute ran out, {user.mention}')**

参考asyncio文档