不好意思,但是在这里。
我输入我希望用户静音的时间,我将该用户静音,然后睡眠一定时间,但是当我睡眠时我无法运行任何命令,因此我假设睡眠会暂停整个过程程序,如何停止呢?还请不要用刻苦的编程人员语言来找我,我昨天开始,请尽可能简单地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}')**```
答案 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
文档