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

问题描述 投票:0回答:1

我一直试图在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个回答
0
投票

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

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

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

在heroku中,ENV在下面

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

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

我希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.