-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtts-events.plugin.js
547 lines (471 loc) · 17.7 KB
/
tts-events.plugin.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//META{"name":"TTSEvents"}*//
/* global console, BdApi, BDfunctionsDevilBro*/
// TTSEvents Plugin for a friend of mine.
// Works under BD v0.2.82, JS v1.792 by Jiiks
// BBD v0.0.6 by Zerebos
let AUTHOR = 'Semnodime'
let VERSION = '1.1.0'
let A = 'TTS-EVENTS'
let debug = false
class ChannelEvent {
constructor(username, type, channel, channel_2) {
this.username = username
this.joined = (type === 'joined')
this.moved = (type === 'moved')
this.disconnected = (type === 'disconnected')
this.channel = channel
this.from = channel
this.to = channel_2
}
}
class VoiceChannel {
constructor(name, users, current) {
this.name = name
this.users = users
this.current = current
}
has_user(username) {
return (undefined !== this.users.find(user => (user === username)))
}
}
class User {
constructor(name) {
this.name = name
this.channel_before = undefined
this.channel_after = undefined
}
}
class TTSEvents {
// Static Class
static getName() {
return 'TTS-Events'
}
static getShortName() {
return 'ttsEvents'
}
static getDescription() {
return 'Plays text-to-speech messages on events such as users joining your voice channel.' + 'Limitation: VoiceChannel must be visible in Discord in order to be recognized by this plugin'
}
static getVersion() {
return VERSION
}
static getAuthor() {
return AUTHOR
}
// Plugin Interface Base
getName() {
return TTSEvents.getName()
}
getShortName() {
return TTSEvents.getShortName()
}
getDescription() {
return TTSEvents.getDescription()
}
getVersion() {
return TTSEvents.getVersion()
}
getAuthor() {
return TTSEvents.getAuthor()
}
getSettingsPanel() {
let self = this
if (!this.started || typeof BDfunctionsDevilBro !== 'object') {
console.error(A, 'getSettingsPanel Failed')
return
}
let save_key = TTSEvents.getShortName() + '-settings'
let loaded_settings = BDfunctionsDevilBro.loadAllData(this, save_key)
window.console.log(A, 'LOAD_SETTINGS', loaded_settings, loaded_settings.length)
if (!$.isEmptyObject(loaded_settings)) {
this.settings = loaded_settings
}
console.log(A, 'ACTUAL_SETTINGS', this.settings, 'default', this.defaults, 'loaded', loaded_settings)
let html = `<div class="bda-header"><span class="bda-header-title"><span class="bda-name">${TTSEvents.getName() + ' Settings'}</span> v<span class="bda-version">${TTSEvents.getVersion()}</span> by <span class="bda-author">${TTSEvents.getAuthor()}</span></span></div>`
for (let key in this.settings) {
if (this.settings.hasOwnProperty(key)) {
//console.log(A, 'iterator', self.settings[key].value, self.defaults[key].value, 'input-' + key)
html += `<div class="flex-3B1Tl4 tooltip-custom" style="margin: 5px 0">`
html += ` <h3 style="flex: 0 0 50%; line-height: 38px; align-self:baseline">${this.defaults[key].description}</h3>`
html += ` <div style="flex: 1 1 auto;" class="inputWrapper-3xoRWR vertical-3X17r5 flex-3B1Tl4 align-self:baseline">`
html += ` <input type="text" value="${self.settings[key].value}" placeholder="${self.defaults[key].value}" class="inputDefault-Y_U37D input-2YozMi size16-3IvaX_" id="${'input-' + key}">`
html += ` <span class="tooltiptext-custom">${self.defaults[key].tooltip}</span>`
html += ` </div>`
html += `</div>`
}
}
// For some reason you nead to wrap everything in ONE div
let settingshtml = `<div>${html}</div>`
let settingspanel = $(settingshtml)[0]
BDfunctionsDevilBro.initElements(settingspanel)
function sanatize(input) {
// Make values save to be printed into html value attribute (between "")
// Whitelist approach
return ('' + input).replace(/[^a-zA-Z0-9äÄöÖüÜ %._]+/g, '')
}
for (let key in this.settings) {
if (this.settings.hasOwnProperty(key)) {
$(settingspanel).on('change', '#input-' + key, function () {
// Sanatize Value
let v = sanatize(this.value)
// Validate Value
v = self.defaults[key].validator(v)
// Save Value
self.settings[key].value = v
console.log(A, 'SAVE_SETTINGS', self.settings)
BDfunctionsDevilBro.saveAllData(self.settings, self, save_key)
})
}
}
return settingspanel
}
// Plugin Constructor
constructor() {
let self = this
function voice_sample() {
setTimeout(function () {
self.speak('Test voice sample')
}, 42)
}
this.channelCache = []
console.log(A, 'CONSTRUCTOR CALLED')
this.defaults = {
speech_volume: {
value: '100',
description: 'Speech Volume %',
tooltip: 'Value from Mute: 0 to 100 :Loud',
validator: function (v) {
let d = self.defaults.speech_volume.value
v = parseInt(v)
if (isNaN(v) || v < 0 || v > 100)
return d
voice_sample()
return v
}
},
speech_rate: {
value: '10',
description: 'Speech Rate',
tooltip: 'Value from Slow: 0 to 100 :Fast',
validator: function (v) {
let d = self.defaults.speech_rate.value
v = parseInt(v)
if (isNaN(v)) return d
if (v < 0 || v > 100)
return d
voice_sample()
return v
}
},
speech_pitch: {
value: '10',
description: 'Voice Pitch',
tooltip: 'Value from Low: 0 to 100 :High',
validator: function (v) {
let d = self.defaults.speech_pitch.value
v = parseInt(v)
if (isNaN(v) || v < 0 || v > 100)
return d
voice_sample()
return v
}
},
message_join: {
value: 'User %u joined from channel %f',
description: 'User joined',
tooltip: '%u=User %f=Channel',
validator: function (v) {
let d = self.defaults.message_join.value
if (v.length === 0)
return d
return v
}
},
message_disconnect: {
value: 'User %u left channel %f',
description: 'User disconnection',
tooltip: '%u=User %f=Channel',
validator: function (v) {
let d = self.defaults.message_disconnect.value
if (v.length === 0)
return d
return v
}
},
message_moved_in: {
value: 'User %u moved here from channel %f',
description: 'User moved & joined',
tooltip: '%u=User %f=ChannelFrom %t=ChannelTo',
validator: function (v) {
let d = self.defaults.message_moved_in.value
if (v.length === 0)
return d
return v
}
},
message_moved_out: {
value: 'User %u moved to channel %t',
description: 'User moved & disconnected',
tooltip: '%u=User %f=ChannelFrom %t=ChannelTo',
validator: function (v) {
let d = self.defaults.message_moved_out.value
if (v.length === 0)
return d
return v
}
},
}
this.settings = this.defaults
this.style = `<style>
.tooltip-custom {
position: relative;
}
.tooltip-custom .tooltiptext-custom {
visibility: hidden;
min-width: 120px;
max-width: 50%;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: 20%;
left: 15%;
margin-left: -60px;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip-custom .tooltiptext-custom::after {
content: "";
position: absolute;
top: 20%;
left: 0%;
margin-left: -14px;
border: 7px solid transparent border-right-color: #555;
}
.tooltip-custom:hover .tooltiptext-custom {
visibility: visible;
opacity: 0.9;
}
</style>`
}
// Plugin Interface for Start/Stop
load() {
console.log(A, 'LOADED')
if (debug)
this.speak(A + 'LOADED')
}
unload() {
console.log(A, 'UNLOADED')
if (debug)
this.speak(A + 'UNLOADED')
}
start() {
this.started = true
this.initialize()
this.startCache()
console.log(A, 'STARTED')
if (debug)
this.speak(A + 'STARTED')
}
initialize() {
if (typeof BDfunctionsDevilBro === 'object')
BDfunctionsDevilBro.loadMessage(this)
BdApi.injectCSS(this.getShortName(), this.style)
console.log(A, 'INITIALIZED')
if (debug)
this.speak(A + 'INITIALIZED')
}
stop() {
this.started = false
if (typeof BDfunctionsDevilBro === 'object')
BDfunctionsDevilBro.unloadMessage(this)
this.stopCache()
BdApi.clearCSS(this.getShortName())
console.log(A, 'STOPPED')
if (debug)
this.speak(A + 'STOPPED')
}
// Own Plugin Helper Functions
startCache() {
let self = this
this.cacheInterval = setInterval(function () {
self.watch()
}, 1000);
}
stopCache() {
if (this.cacheInterval !== undefined)
clearInterval(this.cacheInterval)
}
ownUsername() {
// Return own username from UI DOM
let user_div_result = $('.container-iksrDt .username')
if (user_div_result.length > 0)
return $(user_div_result).text()
}
ownChannel() {
// Return own username from getVoiceChannels()
let self = this
let channel = this.getVoiceChannels().find(channel => channel.has_user(self.ownUsername()))
if (channel)
return channel.name
}
getDeafened() {
// Check if the user is deafened from UI DOM
let deafened_bg = 'url("/assets/c8845c514bbf3f1e5bea064c1e40b08d.svg")'
let deafened_results = $('.button-1aU9q1').filter((i, elem) => (elem.style['background-image'] === deafened_bg))
return deafened_results.length === 1
}
getVoiceChannels() {
// Get the list of voice channels and their users from the UI DOM
let voiceChannels = []
let voiceChannel_Containers = $('.containerDefault-7RImuF')
voiceChannel_Containers.each(function (index, container) {
let channel_name = $($(container).find('.name-2SL4ev')[0]).text()
let channel_is_selected = ($(container).find('.nameSelectedVoice-XpjYTw').length !== 0)
let channel_user_DIVs = $(container).find('.nameDefault-1I0lx8, .nameHovered-28u_Fz')
let usernames = []
channel_user_DIVs.each(function (index, user_DIV) {
usernames.push($(user_DIV).text())
})
let voiceChannel = new VoiceChannel(channel_name, usernames, channel_is_selected)
voiceChannels.push(voiceChannel)
})
return voiceChannels
}
compareVoiceChannelLists(before_list, after_list) {
// Compare two lists of voice channels to find join/disconnect/move actions
// Collect all users of all channels from before and after
let combined_users = {}
before_list.forEach(function (voiceChannel_before) {
voiceChannel_before.users.forEach(function (username) {
if (!combined_users[username])
combined_users[username] = {}
combined_users[username].channel_before = voiceChannel_before.name
})
})
after_list.forEach(function (voiceChannel_after) {
voiceChannel_after.users.forEach(function (username) {
if (!combined_users[username])
combined_users[username] = {}
combined_users[username].channel_after = voiceChannel_after.name
})
})
// Assign channel before/after to users
let users = []
for (let name in combined_users) {
if (combined_users.hasOwnProperty(name)) {
let user = new User(name)
user.channel_before = combined_users[name].channel_before
user.channel_after = combined_users[name].channel_after
users.push(user)
}
}
// Recognize user join/disconnect/move actions
let events = []
users.forEach(function (user) {
if (user.channel_before !== user.channel_after) {
let event
if (!user.channel_before)
event = new ChannelEvent(user.name, 'joined', user.channel_after)
else if (!user.channel_after)
event = new ChannelEvent(user.name, 'disconnected', user.channel_before)
else
event = new ChannelEvent(user.name, 'moved', user.channel_before, user.channel_after)
events.push(event)
}
})
return events
}
playNotification(channelEvent) {
// Log and Speak the event with the selected message texts
let verbosity = 'short' // short/long // Select message text
let strip_shit = true // Only speak [a-z0-9_\ ] symbols
let ownChannel = this.ownChannel()
let ownUsername = this.ownUsername()
let is_local_action = (ownChannel === channelEvent.from) || (ownChannel === channelEvent.to)
let is_own_action = (channelEvent.username === ownUsername)
// Check if user joined/left/moved(from/to) own channel
if (is_local_action)
verbosity = 'local'
// Define default message texts
let messages = {
short: {
joined: '%u joined %c',
disconnected: '%u left %c',
moved: '%u moved from %f to %t',
},
local: {
joined: this.settings.message_join.value,
disconnected: this.settings.message_disconnect.value,
moved_in: this.settings.message_moved_in.value,
moved_out: this.settings.message_moved_out.value,
},
}
// Select message text
let msg = ''
if (channelEvent.joined)
msg = messages[verbosity].joined
if (channelEvent.disconnected)
msg = messages[verbosity].disconnected
if (channelEvent.moved) {
if (verbosity === 'local') {
if (channelEvent.from === ownChannel)
msg = messages[verbosity].moved_out
if (channelEvent.to === ownChannel)
msg = messages[verbosity].moved_in
} else {
msg = messages[verbosity].moved
}
}
if (channelEvent.disconnected)
msg = messages[verbosity].disconnected
// Format message with username and channel info
msg = msg.replace('%u', channelEvent.username)
msg = msg.replace('%c', channelEvent.channel)
msg = msg.replace('%f', channelEvent.from)
msg = msg.replace('%t', channelEvent.to)
// Log whats going on
console.log(A, msg)
if (strip_shit)
msg = msg.replace(/[^ a-zA-Z0-9]+/g, '')
// Dont speak when deafened
if (this.getDeafened())
return
// Dont mention own actions
if (is_own_action)
if (!debug)
return
console.log(A, 'Speech:', msg)
console.log(A, 'Is Local Action:', is_local_action)
// Only mention local channel actions [if option is set]
if (!is_local_action)
if (!debug)
return
// Speak message
this.speak(msg);
}
watch() {
// Check visible voice channels for events and log / speak them
let before = this.channelCache
let after = this.getVoiceChannels()
this.channelCache = after
console.log(A, after.length, 'Channels being watched: ' + after.map(channel => channel.name))
let events = this.compareVoiceChannelLists(before, after)
let self = this
events.forEach(function (event) {
self.playNotification(event)
})
}
speak(text) {
let speech = new window.SpeechSynthesisUtterance(text)
let voices = window.speechSynthesis.getVoices()
speech.volume = parseInt(this.settings.speech_volume.value) / 100 // Range 0-100 in settings
speech.rate = parseInt(this.settings.speech_rate.value) / 10 // Range 0-100 in settings
speech.pitch = parseInt(this.settings.speech_pitch.value) / 20 // Range 0-100 in settings
speech.voice = voices[0]
window.speechSynthesis.speak(speech);
}
}