music play but doesn't sound

js
const { ActionRowBuilder, ButtonBuilder, ButtonStyle, Events, Client, GatewayIntentBits, Partials, Collection, ActivityType, SelectMenuBuilder } = require("discord.js");


const { createReadStream } = require('node:fs');
const { join } = require('node:path');
const { AudioPlayerStatus, StreamType, createAudioResource, createAudioPlayer } = require('@discordjs/voice');

const { joinVoiceChannel, getVoiceConnection } = require('@discordjs/voice');


const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
],
partials: [
Partials.Channel,
Partials.Message,
Partials.User,
Partials.GuildMember,
],
});

// list of sounds to play
//use always "./files" like this
const sounds = [
'./audio1.mp3',
'./audio2.mp3',
'./audio3.mp3'
];

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
console.log(client.channels);
});


client.on('messageCreate', async message => {
if (message.content === '!play') {
// check if bot is in a voice channel
const channel = client.channels.cache.find(c => c.name === "・vc・");



const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
selfDeaf: false,
selfMute: false // this may also
});

const player = createAudioPlayer();

// pick a random sound to play
const sound = sounds[Math.floor(Math.random() * sounds.length)];



// Thanks
js
const { ActionRowBuilder, ButtonBuilder, ButtonStyle, Events, Client, GatewayIntentBits, Partials, Collection, ActivityType, SelectMenuBuilder } = require("discord.js");


const { createReadStream } = require('node:fs');
const { join } = require('node:path');
const { AudioPlayerStatus, StreamType, createAudioResource, createAudioPlayer } = require('@discordjs/voice');

const { joinVoiceChannel, getVoiceConnection } = require('@discordjs/voice');


const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
],
partials: [
Partials.Channel,
Partials.Message,
Partials.User,
Partials.GuildMember,
],
});

// list of sounds to play
//use always "./files" like this
const sounds = [
'./audio1.mp3',
'./audio2.mp3',
'./audio3.mp3'
];

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
console.log(client.channels);
});


client.on('messageCreate', async message => {
if (message.content === '!play') {
// check if bot is in a voice channel
const channel = client.channels.cache.find(c => c.name === "・vc・");



const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
selfDeaf: false,
selfMute: false // this may also
});

const player = createAudioPlayer();

// pick a random sound to play
const sound = sounds[Math.floor(Math.random() * sounds.length)];



// Thanks
7 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Spidey 🕸
Spidey 🕸OP3y ago
const resource = createAudioResource("./audio1.mp3", {
inputType: StreamType.WebmOpus,
inlineVolume: true,
metadata: {
title: 'A good song!',
},
});
resource.volume.setVolume(100);
// Not recommended - listen to errors from the audio player instead for most usecases!
resource.playStream.on('error', error => {
console.error('Error:', error.message, 'with track', resource.metadata.title);
});

player.play(resource);

player.on(AudioPlayerStatus.Playing, () => {
console.log('The audio player has started playing!');
});


}
})
const resource = createAudioResource("./audio1.mp3", {
inputType: StreamType.WebmOpus,
inlineVolume: true,
metadata: {
title: 'A good song!',
},
});
resource.volume.setVolume(100);
// Not recommended - listen to errors from the audio player instead for most usecases!
resource.playStream.on('error', error => {
console.error('Error:', error.message, 'with track', resource.metadata.title);
});

player.play(resource);

player.on(AudioPlayerStatus.Playing, () => {
console.log('The audio player has started playing!');
});


}
})
</> Idris
</> Idris3y ago
you forgot to subscribe the connection to the audio player
Spidey 🕸
Spidey 🕸OP3y ago
How? @idris1401
Jiron
Jiron3y ago
Plus, an mp3 isn't really Webm- You can therefore remove the entire inputtype option since the default is perfect for mp3
Spidey 🕸
Spidey 🕸OP3y ago
Hmm worked

Did you find this page helpful?