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

如何在discord.py中编辑slash命令冷却消息并重置冷却

Discord Slash命令Clear功能:冷却提示编辑与冷却重置解决方案

错误原因分析

1. AttributeError: 'NoneType' object has no attribute 'edit'

这个错误是因为你尝试调用edit()的消息对象为None。discord.py中,interaction.response.send_message()不会返回消息实例(返回值为None),如果直接对其返回值调用edit(),必然触发该错误。

2. InteractionResponded: This interaction has already been responded to

当你已经通过interaction.response.send_message()完成初始响应后,再次调用interaction.response下的响应方法(如send_message)就会触发该错误。slash命令的交互只能通过interaction.response响应一次,后续操作需编辑原响应或发送跟进消息。


动态冷却提示实现方案

要实现动态倒计时的冷却提示,需通过编辑初始响应的方式更新内容,而非重复发送新响应。以下是完整的代码示例:

import discord
from discord import app_commands
import asyncio

# 全局日志频道(需提前初始化)
logs_moderazione: discord.TextChannel = None

@app_commands.checks.cooldown(1, 60, key=lambda i: i.user)
@app_commands.command(name="clear", description="批量清理频道消息")
async def clear_cmd(interaction: discord.Interaction, amount: int):
    # 获取冷却桶,计算剩余冷却时间
    cooldown = clear_cmd.checks[0]
    bucket = cooldown.get_bucket(interaction)
    remaining_cooldown = bucket.get_remaining_cooldown()

    if remaining_cooldown > 0:
        # 发送初始冷却提示(仅用户可见)
        await interaction.response.send_message(
            f"冷却中,剩余 {int(remaining_cooldown)} 秒",
            ephemeral=True
        )
        # 循环更新倒计时
        while remaining_cooldown > 0:
            await asyncio.sleep(1)
            remaining_cooldown -= 1
            # 编辑原响应更新倒计时,避免重复响应错误
            await interaction.edit_original_response(
                content=f"冷却中,剩余 {int(remaining_cooldown)} 秒"
            )
        # 倒计时结束后更新提示
        await interaction.edit_original_response(content="冷却已结束,可再次使用命令")
        return

    # 执行消息清理逻辑
    deleted_messages = await interaction.channel.purge(limit=amount)
    # 发送清理结果提示
    await interaction.response.send_message(
        f"已成功清理 {len(deleted_messages)} 条消息",
        ephemeral=True
    )
    # 记录操作日志
    if logs_moderazione:
        await logs_moderazione.send(
            f"{interaction.user.mention} 在 {interaction.channel.mention} 清理了 {len(deleted_messages)} 条消息"
        )

Slash命令冷却重置方法

要重置指定用户的clear命令冷却,可通过操作冷却装饰器的_buckets字典实现。以下是管理员专属的重置命令示例:

@app_commands.command(name="reset_clear_cooldown", description="重置指定用户的clear命令冷却")
@app_commands.default_permissions(administrator=True)
async def reset_clear_cooldown_cmd(interaction: discord.Interaction, user: discord.User):
    # 获取clear命令的冷却装饰器实例
    cooldown = clear_cmd.checks[0]
    # 生成目标用户对应的冷却键(与冷却规则的key参数对应)
    cooldown_key = cooldown.key(interaction)
    cooldown_key = (user.id,) if isinstance(cooldown_key, tuple) else user.id
    # 从冷却桶中移除目标用户的冷却记录
    cooldown._buckets.pop(cooldown_key, None)
    
    await interaction.response.send_message(
        f"已重置 {user.mention} 的clear命令冷却",
        ephemeral=True
    )

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 13:15:28