Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const path = require('path');
- const { createAudioResource, createAudioPlayer, joinVoiceChannel, NoSubscriberBehavior } = require('@discordjs/voice');
- const { EmbedBuilder, AttachmentBuilder } = require('discord.js');
- const { musicChannelId } = require('../config.json')
- module.exports = async (client) => {
- // Get the correct voice channel for the server
- const musicChannel = client.channels.cache.get(musicChannelId)
- // Join the voice channel
- const connection = joinVoiceChannel({
- channelId: musicChannel.id,
- guildId: musicChannel.guild.id,
- adapterCreator: musicChannel.guild.voiceAdapterCreator,
- });
- // Create the audio player
- const player = createAudioPlayer({
- behaviors: {
- noSubscriber: NoSubscriberBehavior.Pause
- }
- })
- // Get the folder containing the song files
- const songsFolder = path.join(__dirname, '../music-feature/songs')
- const imageFolder = path.join(__dirname, '../music-feature/images')
- // Read the contents of the folders
- fs.readdir(songsFolder, (err, songFiles) => {
- if (err) {
- console.log(err);
- return;
- }
- fs.readdir(imageFolder, (err, imageFiles) => {
- if (err) {
- console.log(err);
- return;
- }
- // Iterate through the song files and play them
- let trackPosition = 0;
- const playNextSong = async () => {
- const songFile = songFiles[trackPosition];
- const imageFile = imageFiles[trackPosition];
- const song = `${songsFolder}/${songFile}`;
- const image = `${imageFolder}/${imageFile}`;
- const songName = path.parse(song).name;
- const attachment = new AttachmentBuilder(image, { name: imageFile } )
- //TODO Update artistList with the name of the artists in the correct order
- const artistList = ['Artist 1', 'Artist 2', 'Artist 3', 'Artist 4']
- const resource = createAudioResource(song, { inlineVolume: true })
- player.play(resource);
- // Create an embed with the track and artist names
- const nowPlaying = new EmbedBuilder()
- .setColor('#4fbaf6')
- .setTitle(`**Currently playing:**`)
- .addFields({ name: 'Title:', value: songName, inline: true })
- if (artistList.length >= 1) {
- nowPlaying.addFields({ name: 'Artist:', value: artistList[trackPosition], inline: true })
- }
- // Find and delete previous messages in the channel
- const previousMessages = await musicChannel.messages.fetch()
- if (previousMessages) await musicChannel.bulkDelete(previousMessages).catch(console.error)
- // Send track image and details messages
- await musicChannel.send({ files: [attachment] })
- await musicChannel.send({ embeds: [nowPlaying] })
- // Increment to the next song
- await trackPosition++;
- // Upon reaching the end of the playlist, loop back to the top
- if (trackPosition >= songFiles.length) trackPosition = 0;
- };
- connection.subscribe(player)
- // Plays songs
- playNextSong();
- // Once current song ends, advance to the next song
- player.on('idle', playNextSong);
- });
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement