Simp-O-Matic

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

commit a97de65d4022db5a9ff80e909db975e611d7bb21
parent c66009eb49ff922a5fdbc5ec97dc9d4f99412066
Author: Demonstrandum <moi@knutsen.co>
Date:   Thu, 19 Mar 2020 14:50:46 +0000

Fixes...

Diffstat:
Mbuild.sh | 2+-
Mlib/api/youtube.ts | 10++++++----
Mlib/commands/b.ts | 12++++--------
Mlib/commands/cowsay.ts | 13++++++++-----
Alib/commands/cowthink.ts | 9+++++++++
Mlib/commands/figlet.ts | 13++++++++-----
Mlib/commands/youtube.ts | 7++++---
Mlib/main.ts | 6+++---
Mpackage.json | 3+++
Myarn.lock | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
10 files changed, 131 insertions(+), 38 deletions(-)

diff --git a/build.sh b/build.sh @@ -14,6 +14,6 @@ echo "${bold}Compiling TypeScript...${reset}" ./node_modules/.bin/tsc -b ./tsconfig.json -[ -f "./export_secrets.sh" ] && source ./export_secrets.sh +[ -f "./.env" ] && source ./.env echo -e "\n${bold}Build done.${reset}" diff --git a/lib/api/youtube.ts b/lib/api/youtube.ts @@ -27,6 +27,10 @@ const web_search = (param: YTSearch) => new Promise((resolve, reject) => { // scope: SCOPES // }); // console.log('Authorize this app by visiting this url: ', auth_url); + + const general_error = e => + reject(`No results, or API capped...\n\`\`\`\n${e}\n\`\`\``); + auth.getToken(process.env['GOOGLE_PERSONAL_CODE']).then(code => { auth.setCredentials(code.tokens); yt.search.list({ @@ -55,10 +59,8 @@ const web_search = (param: YTSearch) => new Promise((resolve, reject) => { return resolve(`${url}\n> ${title} | ${views} views | \ :+1: ${likes} — :-1: ${dislikes} \nby: ${by}.`.squeeze()); - }).catch(e => - reject(`No results, or API capped...\n\`\`\`\n${e}\n\`\`\``)); - }).catch(e => - reject(`No results, or API capped...\n\`\`\`\n${e}\n\`\`\``)); + }).catch(general_error); + }).catch(general_error); }).catch(err => { console.log('Error with code:', err); reject('Token probably expired, i.e. logged out.\n' diff --git a/lib/commands/b.ts b/lib/commands/b.ts @@ -13,14 +13,10 @@ export default home_scope => { const alphabet = 'pb'; - let letters = [...input].map((chr: any) => { - if (isNaN(chr) && alphabet.includes(chr)) - chr.bee() : `b`.bee(); - else if (chr === ' ') - return chr; - else - return chr; - }) + let letters = [...input].map((chr: any) => + isNaN(chr) && alphabet.includes(chr) + ? `b`.emojify() + : chr) message.channel.send(letters.join(' ')); }; diff --git a/lib/commands/cowsay.ts b/lib/commands/cowsay.ts @@ -1,5 +1,9 @@ -import { execFileSync } from 'child_process' +import { execFileSync as exec } from 'child_process' export default home_scope => { - const { message, args } = home_scope - message.answer('``` \n' + execFileSync('/bin/cowsay', args, {encoding: 'utf8'}) + '```') // this is safe because no shell is spawned -}- \ No newline at end of file + const { message, args } = home_scope; + // This is safe because no shell is spawned: + message.answer(exec('./node_modules/.bin/cowsay', args, { + encoding: 'utf8', + cwd: process.cwd() + }).format('```')); +}; diff --git a/lib/commands/cowthink.ts b/lib/commands/cowthink.ts @@ -0,0 +1,9 @@ +import { execFileSync as exec } from 'child_process' +export default home_scope => { + const { message, args } = home_scope; + // This is safe because no shell is spawned: + message.answer(exec('./node_modules/.bin/cowthink', args, { + encoding: 'utf8', + cwd: process.cwd() + }).format('```')); +}; diff --git a/lib/commands/figlet.ts b/lib/commands/figlet.ts @@ -1,5 +1,9 @@ -import { execFileSync } from 'child_process' +import { execFileSync as exec } from 'child_process' export default home_scope => { - const { message, args } = home_scope - message.answer('``` \n' + execFileSync('/bin/figlet', args, {encoding: 'utf8'}) + '```') // this is safe because no shell is spawned -}- \ No newline at end of file + const { message, args } = home_scope; + // This is safe because no shell is spawned: + message.answer(exec('./node_modules/.bin/figlet', args, { + encoding: 'utf8', + cwd: process.cwd() + }).format('```')); +}; diff --git a/lib/commands/youtube.ts b/lib/commands/youtube.ts @@ -2,5 +2,7 @@ import yt_search from '../api/yt_scrape'; export default home_scope => { const { message, args } = home_scope; const query = args.join(' '); - yt_search({ query }).then(message.reply.bind(message)).catch(message.answer.bind(message)); -}- \ No newline at end of file + yt_search({ query }) + .then(message.reply.bind(message)) + .catch(message.answer.bind(message)); +} diff --git a/lib/main.ts b/lib/main.ts @@ -179,9 +179,9 @@ export class SimpOMatic { const commands = read_dir(`${__dirname}/commands`) .map(n => n.slice(0, -3)); if (commands.includes(operator)) - return require(`./commands/${operator}`) - .default({ // Basic 'home-scope' is passed in. - message, args, ...HOMESCOPE}); + return import(`./commands/${operator}`).then(mod => + mod.default({ // Basic 'home-scope' is passed in. + message, args, ...HOMESCOPE})); switch (operator) { case 'commands': { diff --git a/package.json b/package.json @@ -37,8 +37,11 @@ "@types/node-fetch": "^2.5.5", "@types/ws": "^7.2.2", "better-pastebin": "^0.4.1", + "cowsay": "^1.4.0", "deepcopy": "^2.0.0", "discord.js": "11.6.1", + "figlet": "^1.3.0", + "figlet-cli": "^0.1.1", "google-auth-library": "^5.10.1", "googleapis": "^48.0.0", "node-fetch": "^2.6.0", diff --git a/yarn.lock b/yarn.lock @@ -3,9 +3,9 @@ "@typeit/discord@^1.0.3": - version "1.0.3" - resolved "https://registry.yarnpkg.com/@typeit/discord/-/discord-1.0.3.tgz#b24e25fe36ddc84f329186a9dc4170e6dcb3d419" - integrity sha512-s0ahyW51Rz3jDZNODbK5kJoZxBwxahQ5CevpaimZ5KlJylSBA8lM39IRx5QlBOG9TMVktJ2KehMzUDqxkm41PQ== + version "1.0.12" + resolved "https://registry.yarnpkg.com/@typeit/discord/-/discord-1.0.12.tgz#cf60ec626df6979fab75e9a96979c85808557681" + integrity sha512-CP5/GS+xD3Tzl4XPFOIlBBHJahSSb+RNOKLwUiltJv5hK2kIwZsfNh6Fv+Xo8/3lIE0tuPbOdOOUJ/T0V+/d7Q== dependencies: glob "^7.1.4" @@ -18,14 +18,14 @@ form-data "^3.0.0" "@types/node@*", "@types/node@^13.9.0": - version "13.9.0" - resolved "https://registry.yarnpkg.com/@types/node/-/node-13.9.0.tgz#5b6ee7a77faacddd7de719017d0bc12f52f81589" - integrity sha512-0ARSQootUG1RljH2HncpsY2TJBfGQIKOOi7kxzUY6z54ePu/ZD+wJA8zI2Q6v8rol2qpG/rvqsReco8zNMPvhQ== + version "13.9.2" + resolved "https://registry.yarnpkg.com/@types/node/-/node-13.9.2.tgz#ace1880c03594cc3e80206d96847157d8e7fa349" + integrity sha512-bnoqK579sAYrQbp73wwglccjJ4sfRdKU7WNEZ5FW4K2U6Kc0/eZ5kvXG0JKsEKFB50zrFmfFt52/cvBbZa7eXg== "@types/ws@^7.2.2": - version "7.2.2" - resolved "https://registry.yarnpkg.com/@types/ws/-/ws-7.2.2.tgz#1bd2038bc80aea60f8a20b2dcf08602a72e65063" - integrity sha512-oqnI3DbGCVI9zJ/WHdFo3CUE8jQ8CVQDUIKaDtlTcNeT4zs6UCg9Gvk5QrFx2QPkRszpM6yc8o0p4aGjCsTi+w== + version "7.2.3" + resolved "https://registry.yarnpkg.com/@types/ws/-/ws-7.2.3.tgz#a3add56077ac6cc9396b9502c7252a1635922032" + integrity sha512-VT/GK7nvDA7lfHy40G3LKM+ICqmdIsBLBHGXcWD97MtqQEjNMX+7Gudo8YGpaSlYdTX7IFThhCE8Jx09HegymQ== dependencies: "@types/node" "*" @@ -76,6 +76,11 @@ ajv@^6.5.5: json-schema-traverse "^0.4.1" uri-js "^4.2.2" +ansi-regex@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-3.0.0.tgz#ed0317c322064f79466c02966bddb605ab37d998" + integrity sha1-7QMXwyIGT3lGbAKWa922Bas32Zg= + array-equal@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/array-equal/-/array-equal-1.0.0.tgz#8c2a5ef2472fd9ea742b04c77a75093ba2757c93" @@ -228,6 +233,16 @@ core-util-is@1.0.2: resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac= +cowsay@^1.4.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/cowsay/-/cowsay-1.4.0.tgz#7c74e93d2c42542643135796577c3d7e628ba4ea" + integrity sha512-rdg5k5PsHFVJheO/pmE3aDg2rUDDTfPJau6yYkZYlHFktUz+UxbE+IgnUAEyyCyv4noL5ltxXD0gZzmHPCy/9g== + dependencies: + get-stdin "^5.0.1" + optimist "~0.6.1" + string-width "~2.1.1" + strip-eof "^1.0.0" + css-select@~1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/css-select/-/css-select-1.2.0.tgz#2b3a110539c5355f1cd8d314623e870b121ec858" @@ -459,6 +474,19 @@ fast-text-encoding@^1.0.0: resolved "https://registry.yarnpkg.com/fast-text-encoding/-/fast-text-encoding-1.0.1.tgz#4a428566f74fc55ebdd447555b1eb4d9cf514455" integrity sha512-x4FEgaz3zNRtJfLFqJmHWxkMDDvXVtaznj2V9jiP8ACUJrUgist4bP9FmDL2Vew2Y9mEQI/tG4GqabaitYp9CQ== +figlet-cli@^0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/figlet-cli/-/figlet-cli-0.1.1.tgz#109820992173361501602b108b5d207f7189a683" + integrity sha512-GZYTZqj5gJlcz70qHtpQgNDclvisZuGt8ivaTIL+QmMccwrRxA31xrHWITzNrdTsaRus32qO3aYzFx0dCmKHAQ== + dependencies: + figlet "^1.3.0" + optimist "~0.6.0" + +figlet@^1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/figlet/-/figlet-1.3.0.tgz#c49e3d92907ba13bebadc7124f76ba71f1f32ef0" + integrity sha512-f7A8aOJAfyehLJ7lQ6rEA8WJw7kOk3lfWRi5piSjkzbK5YkI5sqO8eiLHz1ehO+DM0QYB85i8VfA6XIGUbU1dg== + forever-agent@~0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91" @@ -515,6 +543,11 @@ gcp-metadata@^3.4.0: gaxios "^2.1.0" json-bigint "^0.3.0" +get-stdin@^5.0.1: + version "5.0.1" + resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-5.0.1.tgz#122e161591e21ff4c52530305693f20e6393a398" + integrity sha1-Ei4WFZHiH/TFJTAwVpPyDmOTo5g= + getpass@^0.1.1: version "0.1.7" resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.7.tgz#5eff8e3e684d569ae4cb2b1282604e8ba62149fa" @@ -655,6 +688,11 @@ inherits@2, inherits@^2.0.1, inherits@^2.0.3: resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c" integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ== +is-fullwidth-code-point@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz#a3b30a5c4f199183167aaab93beefae3ddfb654f" + integrity sha1-o7MKXE8ZkYMWeqq5O+764937ZU8= + is-stream@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-2.0.0.tgz#bde9c32680d6fae04129d6ac9d921ce7815f78e3" @@ -886,6 +924,11 @@ minimatch@^3.0.4: dependencies: brace-expansion "^1.1.7" +minimist@~0.0.1: + version "0.0.10" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf" + integrity sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8= + ms@^2.1.1: version "2.1.2" resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009" @@ -925,6 +968,14 @@ once@^1.3.0: dependencies: wrappy "1" +optimist@~0.6.0, optimist@~0.6.1: + version "0.6.1" + resolved "https://registry.yarnpkg.com/optimist/-/optimist-0.6.1.tgz#da3ea74686fa21a19a111c326e90eb15a0196686" + integrity sha1-2j6nRob6IaGaERwybpDrFaAZZoY= + dependencies: + minimist "~0.0.1" + wordwrap "~0.0.2" + optionator@^0.8.1: version "0.8.3" resolved "https://registry.yarnpkg.com/optionator/-/optionator-0.8.3.tgz#84fa1d036fe9d3c7e21d99884b601167ec8fb495" @@ -1098,6 +1149,14 @@ stealthy-require@^1.1.1: resolved "https://registry.yarnpkg.com/stealthy-require/-/stealthy-require-1.1.1.tgz#35b09875b4ff49f26a777e509b3090a3226bf24b" integrity sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks= +string-width@~2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/string-width/-/string-width-2.1.1.tgz#ab93f27a8dc13d28cac815c462143a6d9012ae9e" + integrity sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw== + dependencies: + is-fullwidth-code-point "^2.0.0" + strip-ansi "^4.0.0" + string_decoder@^1.1.1: version "1.3.0" resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.3.0.tgz#42f114594a46cf1a8e30b0a84f56c78c3edac21e" @@ -1105,6 +1164,18 @@ string_decoder@^1.1.1: dependencies: safe-buffer "~5.2.0" +strip-ansi@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-4.0.0.tgz#a8479022eb1ac368a871389b635262c505ee368f" + integrity sha1-qEeQIusaw2iocTibY1JixQXuNo8= + dependencies: + ansi-regex "^3.0.0" + +strip-eof@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/strip-eof/-/strip-eof-1.0.0.tgz#bb43ff5598a6eb05d89b59fcd129c983313606bf" + integrity sha1-u0P/VZim6wXYm1n80SnJgzE2Br8= + symbol-tree@^3.2.2: version "3.2.4" resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.4.tgz#430637d248ba77e078883951fb9aa0eed7c63fa2" @@ -1247,6 +1318,11 @@ word-wrap@~1.2.3: resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c" integrity sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ== +wordwrap@~0.0.2: + version "0.0.3" + resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107" + integrity sha1-o9XabNXAvAAI03I0u68b7WMFkQc= + wrappy@1: version "1.0.2" resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"