简体   繁体   中英

How do I wait a certain amount of time without pausing the whole program? (Discord.py rewrite.)

Excuse the bad code, but here it is.

I enter the time I want a user to be muted, I mute the user, and sleep for a certain amount of time, but when I sleep I can't run any commands, so I'm assuming sleeping pauses the whole program, how do I stop that? also please don't come at me with hard programmer man language, i started yesterday, as simple as possible please 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}')**```

With asynchronous functions, you can use the asyncio library. It is used to write concurrent code.
So, instead of using time.sleep , you should use 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}')**

Reference: asyncio documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM