My bot doesn't play music sound

I tried several ways and still my bot is not playing the music sound on discord, I will send how my pathetic code is:
export async function playYoutube(message: DiscordMessage) {
if (message.author.bot) return;

const args = message.content.trim().split(/\s+/);
const command = args[0];
const action = args[1];
const urlOrSearch = args.slice(2).join(' ');

if (command === `${prefix}` && action === 'play' && urlOrSearch) {
const memberVoiceChannelId = message.member?.voice.channel?.id;
const serverdId = message.guildId as string;

if (!memberVoiceChannelId) {
message.reply("Cara, você precisa estar em uma call!");
return;
}

try {
const connection = joinVoiceChannel({
channelId: memberVoiceChannelId,
guildId: serverdId,
selfDeaf: false,
adapterCreator: message.guild?.voiceAdapterCreator as DiscordGatewayAdapterCreator
})
const audioData = await fetchAudioStream(urlOrSearch);

if (!audioData) {
message.reply("Cara, deu ruim, não consegui encontrar a música!");
return;
}

console.log("Log do audioData: ")
console.log(audioData);

const resource = createAudioResource(audioData.stream, { inputType: audioData.type });
const player = createAudioPlayer();

console.log("Log do resource: ")
console.log(resource)

// Listeners para debugging
player.on('error', (error) => {
console.error(`Erro no player: ${error.message}`);
});

player.on('stateChange', (oldState, newState) => {
console.log(`Player mudou de ${oldState.status} para ${newState.status}`);
});

connection.on('stateChange', (oldState, newState) => {
console.log(`Conexão mudou de ${oldState.status} para ${newState.status}`);
});

player.play(resource);
console.log("Log do player: ")
console.log(player)
message.reply(":musical_note: Tocando agora!");

return connection.subscribe(player);
} catch (error) {
console.error("Erro no método playYoutube:", error);
message.reply("Ocorreu um erro ao tentar tocar o áudio.");
}
}
}
export async function playYoutube(message: DiscordMessage) {
if (message.author.bot) return;

const args = message.content.trim().split(/\s+/);
const command = args[0];
const action = args[1];
const urlOrSearch = args.slice(2).join(' ');

if (command === `${prefix}` && action === 'play' && urlOrSearch) {
const memberVoiceChannelId = message.member?.voice.channel?.id;
const serverdId = message.guildId as string;

if (!memberVoiceChannelId) {
message.reply("Cara, você precisa estar em uma call!");
return;
}

try {
const connection = joinVoiceChannel({
channelId: memberVoiceChannelId,
guildId: serverdId,
selfDeaf: false,
adapterCreator: message.guild?.voiceAdapterCreator as DiscordGatewayAdapterCreator
})
const audioData = await fetchAudioStream(urlOrSearch);

if (!audioData) {
message.reply("Cara, deu ruim, não consegui encontrar a música!");
return;
}

console.log("Log do audioData: ")
console.log(audioData);

const resource = createAudioResource(audioData.stream, { inputType: audioData.type });
const player = createAudioPlayer();

console.log("Log do resource: ")
console.log(resource)

// Listeners para debugging
player.on('error', (error) => {
console.error(`Erro no player: ${error.message}`);
});

player.on('stateChange', (oldState, newState) => {
console.log(`Player mudou de ${oldState.status} para ${newState.status}`);
});

connection.on('stateChange', (oldState, newState) => {
console.log(`Conexão mudou de ${oldState.status} para ${newState.status}`);
});

player.play(resource);
console.log("Log do player: ")
console.log(player)
message.reply(":musical_note: Tocando agora!");

return connection.subscribe(player);
} catch (error) {
console.error("Erro no método playYoutube:", error);
message.reply("Ocorreu um erro ao tentar tocar o áudio.");
}
}
}
log generated:
5 Replies
d.js toolkit
d.js toolkit4w ago
- What are your intents? GuildVoiceStates is required to receive voice data! - Show what dependencies you are using -- generateDependencyReport() is exported from @discordjs/voice. - Try looking at common examples: https://github.com/discordjs/voice-examples. - 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! - Marked as resolved by OP
treble/luna
treble/luna4w ago
We do not support TOS violations which your code appears to be Anything using youtube is against tos
Rabbit
RabbitOP4w ago
Could you please tell me a way that doesn't violate any rules?
treble/luna
treble/luna4w ago
only playing audio you have the rights to using a tos compliant source
Rabbit
RabbitOP4w ago
So my code is not working because it is violating Youtube guidelines, I understand

Did you find this page helpful?