Advertisement
rjarmon

Discord Music Feature - YouTube Links

Dec 29th, 2022
1,238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { createAudioResource, createAudioPlayer, joinVoiceChannel, NoSubscriberBehavior } = require('@discordjs/voice');
  2. const { EmbedBuilder } = require('discord.js');
  3. const ytdl = require('ytdl-core');
  4. const { musicChannelId } = require('../config.json')
  5.  
  6. module.exports = async (client) => {
  7.    
  8.     const musicChannel = client.channels.cache.get(musicChannelId)
  9.  
  10.     const connection = joinVoiceChannel({
  11.             channelId: musicChannel.id,
  12.             guildId: musicChannel.guild.id,
  13.             adapterCreator: musicChannel.guild.voiceAdapterCreator,
  14.         });
  15.        
  16.         const player = createAudioPlayer({
  17.             behaviors: {
  18.                 noSubscriber: NoSubscriberBehavior.Pause
  19.             }
  20.         })
  21.  
  22.     const trackList = ['Track 1', 'Track 2', 'Track 3', 'Track 4'];
  23.  
  24.     const songList = ['https://www.youtube.com/watch?v=Track1', 'https://www.youtube.com/watch?v=Track2', 'https://www.youtube.com/watch?v=Track3', 'https://www.youtube.com/watch?v=-Track4'];
  25.  
  26.     const artistList = ['Artist 1', 'Artist 2', 'Artist 3', 'Artist 4'];
  27.  
  28.     let trackPosition = 0;
  29.  
  30.     const playSongs = async () => {
  31.    
  32.         player.on('error', error => {
  33.             const time = new Date().toLocaleString();
  34.             console.error('Error at ' + time + ':', error);
  35.         });
  36.    
  37.         let currentSong = songList[trackPosition];
  38.         let currentTrack = trackList[trackPosition];
  39.         let currentArtist = artistList[trackPosition];
  40.  
  41.         const videoId = currentSong.slice(32)
  42.  
  43.         const trackImage = `https://i.ytimg.com/vi/${videoId}/maxresdefault.jpg`;
  44.  
  45.         const stream = ytdl(currentSong, {
  46.             filter: 'audioonly',
  47.             fmt: "mp3",
  48.             highWaterMark: 1 << 62,
  49.             liveBuffer: 1 << 62,
  50.             dlChunkSize: 0, //disabling chunking is recommended in discord bot
  51.             bitrate: 128,
  52.             quality: "lowestaudio"
  53.         });
  54.  
  55.         const resource = createAudioResource(stream, { inlineVolume: true })
  56.         resource.volume.setVolume(0.1);
  57.         player.play(resource)
  58.  
  59.         const nowPlaying = new EmbedBuilder()
  60.             .setColor('#4fbaf6')
  61.             .setTitle(`**Currently playing:**`)
  62.             .addFields({ name: 'Title:', value: currentTrack, inline: true })
  63.  
  64.             if (artistList.length >= 1) {
  65.                 nowPlaying.addFields({ name: 'Artist:', value: currentArtist, inline: true })
  66.             }
  67.  
  68.             nowPlaying.addFields({ name: 'Link:', value: currentSong})
  69.  
  70.         const previousMessages = await musicChannel.messages.fetch();
  71.         if (previousMessages) await musicChannel.bulkDelete(previousMessages).catch(console.error);
  72.  
  73.         await musicChannel.send({ content: trackImage });
  74.         await musicChannel.send({ embeds: [nowPlaying] });
  75.  
  76.         await trackPosition++;
  77.  
  78.         if (trackPosition >= songList.length) trackPosition = 0;
  79.  
  80.     }
  81.  
  82.     connection.subscribe(player);
  83.  
  84.     playSongs();
  85.  
  86.     player.on('idle', playSongs);
  87.    
  88. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement