discord.js机器人在本地运行,但无法在Heroku上运行

时间:2019-10-03 21:31:48

标签: javascript node.js heroku discord.js

我一直试图在Heroku上运行discord.js机器人,但是我很难让我的机器人加入语音通道。每当我在本地运行我的机器人时,一切都运行良好,但是当我将其托管在Heroku上时,某些功能将无法正常工作。

我的bot.js看起来像这样:

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

我的package.json看起来像:

{
  "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"
  }
}

我的Procfile只是:

worker: node bot.js

当我在计算机上本地运行此程序时,一切运行正常。但是,当我在Heroku上托管它时,.join()函数不起作用。它会打印出“ Got here 1”和“ Got here 2”,但该漫游器从不加入语音聊天。

2 个答案:

答案 0 :(得分:0)

您正在使用 client.login(token);,而不是client.login(process.env.token);

问题是您没有要求代码查看Vars。

这也会显示出来,因为您可能没有正确设置.env。

在heroku中,ENV在

(您的应用)>设置>配置变量。

如果没有设置,这也是问题所在。

我希望这会有所帮助。

答案 1 :(得分:0)

将ffmpeg版本添加到Heroku

  1. Heroku→[设置]→[添加构建包]
  2. 粘贴链接

    https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest.git
    
  3. [保存更改]

以下人员将作为指导: https://www.youtube.com/watch?v=f3wsxbMbi5M