Rabbit
DIAdiscord.js - Imagine an app
•Created by Rabbit on 12/26/2024 in #djs-voice
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:
log generated:
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.");
}
}
}
9 replies