put api client back to node modules and added some things

This commit is contained in:
KGT1 2020-09-08 13:17:55 +02:00
parent 6d1ac84be0
commit 5a8ab5dc26
14 changed files with 181 additions and 254 deletions

View File

@ -1,17 +0,0 @@
'use strict';
const DISCORD = require('discord.js');
const JELLYFIN_CLIENT = require('jellyfin-apiclient');
const CONFIG = require('./config.json');
const DISCORD_CLIENT = new DISCORD.Client();
client.on('ready', () => {
console.log('connected to Discord');
});
client.login('your token here');

View File

@ -1,13 +0,0 @@
# Jellyfin Contributors
- [thornbill](https://github.com/thornbill)
- [cvium](https://github.com/cvium)
- [Oddstr13](https://github.com/oddstr13)
- [Andrei Oanca](https://github.com/OancaAndrei)
# Emby Contributors
- [LukePulverenti](https://github.com/LukePulverenti)
- [ebr11](https://github.com/ebr11)
- [softworkz](https://github.com/softworkz)
- [HazCod](https://github.com/HazCod)

View File

@ -1,22 +0,0 @@
The MIT License
Copyright (c) Emby https://emby.media
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -1,71 +0,0 @@
<h1 align="center">Jellyfin API Client for JavaScript</h1>
<h3 align="center">Part of the <a href="https://jellyfin.media">Jellyfin Project</a></h3>
---
<p align="center">
<img alt="Logo Banner" src="https://raw.githubusercontent.com/jellyfin/jellyfin-ux/master/branding/SVG/banner-logo-solid.svg?sanitize=true"/>
<br/>
<br/>
<a href="https://github.com/jellyfin/jellyfin-apiclient-javascript">
<img alt="MIT License" src="https://img.shields.io/github/license/jellyfin/jellyfin-apiclient-javascript.svg"/>
</a>
<a href="https://opencollective.com/jellyfin">
<img alt="Donate" src="https://img.shields.io/opencollective/all/jellyfin.svg?label=backers"/>
</a>
<a href="https://features.jellyfin.org">
<img alt="Feature Requests" src="https://img.shields.io/badge/fider-vote%20on%20features-success.svg"/>
</a>
<a href="https://forum.jellyfin.org">
<img alt="Discuss on our Forum" src="https://img.shields.io/discourse/https/forum.jellyfin.org/users.svg"/>
</a>
<a href="https://matrix.to/#/+jellyfin:matrix.org">
<img alt="Chat on Matrix" src="https://img.shields.io/matrix/jellyfin:matrix.org.svg?logo=matrix"/>
</a>
<a href="https://www.reddit.com/r/jellyfin">
<img alt="Join our Subreddit" src="https://img.shields.io/badge/reddit-r%2Fjellyfin-%23FF5700.svg"/>
</a>
</p>
This library is meant to help clients written in JavaScript or TypeScript interact with Jellyfin's REST API.
## Compatibility
This library depends on the Fetch and Promise APIs. These will be expected to be polyfilled if used in a browser that doesn't support them.
## Build Process
### Dependencies
- Yarn
### Getting Started
1. Clone or download this repository
```sh
git clone https://github.com/jellyfin/jellyfin-apiclient-javascript.git
cd jellyfin-apiclient-javascript
```
2. Install build dependencies in the project directory
```sh
yarn install
```
3. Build the library for production
```sh
yarn build
```
4. Build the library for development
```sh
yarn dev
```
## Building Documentation
This library is documented using [JSDoc](https://jsdoc.app/) style comments. Documentation can be generated in HTML format by running `yarn docs` and viewing the files in any modern browser. The resulting documentation will be saved in the `docs` directory.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,2 +0,0 @@
// Add fetch polyfill for jest
import 'isomorphic-fetch';

View File

@ -1,90 +0,0 @@
{
"_from": "jellyfin-apiclient",
"_id": "jellyfin-apiclient@1.4.1",
"_inBundle": false,
"_integrity": "sha512-BTTRucQ4tCLyiZ9kR9nAoxqxYp5/z+MCzkayy9vmMZ5C7jlVVsnxAXuuZjoa+AgXMjohXcM5Ci54myfJM1pRkA==",
"_location": "/jellyfin-apiclient",
"_phantomChildren": {},
"_requested": {
"type": "tag",
"registry": true,
"raw": "jellyfin-apiclient",
"name": "jellyfin-apiclient",
"escapedName": "jellyfin-apiclient",
"rawSpec": "",
"saveSpec": null,
"fetchSpec": "latest"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/jellyfin-apiclient/-/jellyfin-apiclient-1.4.1.tgz",
"_shasum": "5e544a19bc001b16669eb7ecf46bb7d652365e41",
"_spec": "jellyfin-apiclient",
"_where": "/home/kilian/Documents/GitTests/Jellyfin/jellyfin-discord-music-bot",
"author": "",
"browserslist": [
"last 2 Firefox versions",
"last 2 Chrome versions",
"last 2 ChromeAndroid versions",
"last 2 Safari versions",
"last 2 iOS versions",
"last 2 Edge versions",
"Chrome 27",
"Chrome 38",
"Chrome 47",
"Chrome 53",
"Chrome 56",
"Chrome 63",
"Firefox ESR"
],
"bugs": {
"url": "https://github.com/jellyfin/jellyfin-apiclient-javascript/issues"
},
"bundleDependencies": false,
"dependencies": {},
"deprecated": false,
"description": "API client for Jellyfin",
"devDependencies": {
"@babel/core": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@types/jest": "^25.2.1",
"@types/node": "^13.13.5",
"@typescript-eslint/eslint-plugin": "^2.31.0",
"@typescript-eslint/parser": "^2.31.0",
"babel-loader": "^8.0.5",
"eslint": "^6.8.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jest": "^23.9.0",
"eslint-plugin-promise": "^4.2.1",
"isomorphic-fetch": "^2.2.1",
"jest": "^26.0.1",
"jest-junit": "^10.0.0",
"jsdoc": "^3.6.4",
"prettier": "2.0.5",
"source-map-loader": "^0.2.4",
"ts-jest": "^25.5.0",
"ts-loader": "^7.0.3",
"typescript": "^3.8.3",
"webpack": "^4.43.0",
"webpack-cli": "^3.2.1"
},
"homepage": "https://github.com/jellyfin/jellyfin-apiclient-javascript#readme",
"license": "MIT",
"main": "dist/jellyfin-apiclient.js",
"name": "jellyfin-apiclient",
"repository": {
"type": "git",
"url": "git+https://github.com/jellyfin/jellyfin-apiclient-javascript.git"
},
"scripts": {
"build": "webpack --mode production",
"dev": "webpack --mode development",
"docs": "jsdoc src -r -R README.md -d docs",
"lint": "eslint \"src\"",
"prepare": "webpack",
"test": "jest"
},
"version": "1.4.1"
}

View File

@ -2,9 +2,11 @@
"name": "jellyfin-discord-music-bot",
"version": "0.0.1",
"description": "A Discord Music Bot for the Jellyfin Media Server",
"main": "index.js",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node src/index.js",
"postinstall": "npx patch-package"
},
"repository": {
"type": "git",
@ -24,6 +26,10 @@
"dependencies": {
"@discordjs/opus": "^0.3.2",
"discord.js": "^12.3.1",
"jellyfin-apiclient": "^1.4.1"
"jellyfin-apiclient": "^1.4.1",
"node-fetch": "^2.6.0",
"nodejs": "0.0.0",
"window": "^4.2.7",
"ytdl-core": "^3.2.2"
}
}

File diff suppressed because one or more lines are too long

31
src/index.js Normal file
View File

@ -0,0 +1,31 @@
const CONFIG = require('../config.json');
const jellyfinClientManager=require('./jellyfinclientmanager');
const discordclientmanager= require('./discordclientmanager');
discordclientmanager.init();
const discordClient=discordclientmanager.getDiscordClient();
const {audioDispatcher} = require('./dispachermanager');
const {handleChannelMessage}=require('./messagehandler');
jellyfinClientManager.init();
jellyfinClientManager.getJellyfinClient().getPublicSystemInfo().then((response) => {
jellyfinClientManager.getJellyfinClient().authenticateUserByName(CONFIG["jellyfin-username"],CONFIG["jellyfin-password"]).then((response)=>{
console.log(response)
jellyfinClientManager.getJellyfinClient().setAuthenticationInfo(response.AccessToken, response.SessionInfo.UserId);
});
})
discordClient.on('ready', () => {
console.log('connected to Discord');
});
discordClient.on('message', message => {
handleChannelMessage(message);
});
discordClient.login(CONFIG.token);

View File

@ -0,0 +1,19 @@
const { ApiClient } = require('jellyfin-apiclient');
const CONFIG = require('../config.json');
const os = require('os');
var jellyfinClient;
function init(){
jellyfinClient = new ApiClient(CONFIG["server-adress"], CONFIG["jellyfin-app-name"], "0.0.1", os.hostname(), os.hostname());
}
function getJellyfinClient(){
return jellyfinClient;
}
module.exports = {
getJellyfinClient,
init
}

View File

@ -1,16 +1,21 @@
const CONFIG = require('../config.json');
const Discord = require('discord.js');
const {
checkJellyfinItemIDRegex
} = require('./util');
const {
getAudioDispatcher,
setAudioDispatcher
} = require('./dispachermanager');
const discordclientmanager = require('./discordclientmanager');
const jellyfinClientManager = require('./jellyfinclientmanager');
const discordClient = discordclientmanager.getDiscordClient();
var isSummendByPlay = false;
//random Color of the Jellyfin Logo Gradient
function getRandomDiscordColor() {
function randomNumber(b, a) {
@ -37,6 +42,29 @@ function getRandomDiscordColor() {
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
}
}
function summon(voiceChannel){
if (!voiceChannel) {
return message.reply('please join a voice channel to summon me!');
}
voiceChannel.join()
}
function handleChannelMessage(message) {
getRandomDiscordColor()
@ -45,30 +73,74 @@ function handleChannelMessage(message) {
return;
}
const voiceChannel = message.member.voice.channel;
summon(message.member.voice.channel);
if (!voiceChannel) {
return message.reply('please join a voice channel to summon me!');
}
voiceChannel.join().then(connection => {
const stream = `${CONFIG['server-adress']}/Audio/0751d668d58afb25b755eca639c498ed/universal?UserId=d5ed94520ff542378b368246683ff9de&DeviceId=Jellyfin%20Discord%20Music%20Bot&MaxStreamingBitrate=320000&Container=opus&AudioCodec=opus&api_key=${CONFIG["jellyfin-api-key"]}&TranscodingContainer=ts&TranscodingProtocol=hls`;
setAudioDispatcher(connection.play(stream));
});
} else if (message.content.startsWith(CONFIG["discord-prefix"] + 'disconnect')) {
discordClient.user.client.voice.connections.forEach((element) => {
element.disconnect();
});
} else if (message.content.startsWith(CONFIG["discord-prefix"] + 'pause')) {
} else if ((message.content.startsWith(CONFIG["discord-prefix"] + 'pause')) || (message.content.startsWith(CONFIG["discord-prefix"] + 'resume'))) {
if (getAudioDispatcher() !== undefined) {
if (getAudioDispatcher().paused)
getAudioDispatcher().resume();
else
getAudioDispatcher().pause(true);
} else {
console.log("WHYYYYYYY")
console.log(getAudioDispatcher());
message.reply("there is nothing playing!")
}
} else if (message.content.startsWith(CONFIG["discord-prefix"] + 'play')) {
if (discordClient.user.client.voice.connections.size < 1) {
discordClient.user.client.voice.connections.size
summon(message.member.voice.channel)
isSummendByPlay=true
}
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);
}
}
discordClient.user.client.voice.connections.forEach((element) => {
let stream = `${jellyfinClientManager.getJellyfinClient().serverAddress()}/Audio/${itemID}/universal?UserId=${jellyfinClientManager.getJellyfinClient().getCurrentUserId()}&DeviceId=${jellyfinClientManager.getJellyfinClient().deviceId()}&MaxStreamingBitrate=${element.channel.bitrate.toString()}&Container=opus&AudioCodec=opus&api_key=${jellyfinClientManager.getJellyfinClient().accessToken()}&TranscodingContainer=ts&TranscodingProtocol=hls`;
setAudioDispatcher(element.play(stream));
element.on("error", (error) => {
console.error(error);
})
getAudioDispatcher().on("finish",()=>{
if(isSummendByPlay){
element.disconnect();
}
})
})
}
playThis();
} else if (message.content.startsWith(CONFIG["discord-prefix"] + 'stop')) {
getAudioDispatcher().pause()
setAudioDispatcher(undefined)
} else if (message.content.startsWith(CONFIG["discord-prefix"] + 'help')) {
const reply = new Discord.MessageEmbed()
.setColor(getRandomDiscordColor())

8
src/util.js Normal file
View File

@ -0,0 +1,8 @@
function checkJellyfinItemIDRegex(strgintomatch){
let regexresult=strgintomatch.match(/([0-9]|[a-f]){32}/);
return regexresult;
}
module.exports={
checkJellyfinItemIDRegex
}