Simp-O-Matic

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

commit 6225aeb4fba19d8d3ce1000cd2846e106514d57c
parent a968dcec3b69c96dcc2eee600334840c94513617
Author: Demonstrandum <moi@knutsen.co>
Date:   Wed, 11 Nov 2020 00:51:21 +0000

Attempt to clean up !vc command. Added global instance variable object, per guild.

Diffstat:
Mlib/commands/vc.ts | 91++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Mlib/default.ts | 6++----
Mlib/extensions.ts | 24++++++++++++++++++------
Mlib/main.ts | 10+++++++++-
Mtsconfig.json | 2+-
Myarn.lock | 35++++++++++++++++++++++++++++++++++-
6 files changed, 119 insertions(+), 49 deletions(-)

diff --git a/lib/commands/vc.ts b/lib/commands/vc.ts @@ -1,66 +1,85 @@ -const ytdl = require("ytdl-core"); +import { TextChannel } from 'discord.js'; +import ytdl from 'ytdl-core'; + + +const DL_OPTIONS : any = { filter: 'audioonly', dlChunkSize: 0 }; + export default async(home_scope: HomeScope) => { - const { message, args, CONFIG } = home_scope; + const { message, args, CONFIG, CLIENT, INSTANCE_VARIABLES } = home_scope; + if(!message.guild) { message.answer("Just use youtube-dl at home."); return; }; - if(!CONFIG.vcqueue) - CONFIG.vcqueue = []; - switch(args[0]){ + + const guild : string = message.guild.id; + const GID : Types.GuildInstanceData = INSTANCE_VARIABLES.guilds[guild]; + + if(!CONFIG.vc_queue) CONFIG.vc_queue = []; + + switch (args[0]) { case "join": - if(message.member.voice.channel){ - CONFIG.vc = await message.member.voice.channel.join(); - CONFIG.vcc = message.channel; - } - else + if (message.member.voice.channel) { + GID.vc = await message.member.voice.channel.join(); + CONFIG.vc_channel = message.channel.id; + } else { message.reply("Join A Channel First."); + } break; case "leave": - try{ - CONFIG.vc.disconnect(); - } catch (error){ - message.answer(error); + try { + GID.vc.disconnect(); + } catch (error) { + message.answer("```" + `${error}` + "```"); } break; case "pause": - if(CONFIG.vcdispatcher) - CONFIG.vcdispatcher.pause(); - else - message.answer("Nothing is playng"); + if (GID.vc_dispatcher) GID.vc_dispatcher.pause(); + else message.answer("Nothing is playng"); break; case "play": - if(CONFIG.vcdispatcher) - CONFIG.vcdispatcher.resume(); - else{ - CONFIG.vcdispatcher = CONFIG.vc.play(ytdl(CONFIG.vcqueue.pop(), { filter: 'audioonly' })); - CONFIG.vcdispatcher.on("finish", () => { - CONFIG.vcdispatcher.destroy(); - let next = CONFIG.vcqueue.pop(); - if(next){ - CONFIG.vcdispatcher = CONFIG.vc.play(ytdl(CONFIG.vcqueue.pop(), { filter: 'audioonly' })); - CONFIG.vcc.send("Now playing: " + next) + if (GID.vc_dispatcher) { + GID.vc_dispatcher.resume(); + } else { + GID.vc_dispatcher = GID.vc.play( + ytdl(CONFIG.vc_queue.pop(), DL_OPTIONS)); + + GID.vc_dispatcher.on("finish", () => { + GID.vc_dispatcher.destroy(); + const next = CONFIG.vc_queue.pop(); + if (next) { + GID.vc_dispatcher = GID.vc.play( + ytdl(CONFIG.vc_queue.pop(), DL_OPTIONS)); + CLIENT.channels.fetch(CONFIG.vc_channel) + .then((ch: TextChannel) => + ch.send(`Now playing: ${next}`)); } }) } break; case "d": + CONFIG.vc_queue.splice(Number(args[1]), 1); + break; case "i": - const splice_args : any = args[0] == 'd' ? [1] : [0, args[2]]; - CONFIG.vcqueue = CONFIG.vcqueue.splice(Number(args[1]), ...splice_args); + CONFIG.vc_queue.splice(Number(args[1]), 0, args[2]); + break; case "ls": - message.answer(ls(CONFIG)); + message.answer(ls(CONFIG.vc_queue)); break; case "requeue": - CONFIG.vcqueue = []; + CONFIG.vc_queue = []; message.answer("Queue cleared"); + GID.vc_dispatcher.end(); + break; case "skip": - CONFIG.vcdispatcher.end(); + GID.vc_dispatcher.end(); break; default: - CONFIG.vcqueue.push(args[0]); //add checking for valid URIs? + // TODO: Add checking for valid URIs? + CONFIG.vc_queue.push(args[0]); } } -function ls(CONFIG : Types.Config){ //this could be more sophisticated - return "length: " + CONFIG.vcqueue.length + CONFIG.vcqueue.map((a, b) => "\n" + b + ": " + a).join(""); +function ls(queue : string[]) { // TODO: This could be more sophisticated. + return "Queue size: " + queue.length + + queue.map((e, i) => `\n ${i + 1}: ${e}`).join(''); } diff --git a/lib/default.ts b/lib/default.ts @@ -5,6 +5,8 @@ const DEFAULT_GUILD_CONFIG : Types.Config = { main_channel: null, system_channel: null, + vc_channel: null, + vc_queue: [], whitelistchannels: [], stats: { actions: { @@ -14,10 +16,6 @@ const DEFAULT_GUILD_CONFIG : Types.Config = { rape: 0 } }, - vc: null, - vcc: null, - vcqueue: null, - vcdispatcher: null, pp_sizes: { '541761315887120399': 16, '265958795254038535': 36 diff --git a/lib/extensions.ts b/lib/extensions.ts @@ -1,5 +1,6 @@ import { SimpOMatic } from './main'; import { Client } from '@typeit/discord'; +import { VoiceConnection, StreamDispatcher } from 'discord.js'; // Global Extensions: declare global { @@ -9,8 +10,12 @@ declare global { GIT_URL: string, HELP_MESSAGES: string[], HELP_SECTIONS: string[] , ALL_HELP: string[], CONFIG: Types.Config, SECRETS: any, KNOWN_COMMANDS: string[], - expand_alias: (operator: string, args: string[], message: Message) => string, - CLIENT: Client, main: SimpOMatic; + expand_alias: + (operator: string, + args: string[], + message: Message) => string, + CLIENT: Client, main: SimpOMatic, + INSTANCE_VARIABLES: Types.InstanceVariables }; namespace Types { @@ -36,9 +41,20 @@ declare global { } }; + export type GuildInstanceData = { + vc: VoiceConnection, + vc_dispatcher: StreamDispatcher + }; + + export type InstanceVariables = { + guilds: { [key: string]: GuildInstanceData } + }; + export type Config = { main_channel: string, system_channel: string, + vc_channel: string, + vc_queue: string[], whitelistchannels: string[], pp_sizes: { [key: string]: number @@ -49,10 +65,6 @@ declare global { [key: string]: string }, stats: Stats, - vc: any, - vcc: any, - vcqueue: any[], - vcdispatcher: any, commands: { prefix: string, max_history: number, diff --git a/lib/main.ts b/lib/main.ts @@ -6,6 +6,8 @@ import { Discord, On, Client } from '@typeit/discord'; import { Message, MessageAttachment, MessageEmbed, + VoiceConnection, + StreamDispatcher, TextChannel } from 'discord.js'; // System interaction modules. @@ -32,6 +34,11 @@ import { pastebin_latest, import { Guild } from 'discord.js'; import { Timer } from './commands/cron'; +// Global instance variables +const INSTANCE_VARIABLES = { + guilds: {} +} + // Anything that hasn't been defined in `bot.json` // will be taken care of by the defaults. let GLOBAL_CONFIG : Types.GlobalConfig = { @@ -349,7 +356,8 @@ Would you like to slow down a little?`.squeeze()); HELP_MESSAGES, HELP_SECTIONS, ALL_HELP, CONFIG, SECRETS, KNOWN_COMMANDS, expand_alias: this.expand_alias, - CLIENT: SimpOMatic._CLIENT, main: this + CLIENT: SimpOMatic._CLIENT, main: this, + INSTANCE_VARIABLES }; const commands = read_dir(`${__dirname}/commands`) diff --git a/tsconfig.json b/tsconfig.json @@ -39,7 +39,7 @@ "noUnusedLocals": false, /* Report errors on unused locals. */ "noUnusedParameters": false, /* Report errors on unused parameters. */ "noImplicitReturns": false, /* Report error when not all code paths in function return a value. */ - "noFallthroughCasesInSwitch": false, /* Report errors for fallthrough cases in switch statement. */ + "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ /* Module Resolution Options */ "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ diff --git a/yarn.lock b/yarn.lock @@ -1255,6 +1255,11 @@ has-unicode@^2.0.0: resolved "https://registry.yarnpkg.com/has-unicode/-/has-unicode-2.0.1.tgz#e0e6fe6a28cf51138855e086d1691e771de2a8b9" integrity sha1-4Ob+aijPUROIVeCG0Wkedx3iqLk= +html-entities@^1.3.1: + version "1.3.1" + resolved "https://registry.yarnpkg.com/html-entities/-/html-entities-1.3.1.tgz#fb9a1a4b5b14c5daba82d3e34c6ae4fe701a0e44" + integrity sha512-rhE/4Z3hIhzHAUKbW8jVcCyuT5oJCXXqhN/6mXXVCpzTmvJnoH2HL/bt3EZ6p55jbFJBeAe1ZNpL5BugLujxNA== + htmlparser2@^3.9.1: version "3.10.1" resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-3.10.1.tgz#bd679dc3f59897b6a34bb10749c855bb53a9392f" @@ -1573,6 +1578,14 @@ lru-cache@^5.0.0: dependencies: yallist "^3.0.2" +m3u8stream@^0.8.0: + version "0.8.0" + resolved "https://registry.yarnpkg.com/m3u8stream/-/m3u8stream-0.8.0.tgz#025a63358ee32d7652bdc0a93f46078582ec5e96" + integrity sha512-vvSjdkBPdDHzVr2M+aIXbnYys4zX6m3UzxMaxBJr1PpE0e/3sawkLD4EEmz/q9hv87bleotR70cOWR3UBMtskw== + dependencies: + miniget "^2.0.1" + sax "^1.2.4" + mime-db@1.44.0: version "1.44.0" resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.44.0.tgz#fa11c5eb0aca1334b4233cb4d52f10c5a6272f92" @@ -1624,6 +1637,11 @@ min-document@^2.19.0: dependencies: dom-walk "^0.1.0" +miniget@^2.0.1, miniget@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/miniget/-/miniget-2.1.0.tgz#2dfb9ecb3a9a55d9dc682102f65fca2a06e3f5ca" + integrity sha512-fy9x3d/0oOIhkwAms6kgxTYkHwdELhMfgj+9a/aYZpJdTWIIWGta9aXHUtnzUn+LjBmRoTdPRQSi2hkmEvXk3A== + minimatch@^3.0.4: version "3.0.4" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" @@ -1687,6 +1705,11 @@ node-fetch@^2.3.0, node-fetch@^2.6.0, node-fetch@^2.6.1: resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052" integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw== +node-forge@^0.10.0: + version "0.10.0" + resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.10.0.tgz#32dea2afb3e9926f02ee5ce8794902691a676bf3" + integrity sha512-PPmu8eEeG9saEUvI97fm4OYxXVB6bFvyNTyiUOBichBpFG8A1Ljw3bY62+5oOjDEMHRnd0Y7HQ+x7uzxOzC6JA== + node-forge@^0.9.0: version "0.9.1" resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.9.1.tgz#775368e6846558ab6676858a4d8c6e8d16c677b5" @@ -2097,7 +2120,7 @@ safe-buffer@~5.1.0, safe-buffer@~5.1.1: resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a" integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg== -sax@>=0.6.0, sax@^1.2.4: +sax@>=0.6.0, sax@^1.1.3, sax@^1.2.4: version "1.2.4" resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9" integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw== @@ -2467,3 +2490,13 @@ yallist@^3.0.0, yallist@^3.0.2, yallist@^3.0.3: version "3.1.1" resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.1.1.tgz#dbb7daf9bfd8bac9ab45ebf602b8cbad0d5d08fd" integrity sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g== + +ytdl-core@^4.0.3: + version "4.0.3" + resolved "https://registry.yarnpkg.com/ytdl-core/-/ytdl-core-4.0.3.tgz#9772dc6f7f0272534d50f50903022f8502ae44fa" + integrity sha512-+pM+EocvdHHTfH3xCr3c41cIm8bD7IE/wv/QKjaO7PwdLaaOMIj7xc/7yWwy9NwUDgIKA1YTotcn0qpQ0FVtMA== + dependencies: + html-entities "^1.3.1" + m3u8stream "^0.8.0" + miniget "^2.1.0" + sax "^1.1.3"