Discord Bot版本在Heroku上不断崩溃

时间:2020-04-29 09:31:21

标签: node.js heroku bots discord discord.js

我制作了一个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);

                }else{dm.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);

                }else{dm.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);

1 个答案:

答案 0 :(得分:1)

之所以发生这种情况,是因为您使用的是web dyno:如果该应用程序为网站提供的服务时间超过1小时,则该dyno会处于睡眠状态。 解决方案是从web dyno切换到worker dyno:此类型不提供网站服务(如果您运行Discord bot,则不需要它)并且永远不会进入睡眠状态

进入您的Procfile文件并用worker替换web,它应该像这样:

worker: npm start //this is the command you use to start your app.

来源: Here