From ade3be9447ebd665aea499c1974f6074dca228f0 Mon Sep 17 00:00:00 2001 From: Tyler-A Date: Tue, 31 Dec 2019 15:18:27 -0700 Subject: [PATCH] Add code and config template. --- src/config.template.ts | 11 +++++++++++ src/main.ts | 44 ++++++++++++++++++++++++++++++++++++++++++ src/utils/api.ts | 18 +++++++++++++++++ src/utils/webhook.ts | 25 ++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 src/config.template.ts create mode 100644 src/main.ts create mode 100644 src/utils/api.ts create mode 100644 src/utils/webhook.ts diff --git a/src/config.template.ts b/src/config.template.ts new file mode 100644 index 0000000..93d846d --- /dev/null +++ b/src/config.template.ts @@ -0,0 +1,11 @@ +// The number of seconds between each push, this must be a minimum of 300 +export const DELAY: number = 24 * 60 * 60 + + +// The twitch.center URL for the quotes list, this endpoint must return a +// plaintext response with newline characters separating each quote +export const QUOTE_URL: string = ``; + + +// The Discord webhook URL that we are sending the quotes to. +export const DISCORD_WEBHOOK: string = ``; \ No newline at end of file diff --git a/src/main.ts b/src/main.ts new file mode 100644 index 0000000..53028fd --- /dev/null +++ b/src/main.ts @@ -0,0 +1,44 @@ +// +// main.ts +// +// Written by: Tyler Akins (2019/12/31) +// + + +import { GET_RANDOM_QUOTE } from "./utils/api"; +import { WEBHOOK_PUSH } from "./utils/webhook"; +import { DELAY } from "./config"; + + + +const MAIN = async () => { + const args = process.argv; + + + if (args.includes("--single")) { + console.log(`* Emitting a quote`) + let quote = await GET_RANDOM_QUOTE(); + await WEBHOOK_PUSH(quote); + process.exit(0); + }; + + + if (args.includes("--repeat")) { + + if (DELAY < 30) { + console.log("Can't have a delay < 30 seconds."); + process.exit(1); + }; + + setInterval( + async () => { + console.log(`* Emitting a quote`) + await WEBHOOK_PUSH(await GET_RANDOM_QUOTE()); + }, + DELAY * 1000 + ); + }; +}; + + +MAIN(); \ No newline at end of file diff --git a/src/utils/api.ts b/src/utils/api.ts new file mode 100644 index 0000000..fa89089 --- /dev/null +++ b/src/utils/api.ts @@ -0,0 +1,18 @@ +// +// api.ts +// +// Written by: Tyler Akins (2019/12/31) +// + + +import * as requests from "request-promise-native" +import { QUOTE_URL } from "../config"; + + +export const GET_RANDOM_QUOTE = async (): Promise => { + let response = await requests.get(QUOTE_URL); + + let quotes = response.split("\n") + + return quotes[Math.floor(Math.random() * quotes.length)]; +}; \ No newline at end of file diff --git a/src/utils/webhook.ts b/src/utils/webhook.ts new file mode 100644 index 0000000..31d8ac7 --- /dev/null +++ b/src/utils/webhook.ts @@ -0,0 +1,25 @@ +// +// webhook.ts +// +// Written by: Tyler Akins (2019/12/31) +// + + +import * as requests from "request-promise-native"; +import { DISCORD_WEBHOOK } from "../config"; + + +export const WEBHOOK_PUSH = async (quote: string): Promise => { + return await requests.post({ + uri: DISCORD_WEBHOOK, + body: { + content: quote + }, + qs: { + wait: true + }, + json: true + }) + .then(() => {return true}) + .catch(() => {return false}); +}; \ No newline at end of file