最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Joining a voice channel on ready (discord.js) - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Apr 15, 2018 at 16:45 ejerejer 823 gold badges3 silver badges14 bronze badges 7
  • What is the error that you get? – André Commented Apr 15, 2018 at 17:03
  • Im not sure how to see the errors... Im using heroku to host my discord bot. – ejer Commented Apr 15, 2018 at 17:29
  • Well but while in development, why are you hosting the bot on a remote machine? – André Commented Apr 15, 2018 at 17:31
  • umm... i tried downloading the code and running it locally but its all messed up and gives a lot of errors – ejer Commented Apr 15, 2018 at 18:47
  • And those errors are? – André Commented Apr 15, 2018 at 20:46
 |  Show 2 more comments

2 Answers 2

Reset to default 8

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

发布评论

评论列表(0)

  1. 暂无评论