Skip to content

Instantly share code, notes, and snippets.

@believer-ufa
Last active March 2, 2016 10:15
Show Gist options
  • Save believer-ufa/5d3239bba40c6f21e52f to your computer and use it in GitHub Desktop.
Save believer-ufa/5d3239bba40c6f21e52f to your computer and use it in GitHub Desktop.
Discord github and php bot on Node.js
var express = require('express');
var Discord = require("discord.js");
var HttpApp = express();
var GithubBotClient = new Discord.Client();
require('sugar')
var HQAGithubChannel = 'github channel id in discord'
var HQAPHPErrorsChannel = 'php channel id in discord'
HttpApp.use(require('body-parser').json());
GithubBotClient.login("{discord-user-login}", "{discord-user-email}", function(error_msg) {
// когда залогинились..
HttpApp.post('/github-event', function (request, response) {
//console.log(request.body, request.headers)
if (request.headers['x-github-event'] === 'push')
{
console.log('Получили сообщение о новом коммите')
var commits = request.body.commits
var repo = request.body.repository
var message = 'Новые коммиты в проект {project}:\n'.assign({
project: repo.name
});
commits.forEach(function(commit, index) {
if (index > 0) {
message += '\n\n';
}
message += '--------------------------------------------------\n'
message += '{commitMessage}\n{url}\n\nот {commiter}.'.assign({
commitMessage : commit.message,
url : commit.url,
commiter : commit.author.name
})
if (commit.added.length) {
message += '\n\nДобавлены файлы:\n'
commit.added.forEach(function(fileName) {
message += ' - ' + fileName + '\n'
})
}
if (commit.removed.length) {
message += '\n\nУдалены файлы:\n'
commit.removed.forEach(function(fileName) {
message += ' - ' + fileName + '\n'
})
}
if (commit.modified.length) {
message += '\n\nИзменены файлы:\n'
commit.modified.forEach(function(fileName) {
message += ' - ' + fileName + '\n'
})
}
})
GithubBotClient.sendMessage(HQAGithubChannel,message)
}
response.send('thanks');
});
HttpApp.post('/php-error', function (request, response) {
console.log('Получили сообщение об ошибке на PHP сервере')
var file = request.body.file
var line = request.body.line
var message = request.body.message
var url = request.body.url
var client = request.body.client
var user = request.body.user ? (request.body.user.id + '. ' + request.body.user.name + ' <' + request.body.user.email + '>') : 'неавторизован'
var messageToDiscordChanngel = '----------------------------------'
+('\nФайл: `{file}`'
+'\nНомер строки: `{line}`'
+'\nСообщение: {message}'
+'\nURL: `{url}`'
+'\nКлиент: `{client}`'
+'\nПользователь: {user}').assign({
file : file,
line : line,
message : message,
url : url,
client : client,
user : user,
});
GithubBotClient.sendMessage(HQAPHPErrorsChannel,messageToDiscordChanngel)
response.send('thanks');
});
HttpApp.listen(3000, function() {
console.log('Listening on port 3000.');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment