Discord Voice Bot
I'm new to setting up discord bots and have been working on some small functions. Just trying to learn how things work. I am wanting to get my bot to join a discord voice channel and i have searched in multiple different places and tried what others have reccomended but I can't get my bot to join a voice channel. I was using what the discord.js website said to use. But I have so far been unable to get it to even join a channel, let alone play a sound.
"dependencies": {
"@discordjs/builders": "^1.6.3",
"@discordjs/opus": "^0.9.0",
"@discordjs/rest": "^1.7.1",
"@discordjs/voice": "^0.16.0",
"discord-api-types": "^0.37.48",
"discord-player": "^6.6.1",
"discord.js": "^14.11.0",
"dotenv": "^16.3.1",
"libsodium-wrappers": "^0.7.11"
}
I've tried installing several different dependencies
The https://discordjs.guide/voice/voice-connections.html#cheat-sheet
Says to use :
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
With this I get an error about channel. I've tried putting in the actual channel ids and still couldnlt get it to work.
I've seen several people talk about using "connectToChannel(channel)"
but i get an error about connectToChannel is undefined.
Any help at all or if you send me a place that can explain it to me, I would appreciate it. Thanks
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
5 Replies
- 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.
- Issue solved? Press the button!before actually addressing your issue, I'd just like to mention that you shouldn't install
@discordjs/builders
, @discordjs/rest
, and discord-api-types
separately
just use the versions that discord.js depends ondiscord.js includes multiple sub-packages, installing these separately can mess with internal code:
to actually address your issue
const connection = joinVoiceChannel({ channelId: channel.id, guildId: channel.guild.id, adapterCreator: channel.guild.voiceAdapterCreator, });this is the correct means of connecting to a voice channel on the assumption
channel
is a voice based channel object from discord.js
With this I get an error about channel.what error?
I've tried putting in the actual channel ids and still couldnlt get it to work.could you share your code for that? are you sure your ids were correct and were strings as opposed to numbers? (I hope I don't need to ask you to make sure the channel you're attempting to connect to is a voice channel)
I've seen several people talk about using "connectToChannel(channel)" but i get an error about connectToChannel is undefined.that's because it's not a global function, nor is it one that comes from
@discordjs/voice
it's something that some have copy pasted while following one of the (slightly outdated) examples
https://github.com/discordjs/voice-examples/blob/266d250d34abf352b320be1781b6f39146eefcb3/basic/src/main.ts#L55
also as a side note, it'd be best to use #djs-voice for @discordjs/voice
related questions in futureThank you so much. This got it working. I hadn't created the function for connecttoChannel like you said! That github gave me a great start as it was typescript and a couple of the things were outdated like you said but that was exactly what I needed. I will post in the correct channel next time. I do apologize for that!