jukebox discord only for now, fallback to current voice channel of user

This commit is contained in:
Evert Prants 2020-12-13 13:17:36 +02:00
parent 3fbb0c39f3
commit 3f291e33f6
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 34 additions and 14 deletions

View File

@ -1,9 +1,9 @@
{ {
"main": "plugin.js", "main": "plugin.js",
"name": "jukebox", "name": "jukebox",
"description": "Jukebox plugin for Icecast/Discord", "description": "Jukebox plugin for Discord",
"version": "1.1.0", "version": "1.1.1",
"tags": ["commands", "jukebox", "music"], "tags": ["commands", "jukebox", "music"],
"dependencies": ["simplecommands"], "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"]
} }

View File

@ -13,7 +13,7 @@ import { Readable } from 'stream';
import ytdl from 'ytdl-core'; import ytdl from 'ytdl-core';
import ytsr, { Video } from 'ytsr'; import ytsr, { Video } from 'ytsr';
import { Client, VoiceChannel } from 'discord.js'; import { Client, Message, VoiceChannel, VoiceConnection } from 'discord.js';
interface Configuration { interface Configuration {
control: string[]; control: string[];
@ -26,7 +26,7 @@ interface Cached {
config: Configuration; config: Configuration;
readable: Readable; readable: Readable;
url: string; url: string;
connection?: any; connection?: VoiceConnection;
queue: string[]; queue: string[];
} }
@ -34,14 +34,6 @@ interface Cached {
Discord config example: { Discord config example: {
"control": ["discord/testing/s:<guild id>/*"], "control": ["discord/testing/s:<guild id>/*"],
"voice": "<voice channel id>", "voice": "<voice channel id>",
"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(); 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') { if (!channel || channel.type !== 'voice') {
throw new Error('Invalid voice channel.'); throw new Error('Invalid voice channel.');
} }
@ -125,6 +129,7 @@ class JukeboxPlugin extends Plugin {
if (!cacheCurrent.queue || !cacheCurrent.queue.length) { if (!cacheCurrent.queue || !cacheCurrent.queue.length) {
vc.disconnect(); vc.disconnect();
cacheCurrent.connection = undefined;
return; return;
} }
@ -139,12 +144,14 @@ class JukeboxPlugin extends Plugin {
cached.playing = true; cached.playing = true;
cached.readable = str; cached.readable = str;
cached.url = link; cached.url = link;
cached.connection = vc;
} else { } else {
this.cache.push({ this.cache.push({
playing: true, playing: true,
readable: str, readable: str,
url: link, url: link,
config: cfg, config: cfg,
connection: vc,
queue: [], queue: [],
}); });
} }
@ -207,6 +214,12 @@ class JukeboxPlugin extends Plugin {
if (cached && cached.playing) { if (cached && cached.playing) {
cached.playing = false; cached.playing = false;
cached.readable.destroy(); cached.readable.destroy();
if (cached.connection) {
try {
cached.connection.disconnect();
cached.connection = undefined;
} catch (e) {}
}
msg.resolve('Stopped currently playing.'); msg.resolve('Stopped currently playing.');
} }
return true; return true;
@ -273,6 +286,12 @@ class JukeboxPlugin extends Plugin {
if (cached.playing) { if (cached.playing) {
cached.readable.destroy(); cached.readable.destroy();
cached.playing = false; cached.playing = false;
if (cached.connection) {
try {
cached.connection.disconnect();
cached.connection = undefined;
} catch (e) {}
}
} }
if (cached.queue.length) { if (cached.queue.length) {
const next = cached.queue.shift() as string; const next = cached.queue.shift() as string;
@ -288,6 +307,7 @@ class JukeboxPlugin extends Plugin {
msg.resolve('The queue is empty.'); msg.resolve('The queue is empty.');
return true; return true;
}, },
aliases: ['next'],
source: roomsWithJukeboxes, source: roomsWithJukeboxes,
description: 'Skip the current track', description: 'Skip the current track',
}]); }]);