如何使用discord.py暂时使某人静音?

时间:2020-06-17 19:10:01

标签: python discord discord.py

我正在尝试为discord.py机器人创建!mute命令。我希望将其格式化为!mute @user 10ss持续几秒钟。我还希望它能够读m几分钟,h个小时,d天。如果可能,甚至!mute @user 1d 3h 3s。这是我现在拥有的代码。

@client.command()
@commands.has_permissions(kick_members=True)
async def mute(ctx, member: discord.Member, mute_time : int):
    guild = ctx.guild
    for role in guild.roles:
        if role.name == "Muted":
            await member.add_roles(role)
            await ctx.send("{} has has been muted!" .format(member.mention))
            await asyncio.sleep(mute_time)
            await member.remove_roles(role)
            await ctx.send("{} has been unmuted!" .format(member.mention))

暂时正常。它!mute @user 10持续10秒钟。

我也有一个取消静音的命令。它是这样的。 !unmute @user

@client.command()
@commands.has_permissions(kick_members=True)
async def unmute(ctx, member : discord.Member):
    guild = ctx.guild
    for role in guild.roles:
        if role.name == "Muted":
            await member.remove_roles(role)
            await ctx.send("{} has has been unmuted!" .format(member.mention))

除此命令有问题外。假设我执行了!mute @user 60。如果我决定在60秒结束之前使用命令!unmute @user取消静音,则可以成功取消静音。但是,当60秒结束时,它将再次取消静音,并第二次发送消息。

再次澄清一下,我的目标是创建一个!mute命令,以便可以执行!mute @user 1d 3ss代表秒,m代表分钟,h代表小时,d代表几天,mo代表几个月,y已经好几年了。

任何答案都很好,谢谢!

1 个答案:

答案 0 :(得分:0)

await asyncio.sleep(mute_time)之后,您可以添加一个if语句来查看用户是否具有静音角色,只有当他们具有静音角色时,您才删除该角色并发送消息。

沿这些行:

@commands.has_permissions(kick_members=True)
async def mute(ctx, member: discord.Member, mute_time : int):
    guild = ctx.guild
    for role in guild.roles:
        if role.name == "Muted":
            await member.add_roles(role)
            await ctx.send("{} has has been muted!" .format(member.mention))
            await asyncio.sleep(mute_time)
            IF USER STILL HAS MUTED ROLE:
                await member.remove_roles(role)
                await ctx.send("{} has been unmuted!" .format(member.mention))