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

Discord音乐机器人新播放音频仍触发定时离开故障如何修复?

解决方案

核心逻辑是存储定时销毁任务的ID,在新音频播放时主动取消未执行的定时任务,具体修改如下:

步骤1:修改队列结束监听逻辑

给player实例挂载自定义属性存储定时器ID,替换你原来的queueEnd事件代码:

.on("queueEnd", (player) => {
  // 先清理历史遗留的定时器,避免重复触发
  if (player.leaveTimeout) clearTimeout(player.leaveTimeout);

  // 将定时器ID存入player实例
  player.leaveTimeout = setTimeout(() => {
    // Don't leave if 24/7 mode is active
    let QueueEmbed = new MessageEmbed()
    .setAuthor("The queue has ended", this.botconfig.IconURL)
    .setColor(this.botconfig.EmbedColor)
    .setTimestamp();

    client.channels.cache.get(player.textChannel).send( { embeds: [ QueueEmbed ] } );

    if (!this.config["24/7"]) player.destroy();
  }, 30000); 
});

步骤2:新增播放启动时的定时器清除逻辑

在播放器的曲目开始播放事件(通常是trackStart,不同音乐库事件名可能略有差异)中加入清除定时器的代码:

.on("trackStart", (player, track) => {
  // 检测到新曲目播放,取消待执行的离开定时任务
  if (player.leaveTimeout) {
    clearTimeout(player.leaveTimeout);
    player.leaveTimeout = null;
  }
  // 你原有trackStart事件的其他逻辑保留在下方即可
})

补充说明

如果你的机器人支持暂停后恢复播放、手动插入曲目直接播放等场景,也可以在对应的playerResume、trackAdd等事件中加入相同的清除定时器逻辑,覆盖所有新播放触发的场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 11:18:04