bot joins to voice channel, doesn't play any sound, times out and disconnects

hi there! im following the basic example repo, although is a common error, i wonder, since i basically have the same code as the repo
const player = createAudioPlayer();

const playSong = () => {
const resource = createAudioResource(
"https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3",
{ inputType: StreamType.Arbitrary }
);
player.play(resource);

return entersState(player, AudioPlayerStatus.Playing, 5000);
};

// TODO: Refactor in its own file
const connectToChannel = async (channel: VoiceBasedChannel) => {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: createDiscordJSAdapter(channel),
selfDeaf: false,
selfMute: false,
});

// FIX: It connects but the status is not correctly set so it never plays
// FIX: Times out instead of entering idle
try {
await entersState(connection, VoiceConnectionStatus.Ready, 30_000);

return connection;
} catch (err) {
connection.destroy();

throw err;
}
};
const player = createAudioPlayer();

const playSong = () => {
const resource = createAudioResource(
"https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3",
{ inputType: StreamType.Arbitrary }
);
player.play(resource);

return entersState(player, AudioPlayerStatus.Playing, 5000);
};

// TODO: Refactor in its own file
const connectToChannel = async (channel: VoiceBasedChannel) => {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: createDiscordJSAdapter(channel),
selfDeaf: false,
selfMute: false,
});

// FIX: It connects but the status is not correctly set so it never plays
// FIX: Times out instead of entering idle
try {
await entersState(connection, VoiceConnectionStatus.Ready, 30_000);

return connection;
} catch (err) {
connection.destroy();

throw err;
}
};
6 Replies
d.js toolkit
d.js toolkit2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
hapless.dev
hapless.devOP2y ago
const discord_token = process.env.DISCORD_TOKEN;

console.log("Bot is starting...");

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});

// TODO: Refactor the awaiting in ready.ts
//ready(client);

client.on(Events.ClientReady, async () => {
console.log("Discord.js client is ready!");

try {
await client.application?.commands.set(Commands);
await playSong();
console.log("Song is ready to play!");
} catch (error) {
console.error(error);
}
});

// TODO: Refactor and make play command
client.on("messageCreate", async (message) => {
if (!message.guild) return;

if (message.content === "-join") {
const channel = message.member?.voice.channel;

if (channel) {
try {
const connection = await connectToChannel(channel);

connection.subscribe(player);
await message.reply("Playing now!");
} catch (error) {
console.error(error);
}
} else {
void message.reply("Join a voice channel then try again!");
}
}
});

interactionCreate(client);

void client.login(discord_token);
const discord_token = process.env.DISCORD_TOKEN;

console.log("Bot is starting...");

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});

// TODO: Refactor the awaiting in ready.ts
//ready(client);

client.on(Events.ClientReady, async () => {
console.log("Discord.js client is ready!");

try {
await client.application?.commands.set(Commands);
await playSong();
console.log("Song is ready to play!");
} catch (error) {
console.error(error);
}
});

// TODO: Refactor and make play command
client.on("messageCreate", async (message) => {
if (!message.guild) return;

if (message.content === "-join") {
const channel = message.member?.voice.channel;

if (channel) {
try {
const connection = await connectToChannel(channel);

connection.subscribe(player);
await message.reply("Playing now!");
} catch (error) {
console.error(error);
}
} else {
void message.reply("Join a voice channel then try again!");
}
}
});

interactionCreate(client);

void client.login(discord_token);
"dependencies": {
"@discordjs/core": "^0.6.0",
"@discordjs/opus": "^0.9.0",
"@discordjs/voice": "^0.16.0",
"discord-api-types": "^0.37.43",
"discord.js": "^14.11.0",
"dotenv": "^16.1.4",
"ffmpeg": "^0.0.4",
"libsodium-wrappers": "^0.7.11",
"nodemon": "^2.0.22",
"sodium": "^3.0.2"
}
"dependencies": {
"@discordjs/core": "^0.6.0",
"@discordjs/opus": "^0.9.0",
"@discordjs/voice": "^0.16.0",
"discord-api-types": "^0.37.43",
"discord.js": "^14.11.0",
"dotenv": "^16.1.4",
"ffmpeg": "^0.0.4",
"libsodium-wrappers": "^0.7.11",
"nodemon": "^2.0.22",
"sodium": "^3.0.2"
}
yes, all this is the index.ts and i just followed the repo which abstracts the VoiceAdapter to the function in adapter.ts in the repo, let me try it out!
hapless.dev
hapless.devOP2y ago
GitHub
voice-examples/basic/src/main.ts at main · discordjs/voice-examples
A collection of examples of how to use @discordjs/voice in your projects - voice-examples/basic/src/main.ts at main · discordjs/voice-examples
hapless.dev
hapless.devOP2y ago
i plan to refactor it but when i've used my own ready.ts to instatiate the client and parse my commands but when i call the playSong(), player instance and adapter that the repo calls on index.ts the functions never get triggered because they dont log anything
d.js docs
d.js docs2y ago
property Guild#voiceAdapterCreator The voice state adapter for this guild that can be used with @discordjs/voice to play audio in voice and stage channels.
hapless.dev
hapless.devOP2y ago
let me try it brb weird right? i saw the discordjs.guide and all, and copied the code by hand, reading the types and interfaces but couldnt get why it did it like that instead of using the normal creator, its a shame that actual .ts implementations are so scarce for the discord api huh, it worked with the normal adapter 10 months is old? haha wow, i thought it was kinda recent, this api and its ecosystem go fast af gotta admit
Want results from more Discord servers?
Add your server