From 3f291e33f6b5fec658893a0273400a13994ab9f7 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Sun, 13 Dec 2020 13:17:36 +0200 Subject: [PATCH] jukebox discord only for now, fallback to current voice channel of user --- jukebox/plugin.json | 6 +++--- jukebox/plugin.ts | 42 +++++++++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/jukebox/plugin.json b/jukebox/plugin.json index 4f1b074..1ac67a4 100644 --- a/jukebox/plugin.json +++ b/jukebox/plugin.json @@ -1,9 +1,9 @@ { "main": "plugin.js", "name": "jukebox", - "description": "Jukebox plugin for Icecast/Discord", - "version": "1.1.0", + "description": "Jukebox plugin for Discord", + "version": "1.1.1", "tags": ["commands", "jukebox", "music"], "dependencies": ["simplecommands"], - "npmDependencies": ["ytdl-core@^4.1.3","@discordjs/opus@^0.3.3","discord.js@^12.5.1","ytsr@1.0.4"] + "npmDependencies": ["ytdl-core@^4.1.4","@discordjs/opus@^0.3.3","discord.js@^12.5.1","ytsr@1.0.4"] } diff --git a/jukebox/plugin.ts b/jukebox/plugin.ts index bbeeda7..748631f 100644 --- a/jukebox/plugin.ts +++ b/jukebox/plugin.ts @@ -13,7 +13,7 @@ import { Readable } from 'stream'; import ytdl from 'ytdl-core'; import ytsr, { Video } from 'ytsr'; -import { Client, VoiceChannel } from 'discord.js'; +import { Client, Message, VoiceChannel, VoiceConnection } from 'discord.js'; interface Configuration { control: string[]; @@ -26,7 +26,7 @@ interface Cached { config: Configuration; readable: Readable; url: string; - connection?: any; + connection?: VoiceConnection; queue: string[]; } @@ -34,14 +34,6 @@ interface Cached { Discord config example: { "control": ["discord/testing/s:/*"], "voice": "", - "leaveAfter": 10, - } - - Icecast config example: { - "control": ["irc/icynet/#music"], - "icecast": { - "server": "http://localhost:8080" - } } */ @@ -105,7 +97,19 @@ class JukeboxPlugin extends Plugin { cached.readable.destroy(); } - const channel = await discord.channels.fetch(cfg.voice as string); + let channel; + if (!cfg.voice) { + const member = (msg.data as Message).member; + if (member && member.voice) { + const avc = member.voice.channel; + if (avc && avc.joinable) { + channel = avc; + } + } + } else { + channel = await discord.channels.fetch(cfg.voice as string); + } + if (!channel || channel.type !== 'voice') { throw new Error('Invalid voice channel.'); } @@ -125,6 +129,7 @@ class JukeboxPlugin extends Plugin { if (!cacheCurrent.queue || !cacheCurrent.queue.length) { vc.disconnect(); + cacheCurrent.connection = undefined; return; } @@ -139,12 +144,14 @@ class JukeboxPlugin extends Plugin { cached.playing = true; cached.readable = str; cached.url = link; + cached.connection = vc; } else { this.cache.push({ playing: true, readable: str, url: link, config: cfg, + connection: vc, queue: [], }); } @@ -207,6 +214,12 @@ class JukeboxPlugin extends Plugin { if (cached && cached.playing) { cached.playing = false; cached.readable.destroy(); + if (cached.connection) { + try { + cached.connection.disconnect(); + cached.connection = undefined; + } catch (e) {} + } msg.resolve('Stopped currently playing.'); } return true; @@ -273,6 +286,12 @@ class JukeboxPlugin extends Plugin { if (cached.playing) { cached.readable.destroy(); cached.playing = false; + if (cached.connection) { + try { + cached.connection.disconnect(); + cached.connection = undefined; + } catch (e) {} + } } if (cached.queue.length) { const next = cached.queue.shift() as string; @@ -288,6 +307,7 @@ class JukeboxPlugin extends Plugin { msg.resolve('The queue is empty.'); return true; }, + aliases: ['next'], source: roomsWithJukeboxes, description: 'Skip the current track', }]);