commit 083950028744716680a4b966fe0664ef373e4fa5
parent 96dd9dd208b6d62a442421e22c47441028620cba
Author: Demonstrandum <moi@knutsen.co>
Date: Sat, 16 May 2020 23:54:15 +0100
Basic push and star webhook for github.
Diffstat:
2 files changed, 46 insertions(+), 14 deletions(-)
diff --git a/lib/main.ts b/lib/main.ts
@@ -3,7 +3,10 @@ process.stdin.resume();
// Discord Bot API.
import { Discord, On, Client } from '@typeit/discord';
-import { Message, MessageAttachment, TextChannel } from 'discord.js';
+import { Message,
+ MessageAttachment,
+ MessageEmbed,
+ TextChannel } from 'discord.js';
// System interaction modules.
import {
@@ -87,7 +90,7 @@ console.log('File/Execution locations:', {
'process.cwd()': process.cwd()
});
-const system_message = (client: Client, msg: string) => {
+const system_message = async (client: Client, msg: any) => {
for (const guild in GLOBAL_CONFIG.guilds)
if (GLOBAL_CONFIG.guilds.hasOwnProperty(guild)
&& GLOBAL_CONFIG.guilds[guild].system_channel
@@ -176,9 +179,31 @@ export class SimpOMatic {
// Send messages on web-hooks.
server(body => {
- system_message(client, "Received data:\n```"
- + JSON.stringify(body, null, 4)
- + "```");
+ if (body.ref === "refs/heads/master" || body.action) {
+ const embed = new MessageEmbed()
+ .setColor("#ef88c5")
+ .setURL(body.repository.html_url)
+ .setThumbnail("https://github.com/Demonstrandum/Simp-O-Matic/raw/master/lib/resources/banners/banner.png")
+ .setAuthor(body.sender.login, body.sender.avatar_url);
+
+ if (body.head_commit) {
+ const push_embed = embed
+ .setTitle("Latest Commit")
+ .setDescription(body.head_commit.message)
+ .setURL(body.head_commit.url);
+
+ system_message(client, push_embed);
+
+ } else if (body.starred_at) {
+ const star_embed = embed
+ .setTitle("Star Added!");
+ system_message(client, star_embed);
+ }
+ } else {
+ system_message(client, "Received unknown data:\n```"
+ + JSON.stringify(body, null, 4).slice(0, 1930)
+ + "```");
+ }
});
}
diff --git a/lib/web.ts b/lib/web.ts
@@ -6,16 +6,23 @@ const PORT = Number(process.env.PORT) || 8080;
export default handle_post => {
const request_listener: http.RequestListener = (req, res) => {
- let got_post = false;
- req.on('data', chunk => {
+ if (req.method === 'POST') {
console.log('Web-hook:');
- const body = JSON.parse(chunk);
- console.log(body);
- handle_post(body);
- got_post = true;
- });
- if (got_post) return;
+ let chunks = '';
+ req.on('data', chunk => {
+ chunks += chunk;
+ });
+ req.on('end', () => {
+ console.log('Got whole body.')
+ const body = JSON.parse(chunks);
+ console.log(body);
+ handle_post(body);
+ res.writeHead(200);
+ res.end();
+ });
+ return;
+ }
switch (req.url) {
case '/':
@@ -42,6 +49,6 @@ export default handle_post => {
};
const server = http.createServer(request_listener);
server.listen(PORT, '0.0.0.0', () => {
- console.log(`Web server started on http://0.0.0.0:8080/`)
+ console.log(`Web server started on http://0.0.0.0:8080/`);
});
};