【问题标题】:discord.js bot working locally but not on Herokudiscord.js 机器人在本地工作,但不在 Heroku 上
【发布时间】:2020-02-02 05:54:58
【问题描述】:

我一直在尝试在 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' 但机器人从不加入语音聊天。

【问题讨论】:

  • 因为您说代码正在运行并且您在控制台中收到消息。那么它没有达到连接功能的唯一方法是不输入条件或它的其他部分。尝试记录条件并检查它。

标签: javascript node.js heroku discord.js


【解决方案1】:

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

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

这也出现了,因为您可能没有正确设置 .env。

在heroku中,ENV位于

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

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

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    将 ffmpeg 构建添加到 Heroku

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

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

    有人可以作为指导: https://www.youtube.com/watch?v=f3wsxbMbi5M

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-05
      • 2019-12-27
      • 2019-10-12
      • 2019-07-10
      • 2021-07-15
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多