2020-09-05 08:22:39 +02:00
|
|
|
const CONFIG = require('../config.json');
|
|
|
|
const Discord = require('discord.js');
|
2020-09-08 13:17:55 +02:00
|
|
|
const {
|
|
|
|
checkJellyfinItemIDRegex
|
|
|
|
} = require('./util');
|
2020-09-05 08:59:09 +02:00
|
|
|
const {
|
2020-09-21 04:05:49 +02:00
|
|
|
getAudioDispatcher
|
2020-09-05 08:59:09 +02:00
|
|
|
} = require('./dispachermanager');
|
2020-09-05 08:22:39 +02:00
|
|
|
|
|
|
|
const discordclientmanager = require('./discordclientmanager');
|
2020-09-08 13:17:55 +02:00
|
|
|
const jellyfinClientManager = require('./jellyfinclientmanager');
|
2020-09-21 04:05:49 +02:00
|
|
|
const playbackmanager = require('./playbackmanager');
|
|
|
|
const websocketHanler = require('./websockethandler');
|
2020-09-05 08:59:09 +02:00
|
|
|
const discordClient = discordclientmanager.getDiscordClient();
|
2020-09-05 08:22:39 +02:00
|
|
|
|
|
|
|
|
2020-09-08 13:17:55 +02:00
|
|
|
var isSummendByPlay = false;
|
|
|
|
|
|
|
|
|
2020-09-05 08:59:09 +02:00
|
|
|
//random Color of the Jellyfin Logo Gradient
|
|
|
|
function getRandomDiscordColor() {
|
2020-09-08 13:17:55 +02:00
|
|
|
function randomNumber(b, a) {
|
|
|
|
return Math.floor((Math.random() * Math.pow(Math.pow((b - a), 2), 1 / 2)) + (b > a ? a : b))
|
2020-09-05 08:59:09 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 13:17:55 +02:00
|
|
|
const GRANDIENT_START = '#AA5CC3';
|
|
|
|
const GRANDIENT_END = '#00A4DC';
|
|
|
|
|
|
|
|
let rS = GRANDIENT_START.slice(1, 3);
|
|
|
|
let gS = GRANDIENT_START.slice(3, 5);
|
|
|
|
let bS = GRANDIENT_START.slice(5, 7);
|
|
|
|
rS = parseInt(rS, 16);
|
|
|
|
gS = parseInt(gS, 16);
|
|
|
|
bS = parseInt(bS, 16);
|
|
|
|
|
|
|
|
let rE = GRANDIENT_END.slice(1, 3);
|
|
|
|
let gE = GRANDIENT_END.slice(3, 5);
|
|
|
|
let bE = GRANDIENT_END.slice(5, 7);
|
|
|
|
rE = parseInt(rE, 16);
|
|
|
|
gE = parseInt(gE, 16);
|
|
|
|
bE = parseInt(bE, 16);
|
|
|
|
|
|
|
|
return ('#' + ('00' + (randomNumber(rS, rE)).toString(16)).substr(-2) + ('00' + (randomNumber(gS, gE)).toString(16)).substr(-2) + ('00' + (randomNumber(bS, bE)).toString(16)).substr(-2));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function searchForItemID(searchString) {
|
|
|
|
|
|
|
|
let response = await jellyfinClientManager.getJellyfinClient().getSearchHints({
|
|
|
|
searchTerm: searchString,
|
|
|
|
includeItemTypes: "Audio"
|
|
|
|
})
|
|
|
|
|
|
|
|
if (response.TotalRecordCount < 1) {
|
|
|
|
throw "Found no Song"
|
|
|
|
} else {
|
|
|
|
return response.SearchHints[0].ItemId
|
|
|
|
}
|
|
|
|
}
|
2020-09-05 08:59:09 +02:00
|
|
|
|
2020-09-08 13:17:55 +02:00
|
|
|
function summon(voiceChannel){
|
2020-09-21 04:05:49 +02:00
|
|
|
voiceChannel.join()
|
|
|
|
}
|
2020-09-05 08:59:09 +02:00
|
|
|
|
2020-09-21 04:05:49 +02:00
|
|
|
function summonMessage(message){
|
|
|
|
if (!message.member.voice.channel) {
|
|
|
|
message.reply('please join a voice channel to summon me!');
|
|
|
|
}else if(message.channel.type === 'dm'){
|
|
|
|
message.reply('no dms')
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
summon(message.member.voice.channel)
|
2020-09-08 13:17:55 +02:00
|
|
|
}
|
2020-09-05 08:59:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleChannelMessage(message) {
|
2020-09-08 13:17:55 +02:00
|
|
|
getRandomDiscordColor()
|
2020-09-05 08:22:39 +02:00
|
|
|
|
|
|
|
if (message.content.startsWith(CONFIG["discord-prefix"] + 'summon')) {
|
2020-09-21 04:05:49 +02:00
|
|
|
isSummendByPlay = false;
|
2020-09-05 08:22:39 +02:00
|
|
|
|
2020-09-21 04:05:49 +02:00
|
|
|
websocketHanler.openSocket();
|
|
|
|
|
|
|
|
summonMessage(message);
|
2020-09-08 13:17:55 +02:00
|
|
|
|
2020-09-05 08:22:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
} else if (message.content.startsWith(CONFIG["discord-prefix"] + 'disconnect')) {
|
2020-09-21 04:05:49 +02:00
|
|
|
playbackmanager.stop()
|
|
|
|
jellyfinClientManager.getJellyfinClient().closeWebSocket();
|
2020-09-05 08:22:39 +02:00
|
|
|
discordClient.user.client.voice.connections.forEach((element) => {
|
|
|
|
element.disconnect();
|
|
|
|
});
|
2020-09-08 13:17:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
} else if ((message.content.startsWith(CONFIG["discord-prefix"] + 'pause')) || (message.content.startsWith(CONFIG["discord-prefix"] + 'resume'))) {
|
2020-09-05 08:59:09 +02:00
|
|
|
if (getAudioDispatcher() !== undefined) {
|
2020-09-21 04:05:49 +02:00
|
|
|
playbackmanager.playPause();
|
2020-09-05 08:59:09 +02:00
|
|
|
} else {
|
2020-09-08 13:17:55 +02:00
|
|
|
message.reply("there is nothing playing!")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} else if (message.content.startsWith(CONFIG["discord-prefix"] + 'play')) {
|
|
|
|
|
|
|
|
|
|
|
|
if (discordClient.user.client.voice.connections.size < 1) {
|
2020-09-21 04:05:49 +02:00
|
|
|
summonMessage(message)
|
2020-09-08 13:17:55 +02:00
|
|
|
isSummendByPlay=true
|
2020-09-05 08:22:39 +02:00
|
|
|
}
|
2020-09-08 13:17:55 +02:00
|
|
|
|
|
|
|
async function playThis(){
|
|
|
|
|
|
|
|
let indexOfItemID = message.content.indexOf(CONFIG["discord-prefix"] + 'play') + (CONFIG["discord-prefix"] + 'play').length + 1;
|
|
|
|
let argument = message.content.slice(indexOfItemID);
|
|
|
|
let itemID;
|
|
|
|
//check if play command was used with itemID
|
|
|
|
let regexresults = checkJellyfinItemIDRegex(argument);
|
|
|
|
if (regexresults) {
|
|
|
|
itemID = regexresults[0];
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
itemID = await searchForItemID(argument);
|
|
|
|
} catch (e) {
|
|
|
|
message.reply(e);
|
2020-09-21 04:44:08 +02:00
|
|
|
playbackmanager.stop(discordClient.user.client.voice.connections.first());
|
|
|
|
return;
|
2020-09-08 13:17:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
discordClient.user.client.voice.connections.forEach((element) => {
|
2020-09-21 04:05:49 +02:00
|
|
|
playbackmanager.startPlaying(element,itemID,isSummendByPlay);
|
2020-09-08 13:17:55 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
playThis();
|
|
|
|
|
|
|
|
} else if (message.content.startsWith(CONFIG["discord-prefix"] + 'stop')) {
|
2020-09-21 04:05:49 +02:00
|
|
|
if(isSummendByPlay){
|
|
|
|
if(discordClient.user.client.voice.connections.size > 0){
|
|
|
|
playbackmanager.stop(discordClient.user.client.voice.connections.first());
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
playbackmanager.stop();
|
|
|
|
}
|
2020-09-08 13:17:55 +02:00
|
|
|
|
2020-09-05 08:59:09 +02:00
|
|
|
} else if (message.content.startsWith(CONFIG["discord-prefix"] + 'help')) {
|
2020-09-05 08:22:39 +02:00
|
|
|
const reply = new Discord.MessageEmbed()
|
2020-09-05 08:59:09 +02:00
|
|
|
.setColor(getRandomDiscordColor())
|
|
|
|
.addFields({
|
|
|
|
name: `${CONFIG['discord-prefix']}summon`,
|
|
|
|
value: 'Join the channel the author of the message'
|
|
|
|
}, {
|
|
|
|
name: `${CONFIG['discord-prefix']}disconnect`,
|
|
|
|
value: 'Disconnect from all current Voice Channels'
|
|
|
|
}, {
|
|
|
|
name: `${CONFIG['discord-prefix']}play`,
|
|
|
|
value: 'Play the following item'
|
|
|
|
}, {
|
|
|
|
name: `${CONFIG['discord-prefix']}pause/resume`,
|
|
|
|
value: 'Pause/Resume audio'
|
|
|
|
}, {
|
|
|
|
name: `${CONFIG['discord-prefix']}help`,
|
|
|
|
value: 'Display this help message'
|
|
|
|
})
|
2020-09-05 08:22:39 +02:00
|
|
|
message.channel.send(reply);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
handleChannelMessage
|
|
|
|
}
|