how to know the channel id of voice channel so that i do not need to put my channel id manually

in my code while using joinvoicechannel function it asks for channel id is there any way so that i do not need to put my id mannually see the code for refrence const { SlashCommandBuilder } = require('discord.js'); const { joinVoiceChannel } = require('@discordjs/voice'); module.exports = { data: new SlashCommandBuilder() .setName('join') .setDescription('join voice channel in which you are'), async execute(interaction) { const voiceChannel = interaction.options.getChannel('channel') const connection = joinVoiceChannel({ channelId: "1086434561640108086", guildId: interaction.guild.id, adapterCreator: interaction.guild.voiceAdapterCreator, });
} }
42 Replies
d.js toolkit
d.js toolkitā€¢9mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - 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
ShompiFlen
ShompiFlenā€¢9mo ago
yea if you use the library inside a bot context (where you have access to the client at least) you can just programatically join the channels you want. In this case i see you have access to your interaction, and apparently you accept a channel option aswell. If the channel is a voice channel, you can just pass the voiceChannel.id to the channelId: option in the joinVoiceChannel() call i dont know why you didn't do that, you have the voice channel there
Toothless
ToothlessOPā€¢9mo ago
see my guildid says his value is intraction.guild.id i also want something like that so my bot automatic peek the value for channel id
ShompiFlen
ShompiFlenā€¢9mo ago
yea i am not understading what you want you say you want your bot to pick a value for the channel id but how what channel do you want it to join
Toothless
ToothlessOPā€¢9mo ago
in which i am in currently
ShompiFlen
ShompiFlenā€¢9mo ago
you can access the channel you are in through the interaction object interaction.member is you member.voice is your VoiceState voice.channel.id is the voice channel Id you are connected to
Toothless
ToothlessOPā€¢9mo ago
let me put this value and comeback its says this error ReferenceError: voice is not defined at Object.execute (/workspaces/discord-voice/commands/music/join.js:10:24) at Client.<anonymous> (/workspaces/discord-voice/index.js:40:17) at Client.emit (node:events:518:28) at InteractionCreateAction.handle (/workspaces/discord-voice/node_modules/discord.js/src/client/actions/InteractionCreate.js:97:12) at module.exports [as INTERACTION_CREATE] (/workspaces/discord-voice/node_modules/discord.js/src/client/websocket/handlers/INTERACTION_CREATE.js:4:36) at WebSocketManager.handlePacket (/workspaces/discord-voice/node_modules/discord.js/src/client/websocket/WebSocketManager.js:355:31) at WebSocketManager.<anonymous> (/workspaces/discord-voice/node_modules/discord.js/src/client/websocket/WebSocketManager.js:239:12) at WebSocketManager.emit (/workspaces/discord-voice/node_modules/@vladfrangu/async_event_emitter/dist/index.cjs:282:31) at WebSocketShard.<anonymous> (/workspaces/discord-voice/node_modules/@discordjs/ws/dist/index.js:1173:51) at WebSocketShard.emit (/workspaces/discord-voice/node_modules/@vladfrangu/async_event_emitter/dist/index.cjs:282:31) can i ping you @ShompiFlen
ShompiFlen
ShompiFlenā€¢9mo ago
yea well you have to access it from the interaction i can't really just give you the code, thats not the idea here but i basically gave you the answer
Toothless
ToothlessOPā€¢9mo ago
this is not possible see if i put the real channel id something like this "1086434561640108086" then it works but if i try the solution which you game then it wont all right atleast try one more time
ShompiFlen
ShompiFlenā€¢9mo ago
it doesn't because you did it wrong, if you get an error again show me the code also make sure you have the GuildVoiceStates intent in your client
Toothless
ToothlessOPā€¢9mo ago
ohh shit man i forgot the voicestate intnet let me do this again still the same error ReferenceError: voice is not defined
wen
wenā€¢9mo ago
show your event code
Toothless
ToothlessOPā€¢9mo ago
module.exports = { data: new SlashCommandBuilder() .setName('join') .setDescription('join voice channel in which you are'), async execute(interaction) { const connection = joinVoiceChannel({ channelId: voice.channel.id, guildId: interaction.guild.id, adapterCreator: interaction.guild.voiceAdapterCreator, });
wen
wenā€¢9mo ago
yeah uh u dont have voice defined
Toothless
ToothlessOPā€¢9mo ago
and how to do that
wen
wenā€¢9mo ago
try something like channelId: interaction.member.voice.channel.id
ShompiFlen
ShompiFlenā€¢9mo ago
access it through the interaction thats exactly what i was trying to avoid spoonfeeding
wen
wenā€¢9mo ago
shit sorry šŸ˜”
Toothless
ToothlessOPā€¢9mo ago
i already did that wait
wen
wenā€¢9mo ago
so basically u can access the voice object for that member directly from the member object
Toothless
ToothlessOPā€¢9mo ago
congo guys new error in same line TypeError: Cannot read properties of null (reading 'id')
wen
wenā€¢9mo ago
that means the member is not in a voice channel
Toothless
ToothlessOPā€¢9mo ago
@U j a a n bro if i give it chnnelid like this "1086434561640108086" then it works and other than any thing it wont work the solution you game me i tried but this still error is same
wen
wenā€¢9mo ago
join a vc then try it while u are in the vc
Toothless
ToothlessOPā€¢9mo ago
not working hey big bro help @ShompiFlen
wen
wenā€¢9mo ago
are u sure you're in a vc while using the cmd ??? show me your code again + the error
Toothless
ToothlessOPā€¢9mo ago
okey here we go the code
const { SlashCommandBuilder } = require('discord.js');
const { joinVoiceChannel } = require('@discordjs/voice');

module.exports = {
data: new SlashCommandBuilder()
.setName('join')
.setDescription('join voice channel in which you are'),
async execute(interaction) {
const connection = joinVoiceChannel({
channelId: interaction.member.voice.channel.id,
guildId: interaction.guild.id,
adapterCreator: interaction.guild.voiceAdapterCreator,
});


}
}
const { SlashCommandBuilder } = require('discord.js');
const { joinVoiceChannel } = require('@discordjs/voice');

module.exports = {
data: new SlashCommandBuilder()
.setName('join')
.setDescription('join voice channel in which you are'),
async execute(interaction) {
const connection = joinVoiceChannel({
channelId: interaction.member.voice.channel.id,
guildId: interaction.guild.id,
adapterCreator: interaction.guild.voiceAdapterCreator,
});


}
}
and the error
Ready! Logged in as Craber#3061
TypeError: Cannot read properties of null (reading 'id')
at Object.execute (/workspaces/discord-voice/commands/music/join.js:10:57)
at Client.<anonymous> (/workspaces/discord-voice/index.js:40:17)
at Client.emit (node:events:518:28)
at InteractionCreateAction.handle (/workspaces/discord-voice/node_modules/discord.js/src/client/actions/InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (/workspaces/discord-voice/node_modules/discord.js/src/client/websocket/handlers/INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (/workspaces/discord-voice/node_modules/discord.js/src/client/websocket/WebSocketManager.js:355:31)
at WebSocketManager.<anonymous> (/workspaces/discord-voice/node_modules/discord.js/src/client/websocket/WebSocketManager.js:239:12)
at WebSocketManager.emit (/workspaces/discord-voice/node_modules/@vladfrangu/async_event_emitter/dist/index.cjs:282:31)
at WebSocketShard.<anonymous> (/workspaces/discord-voice/node_modules/@discordjs/ws/dist/index.js:1173:51)
at WebSocketShard.emit (/workspaces/discord-voice/node_modules/@vladfrangu/async_event_emitter/dist/index.cjs:282:31)
^C
Ready! Logged in as Craber#3061
TypeError: Cannot read properties of null (reading 'id')
at Object.execute (/workspaces/discord-voice/commands/music/join.js:10:57)
at Client.<anonymous> (/workspaces/discord-voice/index.js:40:17)
at Client.emit (node:events:518:28)
at InteractionCreateAction.handle (/workspaces/discord-voice/node_modules/discord.js/src/client/actions/InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (/workspaces/discord-voice/node_modules/discord.js/src/client/websocket/handlers/INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (/workspaces/discord-voice/node_modules/discord.js/src/client/websocket/WebSocketManager.js:355:31)
at WebSocketManager.<anonymous> (/workspaces/discord-voice/node_modules/discord.js/src/client/websocket/WebSocketManager.js:239:12)
at WebSocketManager.emit (/workspaces/discord-voice/node_modules/@vladfrangu/async_event_emitter/dist/index.cjs:282:31)
at WebSocketShard.<anonymous> (/workspaces/discord-voice/node_modules/@discordjs/ws/dist/index.js:1173:51)
at WebSocketShard.emit (/workspaces/discord-voice/node_modules/@vladfrangu/async_event_emitter/dist/index.cjs:282:31)
^C
wen
wenā€¢9mo ago
try logging interaction.member.voice
Toothless
ToothlessOPā€¢9mo ago
holly molly when i run this command /join my bot goes temprary offline oh my god this thing is something wth
ShompiFlen
ShompiFlenā€¢9mo ago
run your bot, disconnect and connect to the voice channel again and run the command if its still null then there is probably another issue here
wen
wenā€¢9mo ago
show
Toothless
ToothlessOPā€¢9mo ago
now even ping command is not working let me take step back and put directly the channel id and try again
wen
wenā€¢9mo ago
dude show me what it logs
Toothless
ToothlessOPā€¢9mo ago
sorry bro but now there is nothing in log but my bot goes temprary offline and come back after 3 sec i tried putting my channel id direct now it working wth
wen
wenā€¢9mo ago
well then log it again tf
Toothless
ToothlessOPā€¢9mo ago
ok let me do this again Ready! Logged in as Craber#3061 thats it man
wen
wenā€¢9mo ago
... run the cmd
Toothless
ToothlessOPā€¢9mo ago
ok no logs no error but it says application did not respond in my chat
wen
wenā€¢9mo ago
did u not log it ???? .....
Toothless
ToothlessOPā€¢9mo ago
i did but there is no error man nothing thanks for your pations i will send you dm if it fixes man @U j a a n it worked you missed one key substanse it should be interaction.member.voice.channelId you stoped at voice there should be channelid also congo guys
wen
wenā€¢9mo ago
great šŸ‘
Toothless
ToothlessOPā€¢9mo ago
thanks man today i can say like elon i never give up šŸ”„ thanks @ShompiFlen also good guy see ya
Want results from more Discord servers?
Add your server