Simp-O-Matic

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

commit 8f1ec1812e50e3d3dcbe3825e17efcbc7b800fae
parent eb1a044ba8e7d04a912808d1a0d86b28a180f60f
Author: knutsen <samuel@knutsen.co>
Date:   Sun, 30 May 2021 21:06:30 +0100

Add `ignoreConstraints` parameter to `on_message` function.

This allows bot-issued and self-issued commands always, when `true`.

Diffstat:
Mlib/commands/cron.ts | 2+-
Mlib/commands/get.ts | 3++-
Mlib/commands/tts.ts | 17++++++++++-------
Mlib/commands/vc.ts | 18++++++++++--------
Mlib/main.ts | 33++++++++++++++++++++++++++-------
Mpackage.json | 1+
Myarn.lock | 73++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
7 files changed, 120 insertions(+), 27 deletions(-)

diff --git a/lib/commands/cron.ts b/lib/commands/cron.ts @@ -181,7 +181,7 @@ export class Timer { this.homescope.main.on_message( [this.homescope.message], this.homescope.CLIENT, - null, true + null, true, true ); } diff --git a/lib/commands/get.ts b/lib/commands/get.ts @@ -20,7 +20,8 @@ export default (homescope: HomeScope) => { .replace(/\n/g, '\n@@@').split('@@@'), 1980) .map(s => '```js\n' + s + '\n```'); - for (const msg of msgs) + message.reply(msgs[0]); + for (const msg of msgs.slice(1)) message.channel.send(msg); } catch (e) { message.channel.send(`Invalid object access-path\n` diff --git a/lib/commands/tts.ts b/lib/commands/tts.ts @@ -5,7 +5,7 @@ export default async (hs : HomeScope) => { const { message, args, INSTANCE_VARIABLES } = hs; if (!message.guild) { - message.reply("Stop talkingQ to yourself, loser."); + message.reply("Stop talking to yourself, loser."); return; } @@ -14,6 +14,10 @@ export default async (hs : HomeScope) => { if (!GID.vc) { message.reply("Let me join your voice-chat first."); + return; + } else if (GID.vc_dispatcher && GID.vc_current_stream) { + GID.vc_dispatcher.pause(); + GID.vc_current_stream.pause(); } const text = args.join(' '); @@ -22,21 +26,20 @@ export default async (hs : HomeScope) => { const child = cp.spawn('espeak', ['-s170', text, '--stdout'], { stdio: ['ignore', 'pipe', 'ignore'] }); - const stream = child.stdout; - const temp = GID.vc.play(stream); + const dispatcher = GID.vc.play(stream); - child.on('close', () => child.kill()); - stream.on('end', () => { - stream.pause(); + dispatcher.on('finish', () => { child.kill(); - temp.destroy(); + dispatcher.destroy(); + if (GID.vc_current_stream) { // THIS DOES NOT WORK. I cannot seem to get the song to // resume (if there was a song playing). I've tried // many ways, but someone else is going to have to figure // it out. + GID.vc_current_stream.resume(); GID.vc_dispatcher = GID.vc.play(GID.vc_current_stream); console.log("Resumed playback."); } diff --git a/lib/commands/vc.ts b/lib/commands/vc.ts @@ -27,24 +27,22 @@ export default async (homescope: HomeScope) => { const child = cp.spawn('youtube-dl', [...YTDL_OPTIONS, url], { stdio: ['ignore', 'pipe', 'pipe'] }); - child.stdout.on('end', () => { - child.stdout.pause(); - child.stderr.pause(); - child.kill(); - }); child.on('close', async (code) => { if (code && code !== 0) { console.log(`Exited with code ${code}:`); - console.log(await child.stderr.utf8()); stream = null; child.stdout = null; GID.vc_prefetch[url] = null; CONFIG.vc_queue = CONFIG.vc_queue.filter(q => q !== url); message.reply("Error downloading media."); } + console.log("Child exited fine! Dead now...") child.kill(); }); stream = child.stdout; + child.stderr.on('data', chunk => { + console.warn(`stderr: ${chunk}`); + }); } catch (e) { console.log(e); } if (stream) { @@ -149,10 +147,14 @@ export default async (homescope: HomeScope) => { }); }; - const stream = get_prefetch(CONFIG.vc_queue.shift()); + + message.reply("Playing media from queue..."); + const url = CONFIG.vc_queue.shift(); + const stream = get_prefetch(url); + console.log('Stream:', stream); GID.vc_current_stream = stream; GID.vc_dispatcher = GID.vc.play(stream); - message.channel.send("Playing media from queue..."); + message.channel.send(`Starting off with: ${url}.`); set_event_listeners(); break; diff --git a/lib/main.ts b/lib/main.ts @@ -253,6 +253,9 @@ export abstract class SimpOMatic { console.log("Non-master related git event."); } else if (body.console === true) { if (body.secret !== process.env["WEB_SECRET"]) { + console.warn( + "Console message was sent,", + "but web secret was wrong!"); return; } const send_message = (msg: string, guild?) => { @@ -314,12 +317,17 @@ export abstract class SimpOMatic { console.log('[command] Processing.'); const CONFIG = GLOBAL_CONFIG.guilds[message.guild.id]; - if (message.content.startsWith('..')) return; + if (message.content.startsWith('..')) { + console.log("`..' was probably an ellipses."); + return; + } if (CONFIG.whitelistchannels && CONFIG.whitelistchannels.length > 0 - && !CONFIG.whitelistchannels.includes(message.channel.id)) + && !CONFIG.whitelistchannels.includes(message.channel.id)) { + console.log("[command] Channel was not whitelisted!"); return; + } console.log('[command] Whitelisted.'); @@ -496,7 +504,10 @@ Would you like to slow down a little?`.squeeze()); const p = CONFIG.commands.prefix; message.content = `${p}${response}`; // Send it back as a command. - this.on_message([message], SimpOMatic._CLIENT); + this.on_message([message], + SimpOMatic._CLIENT, + null, false, + true); // Ignore author constraints = true. } } for (const rejecter of CONFIG.rules.reject) { @@ -597,7 +608,8 @@ Would you like to slow down a little?`.squeeze()); @On("message") async on_message([message]: ArgsOf<'message'>, client : Client, guardPayload: any = null, - ignoreSpam = false) { + ignoreSpam = false, + ignoreConstraints = false) { if (!message.guild) { console.warn("Message not in a guild channel."); console.log(message); @@ -611,13 +623,18 @@ Would you like to slow down a little?`.squeeze()); const CONFIG = GLOBAL_CONFIG.guilds[guild_id]; // Ignore empty messages... - if (!message.content) return; + if (!message.content) { + console.log("Empty (text) messages are ignored."); + return; + } console.log('Message acknowledged.'); console.log('Message from Guild ID:', guild_id); - if (SimpOMatic._CLIENT.user.id === message.author.id) { + if (!ignoreConstraints && SimpOMatic._CLIENT.user.id === message.author.id) { + console.log("Messages from self are ignored."); return; } + console.log('Message received:', message.content); try { @@ -635,7 +652,9 @@ Would you like to slow down a little?`.squeeze()); if (message.content[0] === CONFIG.commands.prefix) { console.log('Message type: command.'); - if (message.author.bot && !CONFIG.commands.bot_issued) { + if (!ignoreConstraints + && message.author.bot + && !CONFIG.commands.bot_issued) { message.reply("**I am dubious about letting other bots " + "isssue commands.**\n " + "Say `" + CONFIG.commands.prefix diff --git a/package.json b/package.json @@ -48,6 +48,7 @@ "discord-api-types": "discordjs/discord-api-types#main", "discord.js": "discordjs/discord.js#master", "erlpack": "discord/erlpack", + "ffmpeg-static": "^4.3.0", "figlet": "^1.3.0", "figlet-cli": "^0.1.1", "fortune-teller": "^0.1.2", diff --git a/yarn.lock b/yarn.lock @@ -30,6 +30,16 @@ dependencies: regenerator-runtime "^0.13.4" +"@derhuerst/http-basic@^8.2.0": + version "8.2.1" + resolved "https://registry.yarnpkg.com/@derhuerst/http-basic/-/http-basic-8.2.1.tgz#11e3964cf1f2fb40806f9a0fff0c451bb7526093" + integrity sha512-Rmn7qQQulw2sxJ8qGfZ7OuqMWuhz8V+L5xnYKMF5cXVcYqmgWqlVEAme90pF7Ya8OVhxVxLmhh0rI2k6t7ITWw== + dependencies: + caseless "^0.12.0" + concat-stream "^1.6.2" + http-response-object "^3.0.1" + parse-cache-control "^1.0.1" + "@discordjs/collection@^0.1.6": version "0.1.6" resolved "https://registry.yarnpkg.com/@discordjs/collection/-/collection-0.1.6.tgz#9e9a7637f4e4e0688fd8b2b5c63133c91607682c" @@ -419,6 +429,11 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.25.tgz#15967a7b577ff81383f9b888aa6705d43fbbae93" integrity sha512-EPpXLOVqDvisVxtlbvzfyqSsFeQxltFbluZNRndIb8tr9KiBnYNLzrc1N3pyKUCww2RNrfHDViqDWWE1LCJQtQ== +"@types/node@^10.0.3": + version "10.17.60" + resolved "https://registry.yarnpkg.com/@types/node/-/node-10.17.60.tgz#35f3d6213daed95da7f0f73e75bcc6980e90597b" + integrity sha512-F0KIgDJfy2nA3zMLmWGKxcH2ZVEtCZXHHdOQs2gSaQ27+lNeEfGxzkIw90aXswATX7AZ33tahPbzy6KAfUreVw== + "@types/node@^13.9.3": version "13.13.41" resolved "https://registry.yarnpkg.com/@types/node/-/node-13.13.41.tgz#045a4981318d31a581650ce70f340a32c3461198" @@ -785,6 +800,11 @@ buffer-fill@^1.0.0: resolved "https://registry.yarnpkg.com/buffer-fill/-/buffer-fill-1.0.0.tgz#f8f78b76789888ef39f205cd637f68e702122b2c" integrity sha1-+PeLdniYiO858gXNY39o5wISKyw= +buffer-from@^1.0.0: + version "1.1.1" + resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef" + integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A== + buffer@^5.2.0: version "5.7.1" resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.7.1.tgz#ba62e7c13133053582197160851a8f648e99eed0" @@ -856,7 +876,7 @@ caller@~0.0.1: dependencies: tape "~2.3.2" -caseless@~0.12.0: +caseless@^0.12.0, caseless@~0.12.0: version "0.12.0" resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc" integrity sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw= @@ -1005,6 +1025,16 @@ concat-map@0.0.1: resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s= +concat-stream@^1.6.2: + version "1.6.2" + resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.6.2.tgz#904bdf194cd3122fc675c77fc4ac3d4ff0fd1a34" + integrity sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw== + dependencies: + buffer-from "^1.0.0" + inherits "^2.0.3" + readable-stream "^2.2.2" + typedarray "^0.0.6" + console-control-strings@^1.0.0, console-control-strings@~1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/console-control-strings/-/console-control-strings-1.1.0.tgz#3d7cf4464db6446ea644bf4b39507f9851008e8e" @@ -1266,6 +1296,11 @@ end-of-stream@^1.0.0, end-of-stream@^1.1.0: dependencies: once "^1.4.0" +env-paths@^2.2.0: + version "2.2.1" + resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-2.2.1.tgz#420399d416ce1fbe9bc0a07c62fa68d67fd0f8f2" + integrity sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A== + erlpack@discord/erlpack: version "0.1.3" resolved "https://codeload.github.com/discord/erlpack/tar.gz/e27db8f82892bdb9b28a0547cc394d68b5d2242d" @@ -1446,6 +1481,16 @@ fast-text-encoding@^1.0.0: resolved "https://registry.yarnpkg.com/fast-text-encoding/-/fast-text-encoding-1.0.3.tgz#ec02ac8e01ab8a319af182dae2681213cfe9ce53" integrity sha512-dtm4QZH9nZtcDt8qJiOH9fcQd1NAgi+K1O2DbE6GG1PPCK/BWfOH3idCTRQ4ImXRUOyopDEgDEnVEE7Y/2Wrig== +ffmpeg-static@^4.3.0: + version "4.3.0" + resolved "https://registry.yarnpkg.com/ffmpeg-static/-/ffmpeg-static-4.3.0.tgz#fa5f689eacb0b3f7ed3eaa52760d132d9db818ec" + integrity sha512-w/tXYGlOSeAkPHjypjzylaChLrG5wRzHFyB47KFRDsGyBxUJJWiq9I/39/e6r9Y4aY1gzpejTLg5Aa0aqb0XXA== + dependencies: + "@derhuerst/http-basic" "^8.2.0" + env-paths "^2.2.0" + https-proxy-agent "^5.0.0" + progress "^2.0.3" + figlet-cli@^0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/figlet-cli/-/figlet-cli-0.1.1.tgz#109820992173361501602b108b5d207f7189a683" @@ -1920,6 +1965,13 @@ http-cache-semantics@^4.0.0: resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.0.tgz#49e91c5cbf36c9b94bcfcd71c23d5249ec74e390" integrity sha512-carPklcUh7ROWRK7Cv27RPtdhYhUsela/ue5/jKzjegVvXDqM2ILE9Q2BGn9JZJh1g87cp56su/FgQSzcWS8cQ== +http-response-object@^3.0.1: + version "3.0.2" + resolved "https://registry.yarnpkg.com/http-response-object/-/http-response-object-3.0.2.tgz#7f435bb210454e4360d074ef1f989d5ea8aa9810" + integrity sha512-bqX0XTF6fnXSQcEJ2Iuyr75yVakyjIDCqroJQ/aHfSdlM743Cwqoi2nDYMzLGWUcuTWGWy8AAvOKXTfiv6q9RA== + dependencies: + "@types/node" "^10.0.3" + http-signature@~1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/http-signature/-/http-signature-1.2.0.tgz#9aecd925114772f3d95b65a60abb8f7c18fbace1" @@ -1974,7 +2026,7 @@ inflight@^1.0.4: once "^1.3.0" wrappy "1" -inherits@2, inherits@^2.0.1, inherits@~2.0.1, inherits@~2.0.3: +inherits@2, inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.1, inherits@~2.0.3: version "2.0.4" resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c" integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ== @@ -3022,6 +3074,11 @@ parse-bmfont-xml@^1.1.4: xml-parse-from-string "^1.0.0" xml2js "^0.4.5" +parse-cache-control@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/parse-cache-control/-/parse-cache-control-1.0.1.tgz#8eeab3e54fa56920fe16ba38f77fa21aacc2d74e" + integrity sha1-juqz5U+laSD+Fro493+iGqzC104= + parse-glob@^3.0.4: version "3.0.4" resolved "https://registry.yarnpkg.com/parse-glob/-/parse-glob-3.0.4.tgz#b2c376cfb11f35513badd173ef0bb6e3a388391c" @@ -3117,6 +3174,11 @@ process@^0.11.10: resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182" integrity sha1-czIwDoQBYb2j5podHZGn1LwW8YI= +progress@^2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8" + integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA== + prompt@0.2.14: version "0.2.14" resolved "https://registry.yarnpkg.com/prompt/-/prompt-0.2.14.tgz#57754f64f543fd7b0845707c818ece618f05ffdc" @@ -3202,7 +3264,7 @@ read@1.0.x: dependencies: mute-stream "~0.0.4" -readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.3.0, readable-stream@^2.3.5: +readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.2.2, readable-stream@^2.3.0, readable-stream@^2.3.5: version "2.3.7" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.7.tgz#1eca1cf711aef814c04f62252a36a62f6cb23b57" integrity sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw== @@ -3807,6 +3869,11 @@ type-fest@^0.10.0: resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.10.0.tgz#7f06b2b9fbfc581068d1341ffabd0349ceafc642" integrity sha512-EUV9jo4sffrwlg8s0zDhP0T2WD3pru5Xi0+HTE3zTUmBaZNhfkite9PdSJwdXLwPVW0jnAHT56pZHIOYckPEiw== +typedarray@^0.0.6: + version "0.0.6" + resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" + integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= + typescript@^4.2.4: version "4.3.2" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.3.2.tgz#399ab18aac45802d6f2498de5054fcbbe716a805"