Joining a voice channel on ready (discord.js)

回眸只為那壹抹淺笑 提交于 2020-05-28 05:26:34

问题


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.


回答1:


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.




回答2:


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);
    });
});


来源:https://stackoverflow.com/questions/49844393/joining-a-voice-channel-on-ready-discord-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!