ARulli
ARulli

Reputation: 1

discord.js bot working locally but not on Heroku

I have been attempting to run my discord.js bot on Heroku but I am having trouble getting my bot to join voice channels. Whenever I run my bot locally, everything works well, but when I host it on Heroku, some things don't work.

My bot.js looks like this:

const Discord = require('discord.js');
const client = new Discord.Client();
const ffmpeg = require('ffmpeg');
const opus = require('opusscript');
const token = 'Hidden for obious reasons'
var isReady = true;

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', message => {
     if (message.content === 'ping') {
       message.reply('Test message');
       client.channels.get('Our general chat').send('Test message 2')
      }
});

client.on('message', message => {
    if (message.content === 'join') {
      isReady = false;
      const voiceChannel = client.channels.get('ID of our voiceChannel');
      if (!voiceChannel) {
        client.channels.get('ID of our general chat').send('Can\'t get vc');
      }
      else {
        client.channels.get('ID of our general chat').send('Got here 1');
        voiceChannel.join();
        client.channels.get('ID of our general chat').send('Got here 2');
        isReady = true;
      }
    }
});

client.on('message', message => {
  if (message.content === 'leave') {
    isReady = false;
    const voiceChannel = client.channels.get('ID of our voiceChannel');
    voiceChannel.leave();
    isReady = true;
  }
});

client.on('voiceStateUpdate', (oldMember, newMember) => {
  if (isReady && newMember.id === 'My friends ID' && oldMember.voiceChannel === undefined && newMember.voiceChannel !== undefined)
  {
  isReady = false;
  var voiceChannel = client.channels.get('ID of our voiceChannel');
  voiceChannel.join().then(connection =>
  {
     // Play the file
     const dispatcher = connection.playFile('./clip.mp3');
     dispatcher.on("end", end => {
       voiceChannel.leave();
       });
   }).catch(err => console.log(err));
   isReady = true;
  }
});

client.login(token);

While my package.json looks like:

{
  "name": "mybot",
  "version": "1.0.0",
  "description": "Make It Say Dumb Thing",
  "main": "bot.js",
  "scripts": {
    "start": "node bot.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "discord.js": "^11.5.1",
    "ffmpeg": "0.0.4",
    "opusscript": "0.0.7"
  }
}

With my Procfile simply being:

worker: node bot.js

When I run this locally on my machine, everything works perfectly. However, when I host this on Heroku, the .join() function is not working. It prints out 'Got here 1' and 'Got here 2' but the bot never joins the voice chat.

Upvotes: 0

Views: 1813

Answers (2)

Red_Dye_Number_9
Red_Dye_Number_9

Reputation: 1

Add the ffmpeg build to Heroku

  1. Heroku → [Settings] → [Add buildpack]
  2. paste the link

    https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest.git
    
  3. [Save changes]

Someone to follow as guidance: https://www.youtube.com/watch?v=f3wsxbMbi5M

Upvotes: 0

Tails Era
Tails Era

Reputation: 11

you are using client.login(token);, Not client.login(process.env.token);.

The problem is you are not asking the code to look into the Vars.

this is also showing up because you may not be setting the .env right.

In heroku the ENV is under

(Your App) > Settings > Config Vars .

if those are not set up, this is also the problem.

I hope this helps.

Upvotes: 1

Related Questions