Spaces:
Runtime error
Runtime error
Update server.js
Browse files
server.js
CHANGED
|
@@ -4,7 +4,8 @@ const {
|
|
| 4 |
createAudioPlayer,
|
| 5 |
createAudioResource,
|
| 6 |
AudioPlayerStatus,
|
| 7 |
-
StreamType
|
|
|
|
| 8 |
} = require('@discordjs/voice');
|
| 9 |
const express = require('express');
|
| 10 |
const app = express();
|
|
@@ -25,27 +26,35 @@ const client = new Client({
|
|
| 25 |
});
|
| 26 |
|
| 27 |
let voiceConnection;
|
| 28 |
-
const player = createAudioPlayer(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
let aiStream = null;
|
| 30 |
|
| 31 |
-
// HÀM FIX: Quản lý luồng âm thanh thông minh hơn
|
| 32 |
function setupAudioPipeline() {
|
| 33 |
-
// Nếu
|
| 34 |
-
if (aiStream
|
|
|
|
|
|
|
| 35 |
|
| 36 |
aiStream = new PassThrough();
|
| 37 |
-
|
| 38 |
-
// WebmDemuxer giúp tách Opus từ WebM gửi từ Browser
|
| 39 |
const demuxer = new prism.opus.WebmDemuxer();
|
| 40 |
|
|
|
|
| 41 |
const resource = createAudioResource(aiStream.pipe(demuxer), {
|
| 42 |
inputType: StreamType.Opus,
|
| 43 |
-
inlineVolume: true
|
| 44 |
});
|
| 45 |
|
| 46 |
-
resource.volume
|
|
|
|
|
|
|
|
|
|
| 47 |
player.play(resource);
|
| 48 |
-
console.log("🎙️ Discord Player: Luồng giải mã đã kích hoạt.");
|
| 49 |
}
|
| 50 |
|
| 51 |
app.get('/join', (req, res) => {
|
|
@@ -59,11 +68,13 @@ app.get('/join', (req, res) => {
|
|
| 59 |
channelId: channel.id,
|
| 60 |
guildId: guildId,
|
| 61 |
adapterCreator: channel.guild.voiceAdapterCreator,
|
|
|
|
|
|
|
| 62 |
});
|
| 63 |
|
| 64 |
voiceConnection.subscribe(player);
|
| 65 |
-
setupAudioPipeline();
|
| 66 |
-
res.send("Bot đã vào phòng voice!");
|
| 67 |
});
|
| 68 |
|
| 69 |
io.on('connection', (socket) => {
|
|
@@ -71,12 +82,12 @@ io.on('connection', (socket) => {
|
|
| 71 |
|
| 72 |
socket.on('ai_audio_stream', (audioData) => {
|
| 73 |
if (audioData && audioData.byteLength > 0) {
|
| 74 |
-
//
|
| 75 |
-
if (
|
| 76 |
setupAudioPipeline();
|
| 77 |
}
|
| 78 |
|
| 79 |
-
if (aiStream.writable) {
|
| 80 |
aiStream.write(Buffer.from(audioData));
|
| 81 |
}
|
| 82 |
}
|
|
@@ -87,7 +98,6 @@ io.on('connection', (socket) => {
|
|
| 87 |
});
|
| 88 |
});
|
| 89 |
|
|
|
|
| 90 |
client.login(process.env.DISCORD_TOKEN);
|
| 91 |
-
server.listen(process.env.PORT || 7860, () =>
|
| 92 |
-
console.log(`🚀 Cầu nối Yue AI đang chạy`);
|
| 93 |
-
});
|
|
|
|
| 4 |
createAudioPlayer,
|
| 5 |
createAudioResource,
|
| 6 |
AudioPlayerStatus,
|
| 7 |
+
StreamType,
|
| 8 |
+
NoSubscriberBehavior
|
| 9 |
} = require('@discordjs/voice');
|
| 10 |
const express = require('express');
|
| 11 |
const app = express();
|
|
|
|
| 26 |
});
|
| 27 |
|
| 28 |
let voiceConnection;
|
| 29 |
+
const player = createAudioPlayer({
|
| 30 |
+
behaviors: {
|
| 31 |
+
noSubscriber: NoSubscriberBehavior.Play, // Tiếp tục chạy ngay cả khi không có ai nghe
|
| 32 |
+
},
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
let aiStream = null;
|
| 36 |
|
|
|
|
| 37 |
function setupAudioPipeline() {
|
| 38 |
+
// Nếu đang có luồng cũ thì kết thúc nó để làm sạch bộ nhớ
|
| 39 |
+
if (aiStream) {
|
| 40 |
+
aiStream.destroy();
|
| 41 |
+
}
|
| 42 |
|
| 43 |
aiStream = new PassThrough();
|
|
|
|
|
|
|
| 44 |
const demuxer = new prism.opus.WebmDemuxer();
|
| 45 |
|
| 46 |
+
// Pipe dữ liệu qua demuxer để lấy Opus chuẩn
|
| 47 |
const resource = createAudioResource(aiStream.pipe(demuxer), {
|
| 48 |
inputType: StreamType.Opus,
|
| 49 |
+
inlineVolume: true
|
| 50 |
});
|
| 51 |
|
| 52 |
+
if (resource.volume) {
|
| 53 |
+
resource.volume.setVolume(2.0); // Ép âm lượng to lên 200%
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
player.play(resource);
|
| 57 |
+
console.log("🎙️ Discord Player: Luồng giải mã đã kích hoạt và đang chờ dữ liệu...");
|
| 58 |
}
|
| 59 |
|
| 60 |
app.get('/join', (req, res) => {
|
|
|
|
| 68 |
channelId: channel.id,
|
| 69 |
guildId: guildId,
|
| 70 |
adapterCreator: channel.guild.voiceAdapterCreator,
|
| 71 |
+
selfDeaf: false, // TẮT gạch chéo tai nghe
|
| 72 |
+
selfMute: false // TẮT gạch chéo mic
|
| 73 |
});
|
| 74 |
|
| 75 |
voiceConnection.subscribe(player);
|
| 76 |
+
setupAudioPipeline();
|
| 77 |
+
res.send("Bot đã vào phòng voice và đã tháo tai nghe!");
|
| 78 |
});
|
| 79 |
|
| 80 |
io.on('connection', (socket) => {
|
|
|
|
| 82 |
|
| 83 |
socket.on('ai_audio_stream', (audioData) => {
|
| 84 |
if (audioData && audioData.byteLength > 0) {
|
| 85 |
+
// Nếu player bị Idle (hết dữ liệu cũ), khởi động lại pipeline
|
| 86 |
+
if (player.state.status === AudioPlayerStatus.Idle) {
|
| 87 |
setupAudioPipeline();
|
| 88 |
}
|
| 89 |
|
| 90 |
+
if (aiStream && aiStream.writable) {
|
| 91 |
aiStream.write(Buffer.from(audioData));
|
| 92 |
}
|
| 93 |
}
|
|
|
|
| 98 |
});
|
| 99 |
});
|
| 100 |
|
| 101 |
+
client.on('ready', () => console.log(`🤖 Bot ${client.user.tag} đã sẵn sàng!`));
|
| 102 |
client.login(process.env.DISCORD_TOKEN);
|
| 103 |
+
server.listen(process.env.PORT || 7860, () => console.log("🚀 Server đang chạy..."));
|
|
|
|
|
|