如何使项目自动重新启动



我让机器人不和播放电台24/7链接电台直播

这里的问题是机器人每隔一段时间就会停止有没有解决方案可以每小时重新启动一次机器人程序?当我手动重新启动机器人时,它工作得很好。

const Discord = require("discord.js");
const client = new Discord.Client();
const request = require('request');
const GUILDID = ''; 
const CHANNELID = '';
const url = 'https://sc.creacast.com/topmusic_strasbourg'; 
client.on('ready', async () => {
console.log('done');
voiceStay(GUILDID, CHANNELID);
function voiceStay(guildid, channelid) {
if (!guildid) throw new Error('id sv error');
if (!channelid) throw new Error('id room error');
let guild = client.guilds.cache.get(guildid);
const voiceChannel = guild.channels.cache.get(channelid);;
if (!voiceChannel) {
return
}
voiceChannel.join()
.then(connection => {
const dispatcher = connection.play(url);
});
}
});
client.login("");

注意:我的托管提供商只允许我使用javascript文件。

这里有一些你可以尝试的东西:

  • 使用一种流程管理器:

    const { spawn } = require("child_process");
    function loop() {
    const bot = spawn("node", [ "file_of_bot.js" ], {
    stdio: "inherit",
    });
    bot.once("exit", (code) => {
    // prevents restarting the bot if the code had a fatal error
    if (code === 0) {
    loop();
    }
    });
    }
    loop();
    

    然后你可以在一小时后退出你的机器人:

    setTimeout(process.exit, 1000 * 60 * 60)
    
  • 使用pm2:

    npx pm2 start file_of_bot.js
    
    setTimeout(process.exit, 1000 * 60 * 60)
    
  • 销毁您的客户端并重新登录

    • 您可能需要包装bot的创建来重新创建它,因为Client.destroy()似乎可以防止将来调用Client.login()
  • 退出并重新加入语音通道

相关内容

最新更新