forked from LockBlock-dev/qbittorrent-rp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (42 loc) · 1.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const qbit = require("qbittorrent-api-v2")
const discord = require("discord-rpc")
const config = require("./config")
const clientId = "877963304813867068"
const startTimestamp = new Date()
const client = new discord.Client({ transport: 'ipc' });
const bytesToSize = (bytes) => {
const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
if (bytes === 0) return '0 B'
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10)
if (i === 0) return `${bytes} ${sizes[i]}`
return `${(bytes / (1024 ** i)).toFixed(1)} ${sizes[i]}`
}
const averageRatio = (torrents) => {
var ratio, c = 0
for (var t in torrents) {
ratio += torrents[t].ratio
c += 1
}
return (ratio/c).toFixed(2)
}
const discordRPC = async () => {
const api = await qbit.connect(`http://${config.host.ip}:${config.host.port}`, config.user, config.password)
let torrents, speeds, version
torrents = await api.torrents()
speeds = await api.transferInfo()
version = await api.appVersion()
ratio = averageRatio(torrents)
client.setActivity({
details: `${torrents.length} active torrents | Average ratio: ${ratio}`,
state: `↑: ${bytesToSize(speeds.up_info_speed)}/s | ↓: ${bytesToSize(speeds.dl_info_speed)}/s`,
startTimestamp,
largeImageKey: "qbittorrent-logo",
largeImageText: `qBittorrent ${version}`
})
}
client.on("connected", async () => {
console.log("Connected to Discord!")
setInterval(() => { discordRPC() }, 10000) // update activity every 10 seconds
})
process.on("unhandledRejection", console.error)
client.login({ clientId });