Help with !play song command.

I am a begginer at bot development and I want my bot to play song in the voice channel I am rn without using ytdl-core. How can I do this? I read all the voice pages on the Discord.js guide but still have no idea of how I am going to do this. Discord.js version: 14.14.1 Node version: 21.6.1 @discord/voice version: 0.16.1 @discord/opus version: 0.9.0
2 Replies
d.js toolkit
d.js toolkit12mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
андреас (kanye's version)
Here is my code:
const { joinVoiceChannel, getVoiceConnection, createAudioPlayer, createAudioResource } = require('@discordjs/voice');

module.exports = {
name: "play",
description: "Toca música do YouTube.",
async execute(bot, message, args) {
const url = args.join(" ");
const voiceChannel = message.member.voice.channel;

if(!url) return message.reply("Envie o comando com URL do YouTube!");
if (!voiceChannel) {
return message.reply("Entre em um canal de voz para executar este comando.");
}

const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
return message.reply("Eu preciso das permissões adequadas para entrar e falar no canal de voz.");
}

try {
if (!voiceChannel.guild.voiceConnection) {

} else {
message.reply("O bot já está em um canal de voz.");
}
} catch (error) {
console.error(error);
message.reply("Ocorreu um erro ao tentar tocar a música.");
}
}
};
const { joinVoiceChannel, getVoiceConnection, createAudioPlayer, createAudioResource } = require('@discordjs/voice');

module.exports = {
name: "play",
description: "Toca música do YouTube.",
async execute(bot, message, args) {
const url = args.join(" ");
const voiceChannel = message.member.voice.channel;

if(!url) return message.reply("Envie o comando com URL do YouTube!");
if (!voiceChannel) {
return message.reply("Entre em um canal de voz para executar este comando.");
}

const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
return message.reply("Eu preciso das permissões adequadas para entrar e falar no canal de voz.");
}

try {
if (!voiceChannel.guild.voiceConnection) {

} else {
message.reply("O bot já está em um canal de voz.");
}
} catch (error) {
console.error(error);
message.reply("Ocorreu um erro ao tentar tocar a música.");
}
}
};
Want results from more Discord servers?
Add your server