Simp-O-Matic

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

commit eb1ebcda23cf9bc3a7286a33dd1532af75fa21ac
parent 8dbe7930f11ee8da753f8df90679b08732a09bdd
Author: Demonstrandum <moi@knutsen.co>
Date:   Tue, 28 Jul 2020 22:27:06 +0100

Make ready for new server.

Diffstat:
A.pid | 1+
Mlib/commands/youtube.ts | 2+-
Alib/markov.ts | 33+++++++++++++++++++++++++++++++++
Aset_push_url.sh | 8++++++++
Asimpomatic.service | 15+++++++++++++++
Astart.sh | 10++++++++++
Astop.sh | 4++++
7 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/.pid b/.pid @@ -0,0 +1 @@ +19829 diff --git a/lib/commands/youtube.ts b/lib/commands/youtube.ts @@ -21,7 +21,7 @@ export default async (home_scope: HomeScope) => { } } - const result = await fetch(encodeURI(`https://invidio.us/api/v1/search?q=${query}&sort_by=${sort_by}&type=${type}`)); + const result = await fetch(`https://invidio.us/api/v1/search?q=${query}&sort_by=${sort_by}&type=${type}`); const res_json = await result.json(); const res = res_json[Math.abs(num - 1)]; let duration = new Date(res.lengthSeconds * 1000) diff --git a/lib/markov.ts b/lib/markov.ts @@ -0,0 +1,33 @@ +//! Extending the corpus is done simply by: +//! ```ts +//! const markov = new Markov("Hello,"); +//! markov.corpus += " World!"; +//! ``` + + +class Markov { + corpus: string; + constructor(corpus: string) { + this.corpus = corpus; + } + + get words() { + return this.corpus.split(/\b/).filter(word => word != ''); + } + + get unique() { + return new Set(this.words); + } + + get dictionary_index() { + const dict: { [key: string]: number } = {}; + let i: number = 0; + for (const word of this.unique) { + dict[word] = i; + i += 1; + } + + return dict; + } +} + diff --git a/set_push_url.sh b/set_push_url.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +GIT="$(command -v git)" +[ -z "$GIT" ] && { echo "Please install \`git\` to your PATH."; exit 1; } + +URL="git@git.knutsen.co:/srv/git/Simp-O-Matic.git" +"$GIT" remote set-url --push --add origin "$URL" + diff --git a/simpomatic.service b/simpomatic.service @@ -0,0 +1,15 @@ +[Unit] +Description=Discord Bot +After=network-online.target + +[Service] +Type=forking +User=git +WorkingDirectory=/srv/simpomatic +ExecStart=/bin/sh /srv/simpomatic/start.sh +ExecStop=/bin/sh /srv/simpomatic/stop.sh +GuessMainPID=no +TimeoutStartSec=600 + +[Install] +WantedBy=multi-user.target diff --git a/start.sh b/start.sh @@ -0,0 +1,10 @@ +#!/bin/sh +yarn install +yarn build + +source ./.env + +# Starting the bot. +PID="$(sh -c 'echo $$; exec yarn start >./.log 2>&1' &)" +echo "$PID" > ./.pid + diff --git a/stop.sh b/stop.sh @@ -0,0 +1,4 @@ +[ ! -f ./.pid ] && { echo "No PID file found."; exit 1; } + +PID="$(cat ./.pid)" +kill "$PID"