Skip to content

Instantly share code, notes, and snippets.

@Grayest
Forked from meldsza/index.js
Created August 16, 2019 04:17
Show Gist options
  • Select an option

  • Save Grayest/5304abb39c5ee5050790bb29c976cf5d to your computer and use it in GitHub Desktop.

Select an option

Save Grayest/5304abb39c5ee5050790bb29c976cf5d to your computer and use it in GitHub Desktop.
Pass pm2 logs to discord via webhook
const webhook_uri = "WEBHOOK URL HERE";
const spawn = require('child_process').spawn;
const request = require('request');
var pm2 = false;
let queue = [];
function createChunks(str) {
return str.match(new RegExp('.{1,' + 2000 + '}', 'g'));
}
function send(data) {
if(!data) return;
request.post({
url: webhook_uri,
form: {
content: "```xl\n" + data + "```"
}
}, function (error, response, body) {
//console.log(body); inifite loop
});
}
function sendData(data) {
data = escapeMarkdown(data.toString());
createChunks(data).filter(a => !!a && a != null).map(a => queue.push);
}
function startlog() {
if (pm2 !== false) {
console.log('pm2 logs process already started...');
return;
}
start = false;
pm2 = spawn('pm2', ['logs']);
pm2.on('exit', (code, signal) => {
console.log('PM2 EXIT');
})
pm2.stderr.on('data', (data) => sendData(data));
pm2.stdout.on('data', (data) => sendData(data));
return pm2;
};
function escapeMarkdown(text) {
return text.replace(/```/g, '`\u200b``');
}
startlog();
setInterval(()=>send(queue.shift()),500);
@Grayest
Copy link
Copy Markdown
Author

Grayest commented Aug 16, 2019

Awsome.. thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment