This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
generated from SinusBot-Scripts/Script-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alonemode.js
97 lines (87 loc) · 3.54 KB
/
alonemode.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
registerPlugin({
name: 'AloneMode',
version: '3.4.0',
backends: ['ts3', 'discord'],
description: 'This script will save CPU and bandwidth by stopping or muting the bot when nobody is listening anyways.',
author: 'SinusBot Team', // Michael Friese, Max Schmitt, Jonas Bögle, Fabian "fabm3n", Lala Sabathil
vars: [{
name: 'mode',
title: 'Mode',
type: 'select',
options: [
'mute only',
'stop playback'
]
}]
}, (_, {mode}) => {
const engine = require('engine')
const backend = require('backend')
const event = require('event')
const audio = require('audio')
const media = require('media')
const MUTE_ONLY = '0'
let isMuted = false
let lastPosition = 0
let lastTrack
audio.setMute(false)
if (engine.getBackend() == "ts3") {
event.on('clientMove', () => {
let currentChannel = backend.getCurrentChannel()
let clients = currentChannel ? currentChannel.getClientCount() : 0
if (clients > 1 && isMuted) {
isMuted = false
engine.log('Ending AloneMode...')
if (mode == MUTE_ONLY) {
audio.setMute(false)
} else {
if (lastTrack) {
lastTrack.play()
audio.seek(lastPosition)
engine.log(`Seeking to ${lastPosition} of track '${lastTrack.title()}'`)
}
}
} else if (clients <= 1 && audio.isPlaying() && isMuted == false) {
isMuted = true
engine.log('Starting AloneMode...')
if (mode == MUTE_ONLY) {
audio.setMute(true)
} else {
lastPosition = audio.getTrackPosition()
lastTrack = media.getCurrentTrack()
engine.log(`Position ${lastPosition} saved for track '${lastTrack.title()}'`)
media.stop()
}
}
})
} else {
event.on('discord:VOICE_STATE_UPDATE', function(ev) {
var cid = ev.channel_id;
let currentChannel = backend.getCurrentChannel()
let clients = currentChannel ? currentChannel.getClientCount() : 0
if (clients > 1 && isMuted && !(typeof cid === undefined || cid == null)) {
isMuted = false
engine.log('Ending AloneMode...')
if (mode == MUTE_ONLY) {
audio.setMute(false)
} else {
if (lastTrack) {
lastTrack.play()
audio.seek(lastPosition)
engine.log(`Seeking to ${lastPosition} of track '${lastTrack.title()}'`)
}
}
} else if (clients <= 1 && audio.isPlaying() && isMuted == false && (typeof cid === undefined || cid == null)) {
isMuted = true
engine.log('Starting AloneMode...')
if (mode == MUTE_ONLY) {
audio.setMute(true)
} else {
lastPosition = audio.getTrackPosition()
lastTrack = media.getCurrentTrack()
engine.log(`Position ${lastPosition} saved for track '${lastTrack.title()}'`)
media.stop()
}
}
})
}
})