forked from JeanOUINA/BetterDiscordPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoiceChatNotifications.plugin.js
287 lines (198 loc) · 12.5 KB
/
VoiceChatNotifications.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
//META{"name":"VoiceChatNotifications"}*//
class VoiceChatNotifications {
constructor() {
this.defaultSettings = {
logConnections : true,
logMutes : true,
logDeafens : true,
logMoves : true,
logServerMuteDeaf : true,
displayWhileFocused : true,
displayUpdateNotes : true,
suppressInDnd : true
};
}
getName() { return "VoiceChatNotifications"; }
getDescription() { return "Displays notifications when users connect to/disconnect from, mute/unmute themselves, and deafen/undeafen themselves in the voice channel you're in. Press Alt + V to open the voice log."; }
getVersion() { return "2.2.4"; }
getAuthor() { return "Metalloriff"; }
getChanges() {
return {
"1.0.1" :
`
Redid the plugin entirely. It will now work when the server is not selected, like it should've in the first place.
`,
"1.1.1" :
`
Added a display notifications on user move setting and feature.
Added a suppress notifications in do not disturb setting.
Added a voice notification log. (Alt + V)
`,
"1.1.3" :
`
Changes are now logged while in DnD, without notifications, if suppressed.
Added a server mute and deafen setting.
`,
"1.2.3" :
`
Updated the plugin from Zere's lib to only depend on my lib.
The plugin name was changed. This will require you to re-enable the plugin.
`
};
}
load() {}
start() {
let libLoadedEvent = () => {
try{ this.onLibLoaded(); }
catch(err) { console.error(this.getName(), "fatal error, plugin could not be started!", err); }
};
let lib = document.getElementById("NeatoBurritoLibrary");
if(lib == undefined) {
lib = document.createElement("script");
lib.setAttribute("id", "NeatoBurritoLibrary");
lib.setAttribute("type", "text/javascript");
lib.setAttribute("src", "https://rawgit.com/Metalloriff/BetterDiscordPlugins/master/Lib/NeatoBurritoLibrary.js");
document.head.appendChild(lib);
}
if(typeof window.Metalloriff !== "undefined") libLoadedEvent();
else lib.addEventListener("load", libLoadedEvent);
}
getSettingsPanel() {
setTimeout(() => {
NeatoLib.Settings.pushElement(NeatoLib.Settings.Elements.createToggleGroup("vcn-toggles", "Settings", [
{ title : "Display notifications on user connect/disconnect", value : "logConnections", setValue : this.settings.logConnections },
{ title : "Display notificaitons on user mute/unmute", value : "logMutes", setValue : this.settings.logMutes },
{ title : "Display notifications on user deafen/undeafen", value : "logDeafens", setValue : this.settings.logDeafens },
{ title : "Display notifications on user move", value : "logMoves", setValue : this.settings.logMoves },
{ title : "Display notifications on user server mute/deafen", value : "logServerMuteDeaf", setValue : this.settings.logServerMuteDeaf },
{ title : "Display notifications while Discord is focused", value : "displayWhileFocused", setValue : this.settings.displayWhileFocused },
{ title : "Suppress notifications while in do not disturb", value : "suppressInDnd", setValue : this.settings.suppressInDnd }
], choice => {
this.settings[choice.value] = !this.settings[choice.value];
this.saveSettings();
}), this.getName());
NeatoLib.Settings.pushChangelogElements(this);
}, 0);
return `${NeatoLib.Settings.Elements.pluginNameLabel(this.getName())}`;
}
saveSettings() { NeatoLib.Settings.save(this); }
onLibLoaded() {
this.settings = NeatoLib.Settings.load(this, this.defaultSettings);
NeatoLib.Updates.check(this);
if(this.settings.displayUpdateNotes) NeatoLib.Changelog.compareVersions(this.getName(), this.getChanges());
this.log = [];
let getVoiceStates = NeatoLib.Modules.get(["getVoiceState"]).getVoiceStates,
getUser = NeatoLib.Modules.get(["getUser"]).getUser,
getChannel = NeatoLib.Modules.get(["getChannel"]).getChannel;
let lastStates = {};
let localUser = NeatoLib.getLocalUser();
this.update = setInterval(() => {
if(!this.settings.displayWhileFocused && this.focused) return;
let currentCall = NeatoLib.getSelectedVoiceChannel();
if(currentCall == undefined) return;
let newStates = getVoiceStates(currentCall.guild_id);
for(let id in newStates) {
if(localUser.id == id) continue;
if(lastStates[id] == undefined) {
if(!this.settings.logConnections) continue;
let user = getUser(id), channel = getChannel(newStates[id].channelId);
if(user && channel) {
if(!this.settings.suppressInDnd || NeatoLib.getLocalStatus() != "dnd") new Notification(`${user.username} joined ${channel.name}`, { silent : true, icon : user.getAvatarURL() });
this.log.push({ avatar : user.getAvatarURL(), username : user.username, timestamp : new Date().toLocaleTimeString(), text : `Joined ${channel.name}` });
}
} else {
if(this.settings.logMoves && lastStates[id].channelId != newStates[id].channelId) {
let user = getUser(id), channel = getChannel(newStates[id].channelId);
if(user && channel) {
if(!this.settings.suppressInDnd || NeatoLib.getLocalStatus() != "dnd") new Notification(`${user.username} moved to ${channel.name}`, { silent : true, icon : user.getAvatarURL() });
this.log.push({ avatar : user.getAvatarURL(), username : user.username, timestamp : new Date().toLocaleTimeString(), text : `Moved to ${channel.name}` });
}
continue;
}
if(this.settings.logServerMuteDeaf && lastStates[id].deaf != newStates[id].deaf) {
let user = getUser(id);
if(user) {
if(!this.settings.suppressInDnd || NeatoLib.getLocalStatus() != "dnd") new Notification(`${user.username} ${newStates[id].deaf ? "server deafened" : "server undeafened"}`, { silent : true, icon : user.getAvatarURL() });
this.log.push({ avatar : user.getAvatarURL(), username : user.username, timestamp : new Date().toLocaleTimeString(), text : newStates[id].deaf ? "Server deafened" : "Server undeafened" });
}
continue;
}
if(this.settings.logServerMuteDeaf && lastStates[id].mute != newStates[id].mute) {
let user = getUser(id);
if(user) {
if(!this.settings.suppressInDnd || NeatoLib.getLocalStatus() != "dnd") new Notification(`${user.username} ${newStates[id].mute ? "server muted" : "server unmuted"}`, { silent : true, icon : user.getAvatarURL() });
this.log.push({ avatar : user.getAvatarURL(), username : user.username, timestamp : new Date().toLocaleTimeString(), text : newStates[id].mute ? "Server muted" : "Server unmuted" });
}
continue;
}
if(this.settings.logDeafens && lastStates[id].selfDeaf != newStates[id].selfDeaf) {
let user = getUser(id);
if(user) {
if(!this.settings.suppressInDnd || NeatoLib.getLocalStatus() != "dnd") new Notification(`${user.username} ${newStates[id].selfDeaf ? "deafened" : "undeafened"}`, { silent : true, icon : user.getAvatarURL() });
this.log.push({ avatar : user.getAvatarURL(), username : user.username, timestamp : new Date().toLocaleTimeString(), text : newStates[id].selfDeaf ? "Deafened" : "Undeafened" });
}
continue;
}
if(this.settings.logMutes && lastStates[id].selfMute != newStates[id].selfMute) {
let user = getUser(id);
if(user) {
if(!this.settings.suppressInDnd || NeatoLib.getLocalStatus() != "dnd") new Notification(`${user.username} ${newStates[id].selfMute ? "muted" : "unmuted"}`, { silent : true, icon : user.getAvatarURL() });
this.log.push({ avatar : user.getAvatarURL(), username : user.username, timestamp : new Date().toLocaleTimeString(), text : newStates[id].selfMute ? "Muted" : "Unmuted" });
}
}
}
}
for(let id in lastStates) {
if(localUser.id == id || !this.settings.logConnections) continue;
if(newStates[id] == undefined && id != localUser.id) {
let user = getUser(id), channel = getChannel(lastStates[id].channelId);
if(user && channel) {
if(!this.settings.suppressInDnd || NeatoLib.getLocalStatus() != "dnd") new Notification(`${user.username} left ${channel.name}`, { silent : true, icon : user.getAvatarURL() });
this.log.push({ avatar : user.getAvatarURL(), username : user.username, timestamp : new Date().toLocaleTimeString(), text : `Left ${channel.name}` });
}
}
}
lastStates = newStates;
}, 500);
this.focused = true;
this.focus = () => this.focused = true;
this.unfocus = () => this.focused = false;
window.addEventListener("focus", this.focus);
window.addEventListener("blur", this.unfocus);
this.onKeyDown = e => {
if(e.altKey && e.key == "v") {
if(document.getElementById("vcn-log")) return;
let list = NeatoLib.UI.createBasicScrollList("vcn-log", "Voice Notification Log", { width : 400, height : 500 });
if(this.log.length > 50) this.log.splice(50, this.log.length);
for(let i = 0; i < this.log.length; i++) {
list.scroller.insertAdjacentHTML("afterbegin", `
<div class="message-group hide-overflow">
<div class="avatar-large stop-animation" style="background-image: url(${this.log[i].avatar});"></div>
<div class="comment">
<div class=NeatoLib.Modules.get("message").message.split(" ").join("")>
<div class="body">
<h2 class="old-h2"><span class="username-wrapper"><strong class="user-name" style="color: white">${this.log[i].username}</strong></span><span class="highlight-separator"> - </span><span class="timestamp">${this.log[i].timestamp}</span></h2>
<div class="message-text">
<div class=NeatoLib.Modules.get("markup").markup.split(" ").join("")>${this.log[i].text}.</div>
</div>
</div>
</div>
</div>
</div>
`);
}
}
};
document.addEventListener("keydown", this.onKeyDown);
NeatoLib.Events.onPluginLoaded(this);
}
stop() {
clearInterval(this.update);
if(this.focus && this.unfocus) {
window.removeEventListener("focus", this.focus);
window.removeEventListener("blur", this.unfocus);
}
if(this.onKeyDown) document.removeEventListener("keydown", this.onKeyDown);
this.ready = false;
}
}