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",
"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"]
}

View File

@ -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:<guild 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();
}
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',
}]);