简体   繁体   中英

Joining a voice channel on ready (discord.js)

I tried this:

client.on('ready', () => {
  let channel = client.channels.get('432462518380789771');
  channel.join()
});

It doesnt work. I made sure that the ID is right and everything and its still not working.

Considering we have no context on the error you're receiving, I'll provide a code example to see if this fixes your issue.

client.on("ready", () => {
  const channel = client.channels.get("mychannelid");
  if (!channel) return console.error("The channel does not exist!");
  channel.join().then(connection => {
    // Yay, it worked!
    console.log("Successfully connected.");
  }).catch(e => {
    // Oh no, it errored! Let's log it to console :)
    console.error(e);
  });
});

In this code, we use the ready event and then get the channel, like you do. In addition, we also check if the channel is undefined or null, meaning the bot was unable to find the channel or did not have it cached. Then, we join and see if we get a returning connection. If we do, log to the console the fact we successfully connected. If it didn't successfully connect, we'll catch it and error it to console.

It's always a good idea when debugging to include logging to see how far your code runs, and to see where issues may occur. In Node.js, it's also a good idea to catch for unhandledRejections. Otherwise, they will crash your process. You can do that via the code example below.

process.on("unhandledRejection", console.error);

Good luck, and happy coding!

EDIT: With the new information, I now very easily see the issue. Notice how in the error it says:

Error: FFMPEG not found

You can see that you do not currently have FFMPEG installed. To install FFMPEG, go to this url to download the sources for your platform. Check out this answer to see how to install it on Windows.

This would be an updated version for the working code for update v12. As of 02/05/2020.

 client.on("ready", () => { const channel = client.channels.cache.get("ChannelIDhere"); if (!channel) return console.error("The channel does not exist!"); channel.join().then(connection => { // Yay, it worked! console.log("Successfully connected."); }).catch(e => { // Oh no, it errored! Let's log it to console :) console.error(e); }); });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM