我需要帮助在 discord py 中制作一个 discord py temp mute 命令

TCS*_*TCS 5 python discord discord.py

我让我的不和谐机器人有一个静音命令,但你必须在稍后自己取消静音用户,我想要另一个名为“tempmute”的命令,它可以将成员静音一定的分钟/小时/或天数,这个到目前为止,我的代码是什么,我将如何制作临时静音命令?

#mute command 
@client.command()
@commands.has_permissions(kick_members=True)
async def mute(ctx, member: discord.Member=None):
    if not member:
        await ctx.send("Who do you want me to mute?")
        return
    role = discord.utils.get(ctx.guild.roles, name="muted")
    await member.add_roles(role)
    await ctx.send("ok I did it")
Run Code Online (Sandbox Code Playgroud)

小智 5

与您赋予他们静音角色的方式类似,只需添加另一个参数来确定您希望他们静音的时间(以秒为单位)。然后,您可以在删除该角色之前使用await asyncio.sleep(mute_time)。

代码看起来像这样:

import asyncio

#mute command 
@client.command()
@commands.has_permissions(kick_members=True)
async def mute(ctx, member: discord.Member=None, mute_time : int):
    if not member:
        await ctx.send("Who do you want me to mute?")
        return
    role = discord.utils.get(ctx.guild.roles, name="muted")
    await member.add_roles(role)
    await ctx.send("ok I did it")

    await asyncio.sleep(mute_time)
    await member.remove_roles(role)
    await ctx.send("ok times up")

Run Code Online (Sandbox Code Playgroud)