diff --git a/commands/filters.js b/commands/filters.js index fa2c225..650eaab 100644 --- a/commands/filters.js +++ b/commands/filters.js @@ -37,7 +37,7 @@ module.exports = { embeds: [ new EmbedBuilder() .setDescription( - `**Failed**\nThere are no tracks in the queue.` + `**Failed**\nThere are no tracks in the queue. Add tracks with \`/play\`!` ) .setColor(embedColors.colorWarning) ] diff --git a/commands/pause.js b/commands/pause.js index 18ce475..acd2cac 100644 --- a/commands/pause.js +++ b/commands/pause.js @@ -27,7 +27,7 @@ module.exports = { embeds: [ new EmbedBuilder() .setDescription( - `**Failed**\nThere are no tracks in the queue.` + `**Failed**\nThere are no tracks in the queue. Add tracks with \`/play\`!` ) .setColor(embedColors.colorWarning) ] diff --git a/commands/play.js b/commands/play.js index f8ef997..b2c7008 100644 --- a/commands/play.js +++ b/commands/play.js @@ -1,5 +1,5 @@ const { SlashCommandBuilder } = require('@discordjs/builders'); -const { useMainPlayer, useQueue } = require('discord-player'); +const { useMainPlayer } = require('discord-player'); const { EmbedBuilder } = require('discord.js'); const { embedColors } = require('../config.json'); @@ -29,7 +29,9 @@ module.exports = { const player = useMainPlayer(); const query = interaction.options.getString('query'); - const searchResult = await player.search(query); + const searchResult = await player.search(query, { + requestedBy: interaction.user + }); if (!searchResult || searchResult.tracks.length === 0) { return await interaction.editReply({ @@ -43,20 +45,22 @@ module.exports = { }); } - try { - const { track } = await player.play( - interaction.member.voice.channel, - searchResult.tracks[0].url, - { - requestedBy: interaction.user, - nodeOptions: { - leaveOnEmptyCooldown: 60000, - leaveOnEndCooldown: 60000, - leaveOnStopCooldown: 60000 - } + const { track } = await player.play( + interaction.member.voice.channel, + searchResult, + { + requestedBy: interaction.user, + nodeOptions: { + leaveOnEmptyCooldown: 60000, + leaveOnEndCooldown: 60000, + leaveOnStopCooldown: 60000, + maxSize: 10000, + maxHistorySize: 100 } - ); + } + ); + if (searchResult.playlist && searchResult.tracks.length > 1) { return await interaction.editReply({ embeds: [ new EmbedBuilder() @@ -65,74 +69,31 @@ module.exports = { iconURL: interaction.user.avatarURL() }) .setDescription( - `**Added to queue**\n\`[${track.duration}]\` **[${track.title}](${track.url})**` + `**Added playlist to queue**\n\`[${ + track.duration + }]\` **[${track.title}](${track.url})**\n\nAnd \`${ + searchResult.tracks.length - 1 + }\` more tracks... \`/queue\` to view all.` ) .setThumbnail(track.thumbnail) .setColor(embedColors.colorSuccess) ] }); - } catch (e) { - if (e.message.includes('Could not extract stream for this track')) { - if (searchResult.tracks.length > 1) { - try { - const { track } = await player.play( - interaction.member.voice.channel, - searchResult.tracks[1].url, - { - requestedBy: interaction.user, - nodeOptions: { - leaveOnEmptyCooldown: 60000, - leaveOnEndCooldown: 60000, - leaveOnStopCooldown: 60000 - } - } - ); - - return await interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setAuthor({ - name: interaction.user.username, - iconURL: interaction.user.avatarURL() - }) - .setDescription( - `**Added to queue**\n\`[${track.duration}]\` **[${track.title}](${track.url})**` - ) - .setThumbnail(track.thumbnail) - .setColor(embedColors.colorSuccess) - ] - }); - } catch (e) { - if ( - e.message.includes( - 'Could not extract stream for this track' - ) - ) { - return await interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setDescription( - `**Failed**\nFound a result for query \`${query}\` with source ${searchResult.tracks[1].url}, but was unable to extract audio stream from track.\n\nThis is most likely due to audio source blocking download from the bot. Please try another track or refine your query.` - ) - .setColor(embedColors.colorError) - ] - }); - } - } - } else { - return await interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setDescription( - `**Failed**\nFound a result for query \`${query}\` with source ${searchResult.tracks[0].url}, but was unable to extract audio stream from track.\n\nThis is most likely due to audio source blocking download from the bot or unsupported audio encoding. Please try another track or refine your query.` - ) - .setColor(embedColors.colorError) - ] - }); - } - } else { - throw e; - } } + + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setAuthor({ + name: interaction.user.username, + iconURL: interaction.user.avatarURL() + }) + .setDescription( + `**Added to queue**\n\`[${track.duration}]\` **[${track.title}](${track.url})**` + ) + .setThumbnail(track.thumbnail) + .setColor(embedColors.colorSuccess) + ] + }); } }; diff --git a/commands/queue.js b/commands/queue.js index d22598f..acb9a99 100644 --- a/commands/queue.js +++ b/commands/queue.js @@ -30,7 +30,7 @@ module.exports = { let queueString = ''; if (!queue) { - queueString = 'There are no tracks in the queue.'; + queueString = `**Failed**\nThere are no tracks in the queue. Add tracks with \`/play\`!`; return await interaction.editReply({ embeds: [ new EmbedBuilder() diff --git a/commands/remove.js b/commands/remove.js index b830144..72b1c3f 100644 --- a/commands/remove.js +++ b/commands/remove.js @@ -34,7 +34,7 @@ module.exports = { embeds: [ new EmbedBuilder() .setDescription( - `**Failed**\nThere are no tracks in the queue.` + `**Failed**\nThere are no tracks in the queue. Add tracks with \`/play\`!` ) .setColor(embedColors.colorWarning) ] diff --git a/commands/skip.js b/commands/skip.js index 7526f30..befc28f 100644 --- a/commands/skip.js +++ b/commands/skip.js @@ -35,7 +35,7 @@ module.exports = { embeds: [ new EmbedBuilder() .setDescription( - `**Failed**\nThere are no tracks in the queue.` + `**Failed**\nThere are no tracks in the queue. Add tracks with \`/play\`!` ) .setColor(embedColors.colorWarning) ] diff --git a/commands/volume.js b/commands/volume.js index f43f919..57347fe 100644 --- a/commands/volume.js +++ b/commands/volume.js @@ -34,7 +34,7 @@ module.exports = { embeds: [ new EmbedBuilder() .setDescription( - `**Failed**\nThere are no tracks playing or in the queue.` + `**Failed**\nThere are no tracks playing or in the queue. Add tracks with \`/play\`!` ) .setColor(embedColors.colorWarning) ] diff --git a/index.js b/index.js index 8f90e7b..b2e5881 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,8 @@ const fs = require('node:fs'); const Discord = require('discord.js'); const { EmbedBuilder } = require('discord.js'); -const { Player } = require('discord-player'); +const { Player, onBeforeCreateStream } = require('discord-player'); +const { stream } = require('yt-stream'); const { token, embedColors } = require('./config.json'); // Setup required permissions for the bot to work @@ -31,7 +32,23 @@ for (const file of systemCommandFiles) { } // Create a new Player, and attach it to the bot client. -const player = new Player(client); +const player = new Player(client, { + useLegacyFFmpeg: false +}); + +onBeforeCreateStream(async (track) => { + if (track.source === 'youtube') { + return ( + await stream(track.url, { + type: 'audio', + quality: 'high', + highWaterMark: 1 << 25 + }) + ).stream; + } + + return null; +}); player.events.on('error', (queue, error) => { // Emitted when the player queue encounters error @@ -65,7 +82,8 @@ client.once('ready', async () => { ); // This method will load all the extractors from the @discord-player/extractor package - player.extractors.loadDefault(); + await player.extractors.loadDefault(); + console.log(player.scanDeps()); // Set Discord status client.user.setActivity('/help', { diff --git a/package-lock.json b/package-lock.json index 203e982..8bdcf5c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,13 +9,13 @@ "version": "1.0.2", "license": "MIT", "dependencies": { - "@discord-player/extractor": "^4.3.1", + "@discord-player/extractor": "^4.4.0", "@discordjs/opus": "^0.9.0", "@discordjs/rest": "^1.7.1", "@discordjs/voice": "^0.16.0", - "discord-player": "^6.6.0", + "discord-player": "^6.6.1", "discord.js": "^14.11.0", - "play-dl": "^1.9.6" + "yt-stream": "^1.4.8" } }, "node_modules/@discord-player/equalizer": { @@ -24,9 +24,9 @@ "integrity": "sha512-U86em1cMtopXVuKFDxP8J2DcNOqwCAsYQ2tkzPoJvchsrezcI2cgvN3jtGt9FFECP3pLvSz96ZB9FOHZKG5Mqg==" }, "node_modules/@discord-player/extractor": { - "version": "4.3.1", - "resolved": "https://registry.npmjs.org/@discord-player/extractor/-/extractor-4.3.1.tgz", - "integrity": "sha512-f8EwbQkplZQdVb4pCazc+5zo57y4gkPCl4Zq+3nHhwD+lz03OSmFGfoU6Ho9InBbx6S6PthO6CSPyFXN3sH6nw==", + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/@discord-player/extractor/-/extractor-4.4.0.tgz", + "integrity": "sha512-TpHZk6ZFS5ot504iVAfnaJrGe2xv9yMauI+2zNLCmAYPeFe6YUbliXrSRoOmKoZi5n1ahkP2VurbmBhqKqEVkw==", "dependencies": { "file-type": "^16.5.4", "genius-lyrics": "^4.4.3", @@ -43,9 +43,9 @@ "integrity": "sha512-0kW6q4gMQN2B4Z4EzmUgXrKQSXXmyhjdZBBZ/6jSHZ9fh814oOu+JXP01VvtWHwTylI7qJHIctEWtSyjEubCJg==" }, "node_modules/@discord-player/utils": { - "version": "0.2.1", - "resolved": "https://registry.npmjs.org/@discord-player/utils/-/utils-0.2.1.tgz", - "integrity": "sha512-yPxfdO2N3i2YEEiwlLDNyuBEdnhV1mZaC7im2BI4FKn3ak1UAtVcbKeJhdd/va0A170+PZs3zUKVGldY0z/+ng==", + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@discord-player/utils/-/utils-0.2.2.tgz", + "integrity": "sha512-UklWUT7BcZEkBgywM9Cmpo2nwj3SQ9Wmhu6ml1uy/YRQnY8IRdZEHD84T2kfjOg4LVZek0ej1VerIqq7a9PAHQ==", "dependencies": { "@discordjs/collection": "^1.1.0" } @@ -490,13 +490,13 @@ "integrity": "sha512-rNif8IAv6duS2z47BMXq/V9kkrLfkAoiwpFY3sLxxbyKprk065zqf3HLTg4bEoxRSmi+Lhc7yqGDrG8C3j8GFA==" }, "node_modules/discord-player": { - "version": "6.6.0", - "resolved": "https://registry.npmjs.org/discord-player/-/discord-player-6.6.0.tgz", - "integrity": "sha512-lW5IcGLycJK6mNtpz5/QqKuhhpBVm475xlsVKEUhoS1CFNcLqob5nAlEXurKjlIhxR+fFglSZ8jmtdFjR8p50g==", + "version": "6.6.1", + "resolved": "https://registry.npmjs.org/discord-player/-/discord-player-6.6.1.tgz", + "integrity": "sha512-l72ql51uya3Uq5VrTPvR6xWBHOa3lI9oWTl7YDDHvofMXEYLgqAuDlyuelQ7AY+lUC7FOK7MoS2QgVFHeAciNw==", "dependencies": { "@discord-player/equalizer": "^0.2.2", "@discord-player/ffmpeg": "^0.1.0", - "@discord-player/utils": "^0.2.1", + "@discord-player/utils": "^0.2.2", "@discordjs/voice": "latest", "libsodium-wrappers": "^0.7.10" }, @@ -504,7 +504,7 @@ "url": "https://github.com/Androz2091/discord-player?sponsor=1" }, "peerDependencies": { - "@discord-player/extractor": "^4.3.1", + "@discord-player/extractor": "^4.4.0", "discord.js": "14.x", "youtube-sr": "4.x" } @@ -1026,22 +1026,6 @@ "url": "https://github.com/sponsors/Borewit" } }, - "node_modules/play-audio": { - "version": "0.5.2", - "resolved": "https://registry.npmjs.org/play-audio/-/play-audio-0.5.2.tgz", - "integrity": "sha512-ZAqHUKkQLix2Iga7pPbsf1LpUoBjcpwU93F1l3qBIfxYddQLhxS6GKmS0d3jV8kSVaUbr6NnOEcEMFvuX93SWQ==" - }, - "node_modules/play-dl": { - "version": "1.9.6", - "resolved": "https://registry.npmjs.org/play-dl/-/play-dl-1.9.6.tgz", - "integrity": "sha512-JW44bQbME9fNfGhGXQ/rdcsHr4BfgJabVlSgpS9QY/NscfprFH1asv+q9atrZThP3+hHIpgtFNABccg9rFWlwg==", - "dependencies": { - "play-audio": "^0.5.2" - }, - "engines": { - "node": ">=16.0.0" - } - }, "node_modules/prism-media": { "version": "1.3.5", "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-1.3.5.tgz", @@ -1363,6 +1347,11 @@ "version": "4.3.4", "resolved": "https://registry.npmjs.org/youtube-sr/-/youtube-sr-4.3.4.tgz", "integrity": "sha512-olSYcR80XigutCrePEXBX3/RJJrWfonJQj7+/ggBiWU0CzTDLE1q8+lpWTWCG0JpzhzILp/IB/Bq/glGqqr1TQ==" + }, + "node_modules/yt-stream": { + "version": "1.4.8", + "resolved": "https://registry.npmjs.org/yt-stream/-/yt-stream-1.4.8.tgz", + "integrity": "sha512-XtJ1njQ5xKSmW4FAk+YVliyV+XG5/ejAaANUqbyT5ya+COgW9hyswWh16mZJZzj+uEkRobzCId/oH4kP2VmTbQ==" } } } diff --git a/package.json b/package.json index 9354a08..f523d54 100644 --- a/package.json +++ b/package.json @@ -23,12 +23,12 @@ "deploy": "node ./deploy-commands.js && node ./deploy-system-commands.js" }, "dependencies": { - "@discord-player/extractor": "^4.3.1", + "@discord-player/extractor": "^4.4.0", "@discordjs/opus": "^0.9.0", "@discordjs/rest": "^1.7.1", "@discordjs/voice": "^0.16.0", - "discord-player": "^6.6.0", + "discord-player": "^6.6.1", "discord.js": "^14.11.0", - "play-dl": "^1.9.6" + "yt-stream": "^1.4.8" } }