You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

discord.py Tempmute临时禁言命令无法移除Mute角色问题求助

问题排查与修复方案

核心问题1:代码执行顺序错误,提前return导致逻辑中断

你在第一遍禁言逻辑执行完成后直接写了return语句,后续判断「Muted by ez角色是否存在」的逻辑完全无法触发。如果服务器不存在该角色,你直接调用add_roles传入空值会直接抛出异常,终止代码运行,根本执行不到后续的休眠和移除角色步骤。

核心问题2:属性调用错误直接抛出异常

你写的await ctx.member.send(embed=muteprvt)存在属性错误,ctx没有member属性,要发送私信给被禁言用户应该调用await member.send(),这里运行时会直接报错,代码直接终止,不会走到后面的休眠和解禁逻辑。

核心问题3:时长计算逻辑错误

你的mute_time = int(time[0]) * time_conversion[time[-1]]只取了时长的第一位字符转为数字,如果你输入10m这类超过1位的时长,只会识别为1 * 60 = 60秒,和预期的10分钟不符,如果你测试用的是长时长,会出现还没到你以为的到期时间,自然不会触发解禁。

核心问题4:冗余重复代码

你写了两遍完全一致的禁言、解禁逻辑,第一遍执行完就return,第二遍完全不会运行,属于无效代码。


修复后完整代码

import asyncio
@client.command(description="Mutes the specified user.")
@commands.has_permissions(manage_messages=True)
async def mute(ctx, member: discord.Member, time=None, *, reason=None):
    # 参数校验
    if not time:
        await ctx.send("请指定禁言时长,格式示例:10m、1h、2d")
        return
    # 修复时长计算逻辑
    time_conversion = {"s": 1, "m": 60, "h": 3600, "d": 86400}
    try:
        unit = time[-1]
        if unit not in time_conversion:
            await ctx.send("仅支持的时长单位:s/秒、m/分钟、h/小时、d/天")
            return
        num = int(time[:-1])
        mute_time = num * time_conversion[unit]
    except:
        await ctx.send("时长格式错误,示例:10m代表10分钟")
        return

    guild = ctx.guild
    mutedRole = discord.utils.get(guild.roles, name="Muted by ez")
    # 先判断角色是否存在,不存在就创建
    if not mutedRole:
        mutedRole = await guild.create_role(name="Muted by ez")
        for channel in guild.channels:
            await channel.set_permissions(mutedRole, speak=False, send_messages=False, read_message_history=True, read_messages=True)
    
    # 给用户加禁言角色
    await member.add_roles(mutedRole, reason=reason)
    # 发公屏通知
    mute = discord.Embed(
        description=f"<a:mutediscord:888827254853996564> {member.mention} 已被禁言\n**原因:** {reason}\n**时长:** {time}",
        color=ctx.author.color)
    mute.set_author(name=ctx.author.name, icon_url=ctx.author.avatar.url)
    await ctx.channel.send(embed=mute)
    # 发私信通知,修复ctx.member错误,加异常捕获避免用户关私信导致逻辑中断
    try:
        muteprvt = discord.Embed(
            description=f"<a:mutediscord:888827254853996564> 你在 {ctx.guild.name} 被禁言\n**原因:** {reason}\n**时长:** {time}",
            color=ctx.author.color)
        muteprvt.set_author(name=ctx.author.name, icon_url=ctx.author.avatar.url)
        await member.send(embed=muteprvt)
    except:
        pass
    
    # 等待禁言时长到期
    await asyncio.sleep(mute_time)
    
    # 移除禁言角色
    await member.remove_roles(mutedRole)
    # 发公屏解禁通知
    unmute = discord.Embed(description=f"🔨 {member.mention} 已自动解除禁言", color=ctx.author.color)
    unmute.set_author(name=ctx.author.name, icon_url=ctx.author.avatar.url)
    await ctx.channel.send(embed=unmute)
    # 发解禁私信
    try:
        unmuteprvt = discord.Embed(description=f"🔨 你在 {ctx.guild.name} 的禁言已到期解除", color=ctx.author.color)
        unmuteprvt.set_author(name=ctx.author.name, icon_url=ctx.author.avatar.url)
        await member.send(embed=unmuteprvt)
    except:
        pass
    return

注:如果你使用的是discord.py v2以上版本,原avatar_url属性已弃用,上述代码已经替换为适配v2版本的avatar.url,如果是v1版本改回avatar_url即可。

内容的提问来源于stack exchange,提问作者Socsz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 10:15:03