【发布时间】: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