From a0d6f32ea84396fcfd6cc11745666e27d57ea8c1 Mon Sep 17 00:00:00 2001 From: NoLogicAlan Date: Fri, 30 Oct 2026 00:30:37 +0300 Subject: [PATCH] fix(ui,music): handle deleted message replies and safe null duration during restore - Fall back to channel send when replyingTo message was deleted (UNKNOWN_MESSAGE 404) - Safely handle null duration in dashboard convertVideo and player embed during session restore - Protect lastfm scrobbler and search mixins from null duration properties --- commands/player/embed.mjs | 3 +-- src/dashboard/Serializers.mjs | 3 +-- src/music/PlayerEventsMixin.mjs | 2 +- src/music/player/PlaybackMixin.mjs | 2 +- src/music/player/SearchMixin.mjs | 2 +- src/services/lastfm/LastFmManager.mjs | 8 +++--- src/ui/MessageHandler.mjs | 39 +++++++++++++++++++++++++++ src/utils/Utils.mjs | 2 +- 8 files changed, 49 insertions(+), 12 deletions(-) diff --git a/commands/player/embed.mjs b/commands/player/embed.mjs index 61a7ea9..b5f26be 100644 --- a/commands/player/embed.mjs +++ b/commands/player/embed.mjs @@ -7,7 +7,6 @@ import { Utils } from "../../src/utils/Utils.mjs"; import { EmbedBuilder } from "@fluxerjs/core"; -import { getGlobalColor } from "../../src/ui/index.mjs"; import { STATES, PROGRESS } from "./consts.mjs"; /** @@ -30,7 +29,7 @@ export function buildPlayerEmbed(bot, msg, player, timeout, state = {}) { if (current?.duration && player.startedPlaying) { const elapsed = Date.now() - player.startedPlaying; const totalMs = typeof current.duration === "object" - ? (current.duration.seconds ?? 0) * 1000 + ? (current.duration?.seconds ?? 0) * 1000 : current.duration; const bar = Utils.progressBar(elapsed, totalMs, 20, PROGRESS.filled, PROGRESS.empty, isPlaying ? PROGRESS.indicator : PROGRESS.filled); diff --git a/src/dashboard/Serializers.mjs b/src/dashboard/Serializers.mjs index bc8a5cb..6ecef2a 100644 --- a/src/dashboard/Serializers.mjs +++ b/src/dashboard/Serializers.mjs @@ -58,8 +58,7 @@ const Serializers = { if (!vid) return null; const durationMs = typeof vid.duration === "number" ? vid.duration : - typeof vid.duration === "object" && vid.duration?.seconds !== null ? vid.duration.seconds * 1000 : - 0; + (vid.duration?.seconds != null ? vid.duration.seconds * 1000 : 0); return { title: vid.title, url: vid.type === "radio" ? vid.author?.url : vid.url, diff --git a/src/music/PlayerEventsMixin.mjs b/src/music/PlayerEventsMixin.mjs index 38fa911..2471b95 100644 --- a/src/music/PlayerEventsMixin.mjs +++ b/src/music/PlayerEventsMixin.mjs @@ -268,7 +268,7 @@ const PlayerEventsMixin = { const durationMs = (() => { const d = song.duration; if (!d) return null; - if (typeof d === "object" && d.seconds) return d.seconds * 1000; + if (typeof d === "object" && d?.seconds) return d.seconds * 1000; if (typeof d === "number") return d; return null; })(); diff --git a/src/music/player/PlaybackMixin.mjs b/src/music/player/PlaybackMixin.mjs index 12fe33c..d2b0cc2 100644 --- a/src/music/player/PlaybackMixin.mjs +++ b/src/music/player/PlaybackMixin.mjs @@ -69,7 +69,7 @@ const PlaybackMixin = { if (track?.info?.length) return track.info.length; if (track?.info?.duration != null) { if (typeof track.info.duration === "number") return track.info.duration; - if (typeof track.info.duration === "object" && track.info.duration.seconds != null) + if (typeof track.info.duration === "object" && track.info.duration?.seconds != null) return track.info.duration.seconds * 1000; } return 0; diff --git a/src/music/player/SearchMixin.mjs b/src/music/player/SearchMixin.mjs index 86bb1e7..4259f16 100644 --- a/src/music/player/SearchMixin.mjs +++ b/src/music/player/SearchMixin.mjs @@ -306,7 +306,7 @@ const SearchMixin = { if (ms === 0 && info.duration != null) { if (typeof info.duration === "number") { ms = info.duration; - } else if (typeof info.duration === "object" && info.duration.seconds != null) { + } else if (typeof info.duration === "object" && info.duration?.seconds != null) { ms = info.duration.seconds * 1000; } } diff --git a/src/services/lastfm/LastFmManager.mjs b/src/services/lastfm/LastFmManager.mjs index 93482d5..96eb8d1 100644 --- a/src/services/lastfm/LastFmManager.mjs +++ b/src/services/lastfm/LastFmManager.mjs @@ -243,8 +243,8 @@ export class LastFmManager { /** @private Extract the duration in seconds from a track object, supporting number (ms), object with .seconds, ISO 8601, or plain seconds. @param {object} track - Track object. @returns {string|number} Duration in seconds, or empty string if unavailable. */ _extractDurationSec(track) { - if (!track.duration) return ""; - if (typeof track.duration === "object" && track.duration.seconds) return track.duration.seconds; + if (!track?.duration) return ""; + if (typeof track.duration === "object" && track.duration?.seconds) return track.duration.seconds; if (typeof track.duration === "number") return Math.round(track.duration / 1000); if (typeof track.duration === "string") { const m = track.duration.match(/PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?/); @@ -260,9 +260,9 @@ export class LastFmManager { * @returns {boolean} True if the track meets the scrobble threshold. */ shouldScrobble(track, playedMs) { - const durationMs = typeof track.duration === "object" && track.duration.seconds + const durationMs = track?.duration?.seconds != null ? track.duration.seconds * 1000 - : typeof track.duration === "number" + : typeof track?.duration === "number" ? track.duration : null; diff --git a/src/ui/MessageHandler.mjs b/src/ui/MessageHandler.mjs index 0cbb8e4..a461178 100644 --- a/src/ui/MessageHandler.mjs +++ b/src/ui/MessageHandler.mjs @@ -398,6 +398,19 @@ export class MessageHandler { try { return new Message(await replyingTo.reply(opts, { ping: false }), this); } catch (err) { + if (err.code === ErrorCodes.MessageNotFound || err.code === "UNKNOWN_MESSAGE" || err.statusCode === 404 || String(err?.message ?? "").includes("Message wasn't found")) { + /* Referenced message was deleted before reply arrived — fall back to channel send */ + try { + const ch = replyingTo.channel ?? (typeof this.getChannel === "function" ? this.getChannel(replyingTo.channelId) : null); + if (ch && typeof ch.send === "function") { + return new Message(await ch.send(opts), this); + } + } catch (fallbackErr) { + logger.warn(`[MessageHandler] Fallback channel send failed in ${replyingTo.channelId ?? replyingTo.channel?.id}: ${fallbackErr.code ?? fallbackErr.message}`); + return null; + } + return null; + } if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) { logger.warn(`[MessageHandler] Cannot reply in channel ${replyingTo.channelId ?? replyingTo.channel?.id}: ${err.code ?? err.message}`); return null; @@ -420,6 +433,19 @@ export class MessageHandler { try { return new Message(await replyingTo.reply(message, { ping: false }), this); } catch (err) { + if (err.code === ErrorCodes.MessageNotFound || err.code === "UNKNOWN_MESSAGE" || err.statusCode === 404 || String(err?.message ?? "").includes("Message wasn't found")) { + /* Referenced message was deleted before reply arrived — fall back to channel send */ + try { + const ch = replyingTo.channel ?? (typeof this.getChannel === "function" ? this.getChannel(replyingTo.channelId) : null); + if (ch && typeof ch.send === "function") { + return new Message(await ch.send(message), this); + } + } catch (fallbackErr) { + logger.warn(`[MessageHandler] Fallback channel send failed in ${replyingTo.channelId ?? replyingTo.channel?.id}: ${fallbackErr.code ?? fallbackErr.message}`); + return null; + } + return null; + } if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) { logger.warn(`[MessageHandler] Cannot replyEmbed in channel ${replyingTo.channelId ?? replyingTo.channel?.id}: ${err.code ?? err.message}`); return null; @@ -442,6 +468,19 @@ export class MessageHandler { try { return new Message(await replyingTo.reply(payload, { ping: false }), this); } catch (err) { + if (err.code === ErrorCodes.MessageNotFound || err.code === "UNKNOWN_MESSAGE" || err.statusCode === 404 || String(err?.message ?? "").includes("Message wasn't found")) { + /* Referenced message was deleted before reply arrived — fall back to channel send */ + try { + const ch = replyingTo.channel ?? (typeof this.getChannel === "function" ? this.getChannel(replyingTo.channelId) : null); + if (ch && typeof ch.send === "function") { + return new Message(await ch.send(payload), this); + } + } catch (fallbackErr) { + logger.warn(`[MessageHandler] Fallback channel send failed in ${replyingTo.channelId ?? replyingTo.channel?.id}: ${fallbackErr.code ?? fallbackErr.message}`); + return null; + } + return null; + } if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) { logger.warn(`[MessageHandler] Cannot replyEmbed in channel ${replyingTo.channelId ?? replyingTo.channel?.id}: ${err.code ?? err.message}`); return null; diff --git a/src/utils/Utils.mjs b/src/utils/Utils.mjs index 7e1de22..4077472 100644 --- a/src/utils/Utils.mjs +++ b/src/utils/Utils.mjs @@ -372,7 +372,7 @@ export class Utils { if (includeDuration && track.duration) { const duration = typeof track.duration === "number" ? this.prettifyMS(track.duration) - : track.duration.timestamp || track.duration.seconds + : (track.duration?.timestamp || track.duration?.seconds) ? this.formatSeconds(track.duration.seconds) : "?:??"; return `${title} - ${author} [${duration}]`;