{"size": 1228, "ext": "js", "max_stars_repo_path": "website/routes/api/admin.js", "max_stars_repo_name": "2JJ1/RoundBot", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "website/routes/api/admin.js", "max_issues_repo_name": "2JJ1/RoundBot", "max_issues_repo_issues_event_min_datetime": "2020-06-29T18:40:09.000Z", "max_issues_repo_issues_event_max_datetime": "2020-06-29T18:40:09.000Z", "max_forks_repo_path": "website/routes/api/admin.js", "max_forks_repo_name": "2JJ1/RoundBot", "max_forks_repo_forks_event_min_datetime": "2020-08-09T05:29:09.000Z", "max_forks_repo_forks_event_max_datetime": "2020-08-09T05:29:09.000Z"}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 26.6956521739, "max_line_length": 107, "alphanum_fraction": 0.5342019544}
const router = require("express").Router()
const pm2 = require('pm2')
const DiscordOAuth2 = require('../../my_modules/discordapi')

// /api/admin

router.post("/restartbot", async (req, res) => {
    var response = {success: false}

    try{
        var user = await DiscordOAuth2.GetUser(req.session.access_token)

        //Only FF#0255 and Funtimesgetfunner can request to this page
        if(user.id !== "423644128681656320" && user.id !== "526806117993545748") throw "Invalid permission"

        //Restart PM2 bot
        await new Promise((resolve, reject) => {
            pm2.connect(function(err) {
                if (err) reject(err)
                pm2.restart("bot", err => {
                    if(err) reject(err)
                    resolve()
                })
            })
        })
        .catch((err)=>{ 
            console.error(new Error(err))
            throw "The server failed to restart the bot..." 
        })

        response.success = true
    }
    catch(e){
        if(typeof e === "string") response.reason = e
        else {
            console.log(e)
            response.reason = "Server error"
        }
    }

    pm2.disconnect()

    res.json(response)
})

module.exports = router