首页 > 解决方案 > 我需要帮助在 discord py 中创建 discord py temp 静音命令

问题描述

我让我的不和谐机器人有一个静音命令,但你必须在以后自己取消静音用户,我想要另一个名为“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")

标签: pythondiscorddiscord.py

解决方案


与您赋予他们静音的角色类似,只需添加另一个参数以计算您希望他们在几秒钟内静音的时间。然后,您可以在删除该角色之前使用 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")


推荐阅读