init commit

master
Jan 2021-01-16 00:21:39 +01:00
commit 96ae89e3ac
5 changed files with 1767 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.env
data/
node_modules/

15
README.md Normal file
View File

@ -0,0 +1,15 @@
# Deluge Discord Webhook
Simple node.js script that logs updates on your Deluge server to Discord.
## Setup
Run `npm install` to install dependencies.
Create a file called `.env` and add your credentials like this:
URL=http://localhost:8112
PASS=password
WH_ID=123456789012345678
WH_TOKEN=long-ass-token
URL is your Deluge WebUI address and port, PASS is your WebUI password.
WH_ID and WH_TOKEN are your webhook ID/token.
Run with `node .` or using [pm2](https://github.com/Unitech/pm2)

89
index.js Normal file
View File

@ -0,0 +1,89 @@
require('dotenv').config();
const DelugeRPC = require('deluge-rpc');
const Enmap = require('enmap');
const { WebhookClient,
MessageEmbed } = require('discord.js');
const deluge = new DelugeRPC(process.env.URL, process.env.PASS);
const db = new Enmap({ name: 'db' });
const webhook = new WebhookClient(process.env.WH_ID, process.env.WH_TOKEN, { disableMentions: 'everyone' });
const update = async () => {
try {
const { torrents } = await deluge.getTorrentRecord();
if (!torrents) return;
Object.entries(torrents).forEach((data) => {
let [ hash, torrent ] = data;
let update = (status) => sendUpdate(status, hash, torrent, db.get(hash)?.state);
if (!db.get(hash)) {
update('added');
db.set(hash, torrent);
return;
} else {
let oldState = db.get(hash)?.state,
newState = torrent.state;
if (oldState == newState) return;
switch(true) {
case oldState == 'Seeding' && newState == 'Completed':
update('completed'); break;
case oldState == 'Downloading' && newState == 'Seeding':
update('seeding'); break;
case newState == 'Paused':
update('paused'); break;
case oldState == 'Paused' && (newState == 'Seeding' || newState == 'Downloading'):
update('unpaused'); break;
case oldState != 'Moving' && newState == 'Moving':
update('moving'); break;
default:
update('changed');
}
db.set(hash, torrent);
}
});
db.forEach((torrent, hash) => {
if (Object.keys(torrents).indexOf(hash) == -1) {
sendUpdate('removed', hash, torrent, 'None');
db.delete(hash);
}
});
} catch(e) {
console.error(e);
webhook.send(new MessageEmbed().setDescription(''+error).setColor('ff0000')).catch();
}
}
update();
setInterval(update, 1000);
/**
*
* @param {'added'|'changed'|'paused'|'unpaused'|'seeding'|'completed','removed'|'moving'} update
* @param {string} hash
* @param {DelugeRPC.DelugeTorrentRecord} torrent
*/
let sendUpdate = (update, hash, torrent, oldState) => {
console.log(`${update} : ${hash} (${torrent.state})`);
let embed = new MessageEmbed()
.setAuthor(torrent.name)
.setTitle(updateFriendlyNames[update] || update)
.addField('Seeds / Peers', `${torrent.total_seeds} / ${torrent.total_peers}`, true)
.addField('Progress', `${Math.round(torrent.progress * 10) / 10}%`, true)
.addField('Ratio', Math.round(torrent.ratio * 100) / 100, true)
.addField('Change', `${oldState} -> ${torrent.state}`, false)
.addField('Hash', hash, false)
.setColor(0x4983c6);
webhook.send(embed);
}
let updateFriendlyNames = {
added: 'Torrent added',
paused: 'Torrent paused',
unpaused: 'Torrent unpaused',
seeding: 'Torrent started seeding',
completed: 'Torrent completed',
changed: 'Torrent updated',
removed: 'Torrent removed',
moving: 'Moving'
}

1652
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

8
package.json Normal file
View File

@ -0,0 +1,8 @@
{
"dependencies": {
"deluge-rpc": "^1.0.5",
"discord.js": "^12.5.1",
"dotenv": "^8.2.0",
"enmap": "^5.8.2"
}
}