Discord Bot 构建在 Heroku 上不断崩溃
Posted
技术标签:
【中文标题】Discord Bot 构建在 Heroku 上不断崩溃【英文标题】:Discord Bot build constantly crashing on Heroku 【发布时间】:2020-08-13 08:26:06 【问题描述】:我制作了一个 Discord 机器人,当我在本地运行它时它运行良好,但当我将它上传到 Heroku 时却不行。我不知道为什么它总是崩溃。 机器人在本地完美运行。
当我在脚本中添加start
时,还有一件事。它跑了又跑了一分钟,然后又坠毁了。然后自行重启,一分钟后再次崩溃。
2020-04-29T09:24:00.011590+00:00 app[api]: Scaled to web@0:Free worker@0:Free by user owaisshamsi217@gmail.com
2020-04-29T09:24:01.483021+00:00 app[api]: Scaled to web@0:Free worker@1:Free by user owaisshamsi217@gmail.com
2020-04-29T09:24:04.650208+00:00 heroku[worker.1]: State changed from starting to up
2020-04-29T09:24:06.950244+00:00 heroku[worker.1]: State changed from up to crashed
2020-04-29T09:24:06.953653+00:00 heroku[worker.1]: State changed from crashed to starting
2020-04-29T09:24:10.862018+00:00 heroku[worker.1]: State changed from starting to up
2020-04-29T09:24:13.509980+00:00 heroku[worker.1]: State changed from up to crashed
我的代码只是一个 server.js 文件,代码是:
const Discord = require('discord.js');
const settings = require('./config.json');
const fs = require('fs');
const bot = new Discord.Client();
bot.commands = new Discord.Collection();
const prefix = settings.prefix;
//const token = settings.token;
bot.on('message', message =>
if(message.author.bot) return;
let dm = message.author;
if(message.channel.name === 'verify')
if(message.content === '!verify')
message.delete()
let server = message.guild.name;
let servericon = message.guild.iconURL();
var captcha1 = 'https://i.ibb.co/DpjB37s/captcha1.png'
var captcha2 = 'https://i.ibb.co/nsbq041/captcha2.png'
var select = [];
select.push(captcha1);
select.push(captcha2);
var chosen = select[Math.floor(Math.random() * select.length)];
let embed = new Discord.MessageEmbed()
.setTitle(`**Welcome to $server**\n\nCaptcha`)
.setDescription("Please complete the captcha given below to gain access to the server.\n**Note:** This is case sensitive")
.setAuthor('Mr.Verifier', "https://i.ibb.co/nckjDjG/hmm.png")
.setColor('#0929ff')
.setThumbnail(servericon)
.addField('**Why all this?**' , 'This is to protect the servers from\nmalicious raids of automated bots',true)
.setImage(chosen)
.setTimestamp();
dm.send(embed)
let guild = message.guild.id;
module.exports.guild = guild
module.exports.chosen = chosen
module.exports.captcha1 = captcha1
module.exports.captcha2 = captcha2
else
message.delete();
;
if(message.channel.type === 'dm')
const response = message.content;
if(this.chosen === this.captcha1)
if(dm && response === 'L3thq4')
var role = bot.guilds.cache.get(this.guild).roles.cache.find(r => r.name == 'Verified Member');
var user = bot.guilds.cache.get(this.guild).members.cache.find(member => member.id === dm.id)
user.roles.add(role);
let embed = new Discord.MessageEmbed()
.setTitle('**THANK YOU**')
.setDescription('You have been given access to the server.')
.setColor('#0929ff')
dm.send(embed);
elsedm.send('You entered the captcha incorrectly');
else if(this.chosen === this.captcha2)
if (dm && response === 'Fe1t6p')
var role = bot.guilds.cache.get(this.guild).roles.cache.find(r => r.name == 'Verfied Member');
var user = bot.guilds.cache.get(this.guild).members.cache.find(m => m.id === dm.id);
user.roles.add(role);
let embed = new Discord.MessageEmbed()
.setTitle('**THANK YOU**')
.setDescription('You have been given access to the server.')
.setColor('#0929ff')
dm.send(embed);
elsedm.send('You entered the captcha incorrectly');
else
dm.send('No captcha in inventory.');
;
;
)
bot.on('ready', async() =>
console.log("At your service");
)
bot.login(process.env.token);
【问题讨论】:
你也可以分享你的 Procfile 吗? Procfile 只有这一行worker: node server.js
尝试在控制台中输入heroku logs
,也许它会显示更多有助于调试问题的信息。
它根本不起作用
【参考方案1】:
发生这种情况是因为您使用的是 web
dyno:如果应用程序在超过 1 小时内没有为网站提供服务,则该 dyno 会进入休眠状态。
解决方案是从 web
dyno 切换到 worker
dyno:这种类型不服务于网站(如果您正在运行 Discord 机器人,则不需要它)并且永远不会进入睡眠状态。
进入你的 Procfile 文件并用 worker 替换 web,它应该是这样的:
worker: npm start //this is the command you use to start your app.
来源: Here
【讨论】:
以上是关于Discord Bot 构建在 Heroku 上不断崩溃的主要内容,如果未能解决你的问题,请参考以下文章
Discord Py - Heroku 托管 Bot 上的音乐命令
Discord.JS Bot 通过 Heroku 托管抛出 500 错误