I'm building a Discord Bot for Play Youtube musics on my friend's server. Btw, When I run "node ." everything is ok.. But when I try to insert a command on Discord Server like a URL YouTube video, this error is printed on terminal..
Play.js file..
const { SlashCommandBuilder } = require("@discordjs/builders")
const { MessageEmbed, EmbedBuilder } = require("discord.js")
const { QueryType, Player } = require("discord-player");
const { YouTubeExtractor } = require("@discord-player/extractor");
module.exports = {
data: new SlashCommandBuilder()
.setName("play")
.setDescription("play a song from YouTube.")
.addSubcommand(subcommand =>
subcommand
.setName("search")
.setDescription("Searches for a song and plays it")
.addStringOption(option =>
option.setName("searchterms").setDescription("search keywords").setRequired(true)
)
)
.addSubcommand(subcommand =>
subcommand
.setName("playlist")
.setDescription("Plays a playlist from YT")
.addStringOption(option => option.setName("url").setDescription("the playlist's url").setRequired(true))
)
.addSubcommand(subcommand =>
subcommand
.setName("song")
.setDescription("Plays a single song from YT")
.addStringOption(option => option.setName("url").setDescription("the song's url").setRequired(true))
),
execute: async ({ client, interaction }) => {
// Make sure the user is inside a voice channel
if (!interaction.member.voice.channel) return interaction.reply("You need to be in a Voice Channel to play a song.");
// Create a play queue for the server
// const queue = await client.player.createQueue(interaction.guild);
const queue = await client.player.nodes.create(interaction.guild)
// Wait until you are connected to the channel
if (!queue.connection) await queue.connect(interaction.member.voice.channel)
let embed = new EmbedBuilder()
if (interaction.options.getSubcommand() === "song") {
let url = interaction.options.getString("url")
// Search for the song using the discord-player
const result = await client.player.search(url, {
requestedBy: interaction.user,
searchEngine: YouTubeExtractor.identifier
})
// finish if no tracks were found
if (result.tracks.length === 0)
return interaction.reply("No results")
// Add the track to the queue
const song = result.tracks[0]
await queue.addTrack(song)
embed
.setDescription(`**[${song.title}](${song.url})** has been added to the Queue`)
.setThumbnail(song.thumbnail)
.setFooter({ text: `Duration: ${song.duration}`})
}
else if (interaction.options.getSubcommand() === "playlist") {
// Search for the playlist using the discord-player
let url = interaction.options.getString("url")
const result = await client.player.search(url, {
requestedBy: interaction.user,
searchEngine: QueryType.YOUTUBE_PLAYLIST
})
if (result.tracks.length === 0)
return interaction.reply(`No playlists found with ${url}`)
// Add the tracks to the queue
const playlist = result.playlist
await queue.addTracks(result.tracks)
embed
.setDescription(`**${result.tracks.length} songs from [${playlist.title}](${playlist.url})** have been added to the Queue`)
.setThumbnail(playlist.thumbnail)
}
else if (interaction.options.getSubcommand() === "search") {
// Search for the song using the discord-player
let url = interaction.options.getString("searchterms")
const result = await client.player.search(url, {
requestedBy: interaction.user,
searchEngine: QueryType.AUTO
})
// finish if no tracks were found
if (result.tracks.length === 0)
return interaction.editReply("No results")
// Add the track to the queue
const song = result.tracks[0]
await queue.addTrack(song)
embed
.setDescription(`**[${song.title}](${song.url})** has been added to the Queue`)
.setThumbnail(song.thumbnail)
.setFooter({ text: `Duration: ${song.duration}`})
}
// Play the song
if (!queue.playing) await queue.play()
// Respond with the embed containing information about the player
await interaction.reply({
embeds: [embed]
})
},
}
Index.js file
require('dotenv').config();
const {REST} = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { Client, Intents, Collection, GatewayIntentBits } = require('discord.js');
const { Player } = require("discord-player")
const fs = require('fs');
const path = require('path');
const client = new Client({
intents: [GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildVoiceStates,
]
});
// List of all commands
const commands = [];
client.commands = new Collection();
const commandsPath = path.join(__dirname, "commands"); // E:\yt\discord bot\js\intro\commands
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for(const file of commandFiles)
{
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
commands.push(command.data.toJSON());
}
// Add the player on the client
client.player = new Player(client, {
ytdlOptions: {
quality: "highestaudio",
highWaterMark: 1 << 25
},
})
client.on("ready", () => {
// Get all ids of the servers
const guild_ids = client.guilds.cache.map(guild => guild.id);
const rest = new REST({version: '9'}).setToken(process.env.TOKEN);
for (const guildId of guild_ids)
{
rest.put(Routes.applicationGuildCommands(process.env.CLIENT_ID, guildId),
{body: commands})
.then(() => console.log('Successfully updated commands for guild ' + guildId))
.catch(console.error);
}
});
client.on("interactionCreate", async interaction => {
if(!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if(!command) return;
try
{
await command.execute({client, interaction});
}
catch(error)
{
console.error(error);
await interaction.reply({content: "There was an error executing this command"});
}
});
client.login(process.env.TOKEN);
Terminal code:
Successfully updated commands for guild 383020755790725121
Successfully updated commands for guild 1171689059337383956
(node:18658) [NoExtractors] Warning: Skipping extractors execution since zero extractors were registered
(Use node --trace-warnings ...
to show where the warning was created)
If don't see that you load any extractor thats why its telling you that no extractors are registered to load the default extractors just insert at the top of your play command: