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

如何在discord.py中实现多命令共享冷却机制?

实现Discord命令的共享冷却系统

当然可以实现共享冷却,下面提供两种可行方案:

方案一:自定义共享冷却类

通过继承app_commands.Cooldown类,重写get_key方法,让多个命令使用同一个冷却标识(比如用户ID),就能实现共享冷却效果。

import discord
from discord import app_commands
from typing import Optional

class SharedCooldown(app_commands.Cooldown):
    def get_key(self, interaction: discord.Interaction) -> Optional[str]:
        # 用用户ID作为共享冷却的唯一标识,确保同一用户的多个命令共享冷却规则
        return str(interaction.user.id)

# 定义共享冷却规则:10秒内仅可执行一次(可按需调整参数)
shared_cooldown = SharedCooldown(1, 10)

intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)

@tree.command(name="command1", description="1st Command")
@app_commands.checks.cooldown(1, 10, key=shared_cooldown.get_key)  # 绑定共享冷却键
async def command1(interaction):
    await interaction.response.send_message("执行Command1,现在两个命令都进入冷却")

@tree.command(name="command2", description="2nd Command")
@app_commands.checks.cooldown(1, 10, key=shared_cooldown.get_key)  # 同样绑定共享冷却键
async def command2(interaction):
    await interaction.response.send_message("执行Command2,现在两个命令都进入冷却")

@client.event
async def on_ready():
    await tree.sync()
    print(f"Logged in as {client.user}")

client.run("YOUR_BOT_TOKEN")

方案二:手动维护冷却状态

用全局字典跟踪每个用户的冷却结束时间,在每个命令执行前检查是否处于冷却状态,逻辑更灵活。

import discord
from discord import app_commands
import time

# 存储用户冷却结束时间:key=用户ID,value=冷却结束的时间戳
user_cooldowns = {}
COOLDOWN_DURATION = 10  # 冷却时长,单位为秒

intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)

def check_shared_cooldown(interaction: discord.Interaction) -> bool:
    user_id = interaction.user.id
    current_time = time.time()
    # 检查用户是否处于冷却中
    if user_id in user_cooldowns and current_time < user_cooldowns[user_id]:
        return False
    # 更新用户的冷却结束时间
    user_cooldowns[user_id] = current_time + COOLDOWN_DURATION
    return True

@tree.command(name="command1", description="1st Command")
@app_commands.check(check_shared_cooldown)
async def command1(interaction):
    await interaction.response.send_message("执行Command1,两个命令已进入冷却")

@tree.command(name="command2", description="2nd Command")
@app_commands.check(check_shared_cooldown)
async def command2(interaction):
    await interaction.response.send_message("执行Command2,两个命令已进入冷却")

# 处理冷却触发的错误提示
@tree.error
async def on_app_command_error(interaction: discord.Interaction, error: app_commands.AppCommandError):
    if isinstance(error, app_commands.CheckFailure):
        await interaction.response.send_message("请等待冷却结束后再执行命令", ephemeral=True)

@client.event
async def on_ready():
    await tree.sync()
    print(f"Logged in as {client.user}")

client.run("YOUR_BOT_TOKEN")

两种方案的特点:

  • 方案一依托Discord.py内置冷却机制,符合官方规范,自动处理冷却错误逻辑。
  • 方案二更灵活,适合需要自定义复杂冷却规则的场景(比如划分多组共享冷却)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 20:47:16