67 lines
1.7 KiB
JavaScript
67 lines
1.7 KiB
JavaScript
const { createAudioPlayer, createAudioResource, AudioPlayerStatus, joinVoiceChannel } = require('@discordjs/voice');
|
|
const fs = require('fs');
|
|
|
|
const queueMap = new Map();
|
|
|
|
function addToQueue(guildId, filePath) {
|
|
if (!queueMap.has(guildId)) {
|
|
queueMap.set(guildId, []);
|
|
}
|
|
queueMap.get(guildId).push(filePath);
|
|
}
|
|
|
|
function getQueue(guildId) {
|
|
return queueMap.get(guildId) || [];
|
|
}
|
|
|
|
function playNextInQueue(guildId, voiceChannel) {
|
|
const queue = getQueue(guildId);
|
|
if (queue.length === 0) return false;
|
|
|
|
const connection = joinVoiceChannel({
|
|
channelId: voiceChannel.id,
|
|
guildId: guildId,
|
|
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
|
|
});
|
|
|
|
const audioPlayer = createAudioPlayer();
|
|
|
|
const filePath = queue.shift();
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
console.error('Audio file not found:', filePath);
|
|
return false;
|
|
}
|
|
|
|
const resource = createAudioResource(filePath);
|
|
audioPlayer.play(resource);
|
|
connection.subscribe(audioPlayer);
|
|
|
|
audioPlayer.on(AudioPlayerStatus.Idle, () => {
|
|
if (queue.length > 0) {
|
|
playNextInQueue(guildId, voiceChannel);
|
|
} else {
|
|
connection.destroy();
|
|
}
|
|
fs.unlink(filePath, (err) => {
|
|
if (err) console.error('Error deleting file:', filePath, err);
|
|
});
|
|
});
|
|
|
|
audioPlayer.on('error', (err) => {
|
|
console.error('AudioPlayer error:', err);
|
|
connection.destroy();
|
|
});
|
|
|
|
return true;
|
|
}
|
|
|
|
function skipTrack(guildId) {
|
|
const queue = getQueue(guildId);
|
|
if (queue.length > 0) {
|
|
queue.shift();
|
|
}
|
|
}
|
|
|
|
module.exports = { addToQueue, getQueue, playNextInQueue, skipTrack };
|