fix(entrance-exit): Trigger auch bei Channel-Wechsel; zusätzliche Logs; robustes Join falls keine aktive Verbindung

This commit is contained in:
vibe-bot 2025-08-10 23:50:51 +02:00
parent 64d2e91efa
commit 9f7aa5fc94

View file

@ -221,6 +221,7 @@ async function playFilePath(guildId: string, channelId: string, filePath: string
selfMute: false, selfMute: false,
selfDeaf: false selfDeaf: false
}); });
// Reuse bestehenden Player falls vorhanden
const player = state.player ?? createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Play } }); const player = state.player ?? createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Play } });
connection.subscribe(player); connection.subscribe(player);
state = { connection, player, guildId, channelId, currentVolume: state.currentVolume ?? getPersistedVolume(guildId) }; state = { connection, player, guildId, channelId, currentVolume: state.currentVolume ?? getPersistedVolume(guildId) };
@ -229,6 +230,23 @@ async function playFilePath(guildId: string, channelId: string, filePath: string
attachVoiceLifecycle(state, guild); attachVoiceLifecycle(state, guild);
} }
} catch {} } catch {}
// Falls keine aktive Verbindung existiert (oder nach destroy), sicherstellen
if (!getVoiceConnection(guildId)) {
const connection = joinVoiceChannel({
channelId,
guildId,
adapterCreator: guild.voiceAdapterCreator as any,
selfMute: false,
selfDeaf: false
});
const player = state?.player ?? createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Play } });
connection.subscribe(player);
state = { connection, player, guildId, channelId, currentVolume: state?.currentVolume ?? getPersistedVolume(guildId) };
guildAudioState.set(guildId, state);
state.connection = await ensureConnectionReady(connection, channelId, guildId, guild);
attachVoiceLifecycle(state, guild);
}
const useVolume = typeof volume === 'number' && Number.isFinite(volume) const useVolume = typeof volume === 'number' && Number.isFinite(volume)
? Math.max(0, Math.min(1, volume)) ? Math.max(0, Math.min(1, volume))
: (state.currentVolume ?? 1); : (state.currentVolume ?? 1);
@ -416,8 +434,9 @@ client.on(Events.VoiceStateUpdate, async (oldState: VoiceState, newState: VoiceS
const after = newState.channelId; const after = newState.channelId;
console.log(`${new Date().toISOString()} | VoiceStateUpdate user=${userId} before=${before ?? '-'} after=${after ?? '-'}`); console.log(`${new Date().toISOString()} | VoiceStateUpdate user=${userId} before=${before ?? '-'} after=${after ?? '-'}`);
// Entrance: Nutzer joint einem Channel // Entrance: Nutzer betritt einen Channel (erstmaliger Join oder Wechsel)
if (!before && after) { if (after && before !== after) {
console.log(`${new Date().toISOString()} | Entrance condition met for user=${userId} before=${before ?? '-'} after=${after}`);
const mapping = persistedState.entranceSounds ?? {}; const mapping = persistedState.entranceSounds ?? {};
const file = mapping[userId]; const file = mapping[userId];
if (file) { if (file) {
@ -432,8 +451,9 @@ client.on(Events.VoiceStateUpdate, async (oldState: VoiceState, newState: VoiceS
} }
} }
} }
// Exit: Nutzer verlässt einen Channel spiele im vorherigen Channel // Exit: Nutzer verlässt einen Channel (vollständig oder wechselt zu anderem)
if (before && !after) { if (before && (!after || after !== before)) {
console.log(`${new Date().toISOString()} | Exit condition met for user=${userId} before=${before} after=${after ?? '-'}`);
const mapping = persistedState.exitSounds ?? {}; const mapping = persistedState.exitSounds ?? {};
const file = mapping[userId]; const file = mapping[userId];
if (file) { if (file) {