-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.js
More file actions
225 lines (199 loc) · 6.59 KB
/
player.js
File metadata and controls
225 lines (199 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// ✅ VERSION CORRIGÉE DU FICHIER `player.js`
const { createAudioPlayer, createAudioResource, AudioPlayerStatus, StreamType } = require('@discordjs/voice');
const fs = require('fs');
const path = require('path');
const ffmpeg = require('fluent-ffmpeg');
const { createNowPlayingEmbed } = require('./embed/embed_player');
const {
getConnection,
getTextChannel,
getIsPlaying,
setIsPlaying,
getModeLecture
} = require('./state');
require('dotenv').config();
const MUSIC_API_KEY = process.env.API_MUSIC_KEY;
const MUSIC_FOLDER = './musique';
const VOIX_INFO = path.join('./alerte', 'voix_info.mp3');
let progressInterval = null;
let duration = 240; // Durée par défaut si inconnue
let elapsed = 0;
let lastEmbedMessage = null;
let alreadyWarnedAboutApiError = false;
let currentTrack = null;
function getAudioDuration(filePath) {
return new Promise((resolve, reject) => {
ffmpeg.ffprobe(filePath, (err, metadata) => {
if (err) return reject(err);
resolve(Math.floor(metadata.format.duration)); // durée en secondes
});
});
}
function setLastEmbedMessage(msg) {
lastEmbedMessage = msg;
}
function deleteLastEmbedMessage() {
if (lastEmbedMessage) {
try {
lastEmbedMessage.delete();
} catch (e) {
console.warn("⚠️ Impossible de supprimer le message embed :", e.message);
}
lastEmbedMessage = null;
}
}
const player = createAudioPlayer();
let infoInterval = null;
async function fetchZenSoundUrl() {
const url = `https://freesound.org/apiv2/search/text/?query=meditation&fields=id,name,previews,duration&filter=duration:[30 TO 300]&sort=score&token=${MUSIC_API_KEY}`;
const response = await fetch(url, {
headers: {
'User-Agent': 'ZenBot/1.0 (contact@example.com)'
}
});
const data = await response.json();
if (!data.results || data.results.length === 0) throw new Error("Aucun résultat trouvé");
const randomSound = data.results[Math.floor(Math.random() * data.results.length)];
return {
name: randomSound.name,
url: randomSound.previews['preview-lq-mp3'],
duration: Math.floor(randomSound.duration)
};
}
function clearProgress() {
if (progressInterval) {
clearInterval(progressInterval);
progressInterval = null;
}
elapsed = 0;
}
function getRandomTrack() {
const files = fs.readdirSync(MUSIC_FOLDER).filter(f => f.endsWith('.mp3') && f !== 'voix_info.mp3');
if (files.length === 0) return null;
const track = files[Math.floor(Math.random() * files.length)];
return {
name: track,
path: path.join(MUSIC_FOLDER, track)
};
}
async function playRandomMusic(connection, textChannel, botAvatarURL) {
if (getIsPlaying()) return;
setIsPlaying(true);
clearInterval(infoInterval);
const mode = getModeLecture();
if (!connection) {
console.warn("❌ Aucun salon vocal actif. Connexion absente.");
if (textChannel) textChannel.send("❌ Le bot n'est pas connecté à un salon vocal.");
setIsPlaying(false);
return;
}
try {
let resource, name;
let triedAPI = false;
if (mode === 'api' || mode === 'mix') {
try {
const sound = await fetchZenSoundUrl();
const stream = await fetch(sound.url).then(res => res.body);
resource = createAudioResource(stream, { inputType: StreamType.Arbitrary });
name = `[API] ${sound.name}`;
duration = sound.duration;
triedAPI = true;
} catch (e) {
if (mode === 'api') throw e;
if (textChannel) textChannel.send("⚠️ API inaccessible, utilisation de la musique locale.");
const local = getRandomTrack();
if (!local) throw new Error("Aucune musique locale trouvée");
resource = createAudioResource(local.path);
name = `[Local] ${local.name}`;
duration = await getAudioDuration(local.path);
}
} else {
const local = getRandomTrack();
if (!local) throw new Error("Aucune musique locale trouvée");
resource = createAudioResource(local.path);
name = `[Local] ${local.name}`;
duration = await getAudioDuration(local.path);
}
elapsed = 0;
connection.subscribe(player);
player.play(resource);
currentTrack = {
name,
source: triedAPI ? "API" : "Locale",
elapsed,
duration
};
if (textChannel) {
const { embed, file } = createNowPlayingEmbed(name, triedAPI ? "API" : "Locale", botAvatarURL, elapsed, duration);
const sent = await textChannel.send({ embeds: [embed], files: [file] });
setLastEmbedMessage(sent);
progressInterval = setInterval(async () => {
elapsed += 20;
if (elapsed >= duration) {
clearInterval(progressInterval);
return;
}
const { embed } = createNowPlayingEmbed(name, triedAPI ? "API" : "Locale", botAvatarURL, elapsed, duration);
try {
await sent.edit({ embeds: [embed] });
} catch (err) {
clearInterval(progressInterval);
}
}, 20000);
}
} catch (err) {
console.error('❌ Erreur de lecture zen :', err.message);
const infoResource = createAudioResource(VOIX_INFO);
console.warn('🔁 Lecture répétée de voix_info.mp3');
connection.subscribe(player);
player.play(infoResource);
const local = getRandomTrack();
if (!local) {
currentTrack = null;
throw new Error("Aucune musique locale trouvée");
}
const fallbackResource = createAudioResource(local.path);
const name = `[Local fallback] ${local.name}`;
duration = await getAudioDuration(local.path);
connection.subscribe(player);
player.play(fallbackResource);
}
if (textChannel && !alreadyWarnedAboutApiError) {
textChannel.send("⚠️ API inaccessible, utilisation de la musique locale.");
alreadyWarnedAboutApiError = true;
}
player.removeAllListeners(AudioPlayerStatus.Idle);
player.on(AudioPlayerStatus.Idle, () => {
clearProgress();
setIsPlaying(false);
if (getConnection() && getConnection().state.status !== "destroyed") {
setTimeout(() => {
playRandomMusic(getConnection(), getTextChannel(), botAvatarURL);
}, 10000);
}
});
}
let lastFarewellMessage = null;
function setLastFarewellMessage(msg) {
lastFarewellMessage = msg;
}
function deleteLastFarewellMessage() {
if (lastFarewellMessage) {
try {
lastFarewellMessage.delete();
} catch (e) {
console.warn("⚠️ Impossible de supprimer le message d'au revoir :", e.message);
}
lastFarewellMessage = null;
}
}
module.exports = {
playRandomMusic,
player,
clearProgress,
setLastEmbedMessage,
deleteLastEmbedMessage,
setLastFarewellMessage,
deleteLastFarewellMessage,
currentTrack
};