Simp-O-Matic

Dumb Discord bot in TS.
git clone git://git.knutsen.co/Simp-O-Matic
Log | Files | Refs | README | LICENSE

commit cc9c54a3df28741b4593e10f4d5016a3cb166dd7
parent 768a72aec9fc5677b926dee4944e20ce6c4ba337
Author: Demonstrandum <moi@knutsen.co>
Date:   Sat, 16 May 2020 22:30:32 +0100

Basic webhooks setup.

Diffstat:
MHELP.md | 7+++++++
Mlib/main.ts | 9+++++++++
Alib/web.ts | 45+++++++++++++++++++++++++++++++++++++++++++++
Aweb/index.html | 143+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 204 insertions(+), 0 deletions(-)

diff --git a/HELP.md b/HELP.md @@ -69,6 +69,13 @@ - `!cron <ls>` — lists all active cron-jobs numerically. - `!cron <clear>` — clears all executed cron-jobs. - `!cron rm #[job-index]` — removes a cron-job by index. +- `!git` — Information on commits and more of the bot's Git repository: + - `!git history <#number>` — Gives latest commits to the repo, number defaults to 5. + - `!git latest` — Gets latest commit. + - `!git contributors` — List of contributors. + - `!git breakdown` — Breakdown of additions and deletions by contributors. + - `!git stars` — Get star count. + - `!git forks` — Get fork count. - `!choose [comma-separated-values]` — Choose randomly from a list of items, separated by commas. - `!define [word]` — Looks a word up in the Oxford English Dictionary. - `!urban [slang]` — Looks up a piece of slang in the _Urban Dictionary_. diff --git a/lib/main.ts b/lib/main.ts @@ -98,6 +98,8 @@ const system_message = (client: Client, msg: string) => { c.send(msg)); }; +import server from './web'; + @Discord export class SimpOMatic { private static _CLIENT : Client; @@ -171,6 +173,13 @@ export class SimpOMatic { || member.user.tag.split('#').first())); } }); + + // Send messages on web-hooks. + server(body => { + system_message(client, "Received data:\n```" + + JSON.stringify(body, null, 4) + + "```"); + }); } expand_alias(operator: string, args: string[], message: Message) { diff --git a/lib/web.ts b/lib/web.ts @@ -0,0 +1,45 @@ +import http from 'http'; +import { readFileSync as read_file } from 'fs'; + + +export default handle_post => { + const request_listener: http.RequestListener = (req, res) => { + let got_post = false; + req.on('data', chunk => { + console.log('Web-hook:'); + const body = JSON.parse(chunk); + console.log(body); + handle_post(body); + got_post = true; + }); + + if (got_post) return; + + switch (req.url) { + case '/': + res.writeHead(200, { + 'Content-Type': 'text/html' + }); + + res.write(read_file('./web/index.html').toString()); + res.end(); + break; + default: + res.writeHead(404, { + 'Content-Type': 'text/html' + }); + res.write(`<!DOCTYPE html> + <html> + <body> + <h1>404 - Not found.</h1> + </body> + </html> + `); + res.end(); + } + }; + const server = http.createServer(request_listener); + server.listen(8080, '0.0.0.0', () => { + console.log(`Web server started on http://0.0.0.0:8080/`) + }); +}; diff --git a/web/index.html b/web/index.html @@ -0,0 +1,143 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Simp'O'Matic</title> + <link href="https://fonts.googleapis.com/css2?family=Balsamiq+Sans:wght@400;700&display=swap" rel="stylesheet"> + <style> + :root { + --grey: #666472; + --pink: #ef88c5; + } + * { + vertical-align: middle; + box-sizing: border-box; + } + body, html { + font-size: 16px; + font-family: 'Balsamiq Sans', 'cursive'; + padding: 0; + margin: 0; + height: 100%; + } + body { + max-width: 100vw; + min-height: calc(100vh - 3px); + overflow-y: visible; + overflow-x: hidden; + } + #content { + height: 100vh; + display: flex; + align-items: center; + justify-content: center; + } + #content::after { + content: ""; + background: url("https://github.com/Demonstrandum/Simp-O-Matic/blob/master/lib/resources/banners/banner.png?raw=true"); + background-position: center; + background-size: cover; + opacity: 0.1; + top: 0; + left: 0; + bottom: 0; + right: 0; + position: absolute; + z-index: -1; + } + .center { + color: var(--grey); + --font-height: 3em; + text-align: center; + } + .center p { + letter-spacing: -1px; + font-weight: bold; + width: 16em; + display: inline-block; + margin: 0; + font-size: 1.2em; + opacity: 0.7; + } + .center h1 { + font-size: var(--font-height); + filter: drop-shadow(0px 2px 4px rgba(0,0,0,0.13)); + } + .center h1 img { + height: calc(var(--font-height) / 2); + margin: 0 0.2em; + } + .center h1 .pink { + /* font-size: calc(var(--font-height) * 0.2); */ + } + .bar { + display: inline-block; + vertical-align: text-top; + height: 1.1em; + width: 0.3em; + border-radius: 3px; + background: var(--pink); + } + .grey { + color: var(--grey); + } + .pink { + color: var(--pink); + } + </style> +</head> +<body> + <div id="content"> + <div class="center"> + <h1> + <div class="bar"></div> + <span class="grey">Simp'O'Matic</span> + <span class="pink">is Online</span> + <img + class="emoji" + alt="uwu" + src="https://discordemoji.com/assets/emoji/BlobUWU.png" /> + </h1> + <p> + Maybe <span class="pink">Danny</span> will put + cool usage graphs here one day... + </p> + </div> + </div> + <!-- GitLink --> + <style> + .gitlink { + position: fixed; + bottom: 1.8em; + right: 1.9em; + padding: 1em 1em 0.9em 1em; + background: #fff; + border: 1px solid #0001; + border-radius: 6px; + } + + .gitlink img { + transition: all .1s ease-in-out; + opacity: 0.3; + } + + .gitlink img:hover { + opacity: 1; + cursor: pointer; + } + + .gitlink, + .gitlink img { + margin: 0; + } + + .gitlink img { + padding: 0; + } + </style> + <a class="gitlink" target="_blank" href="https://github.com/Demonstrandum/Simp-O-Matic"> + <img src="https://cdn.jsdelivr.net/npm/simple-icons@latest/icons/github.svg" height=22 alt="GitHub" /> + </a> +</body> +</html>