Update NotificationSounds.plugin.js

This commit is contained in:
Mirco Wittrien 2022-12-08 19:09:01 +01:00
parent cefe1260f2
commit 1dbd92f67e
1 changed files with 46 additions and 34 deletions

View File

@ -2,7 +2,7 @@
* @name NotificationSounds * @name NotificationSounds
* @author DevilBro * @author DevilBro
* @authorId 278543574059057154 * @authorId 278543574059057154
* @version 3.7.5 * @version 3.7.6
* @description Allows you to replace the native Sounds with custom Sounds * @description Allows you to replace the native Sounds with custom Sounds
* @invite Jx3TjNS * @invite Jx3TjNS
* @donate https://www.paypal.me/MircoWittrien * @donate https://www.paypal.me/MircoWittrien
@ -85,9 +85,9 @@ module.exports = (_ => {
const WebAudioSound = class WebAudioSound { const WebAudioSound = class WebAudioSound {
constructor (type) { constructor (type) {
this._name = type; this.name = type;
this._src = audios[choices[type].category][choices[type].sound] || types[type].src; this._src = audios[choices[type].category][choices[type].sound] || types[type].src;
this._volume = choices[type].volume; this._volume = choices[type].volume / 100;
} }
loop () { loop () {
this._ensureAudio().then(audio => { this._ensureAudio().then(audio => {
@ -133,7 +133,7 @@ module.exports = (_ => {
let audio = new Audio; let audio = new Audio;
audio.src = this._src && this._src.startsWith("data") ? this._src.replace(/ /g, "") : this._src; audio.src = this._src && this._src.startsWith("data") ? this._src.replace(/ /g, "") : this._src;
audio.onloadeddata = _ => { audio.onloadeddata = _ => {
audio.volume = Math.min((BDFDB.LibraryStores.MediaEngineStore.getOutputVolume() / 100) * (this._volume / 100) * (volumes.globalVolume / 100), 1); audio.volume = Math.min((BDFDB.LibraryStores.MediaEngineStore.getOutputVolume() / 100) * this._volume * (volumes.globalVolume / 100), 1);
BDFDB.DiscordUtils.isPlaformEmbedded() && audio.setSinkId(currentDevice || defaultDevice); BDFDB.DiscordUtils.isPlaformEmbedded() && audio.setSinkId(currentDevice || defaultDevice);
callback(audio); callback(audio);
}; };
@ -280,34 +280,46 @@ module.exports = (_ => {
let soundObjIndex = Array.from(e.methodArguments).findIndex(n => n && n.sound); let soundObjIndex = Array.from(e.methodArguments).findIndex(n => n && n.sound);
if (soundObjIndex && e.methodArguments[soundObjIndex].sound.includes("message")) e.methodArguments[soundObjIndex].sound = null; if (soundObjIndex && e.methodArguments[soundObjIndex].sound.includes("message")) e.methodArguments[soundObjIndex].sound = null;
}}); }});
BDFDB.PatchUtils.patch(this, BDFDB.LibraryModules.SoundUtils, "playSound", {instead: e => { if (BDFDB.LibraryModules.SoundUtils && BDFDB.LibraryModules.SoundUtils.createSound) {
let type = e.methodArguments[0]; let cancel = BDFDB.PatchUtils.patch(this, BDFDB.LibraryModules.SoundUtils, "createSound", {after: e => {
if (type && choices[type]) { if (e.returnValue && e.returnValue.constructor && e.returnValue.constructor.prototype && typeof e.returnValue.constructor.prototype.play == "function") {
e.stopOriginalMethodCall(); cancel();
BDFDB.TimeUtils.timeout(_ => { BDFDB.PatchUtils.patch(this, e.returnValue.constructor.prototype, ["play", "loop"], {instead: e2 => {
if (type == "message1") { let type = e2.instance && e2.instance.name;
let called = false; if (type && choices[type]) {
for (let subType of [type].concat(Object.keys(message1Types))) if (firedEvents[subType]) { let loop = e2.originalMethodName == "loop";
delete firedEvents[subType]; e2.stopOriginalMethodCall();
called = true; BDFDB.TimeUtils.timeout(_ => {
break; if (type == "message1") {
let called = false;
for (let subType of [type].concat(Object.keys(message1Types))) if (firedEvents[subType]) {
delete firedEvents[subType];
called = true;
break;
}
if (!called) this.playAudio(type, loop);
}
else this.playAudio(type, loop);
});
} }
if (!called) this.playAudio(type); else e2.callOriginalMethodAfterwards();
} }});
else this.playAudio(type); BDFDB.PatchUtils.patch(this, e.returnValue.constructor.prototype, "stop", {after: e2 => {
}); let type = e2.instance && e2.instance.name;
} if (type && createdAudios[type]) createdAudios[type].stop();
else e.callOriginalMethodAfterwards(); }});
}}); }
BDFDB.PatchUtils.patch(this, BDFDB.LibraryModules.SoundUtils, ["createSound", "createSoundpackSound"], {after: e => { return;
let type = e.methodArguments[0]; let type = e.methodArguments[0];
if (type && choices[type]) { if (type && choices[type]) {
let audio = new WebAudioSound(type); let audio = new WebAudioSound(type);
createdAudios[type] = audio; createdAudios[type] = audio;
return audio; return audio;
} }
else BDFDB.LogUtils.warn(`Could not create Sound for "${type}".`, this); else BDFDB.LogUtils.warn(`Could not create Sound for "${type}".`, this);
}}); }}, {noCache: true});
BDFDB.LibraryModules.SoundUtils.createSound("call_calling");
}
this.loadAudios(); this.loadAudios();
this.loadChoices(); this.loadChoices();
@ -661,7 +673,7 @@ module.exports = (_ => {
forceUpdateAll () { forceUpdateAll () {
volumes = BDFDB.DataUtils.get(this, "volumes"); volumes = BDFDB.DataUtils.get(this, "volumes");
if (BDFDB.LibraryStores.SoundpackStore) BDFDB.LibraryStores.SoundpackStore.emitChange();
BDFDB.PatchUtils.forceAllUpdates(this); BDFDB.PatchUtils.forceAllUpdates(this);
BDFDB.DiscordUtils.rerenderAll(); BDFDB.DiscordUtils.rerenderAll();
} }
@ -700,11 +712,11 @@ module.exports = (_ => {
} }
} }
playAudio (type) { playAudio (type, loop = false) {
if (this.dontPlayAudio(type) || BDFDB.LibraryStores.StreamerModeStore.disableSounds) return; if (this.dontPlayAudio(type) || BDFDB.LibraryStores.StreamerModeStore.disableSounds) return;
if (createdAudios[type]) createdAudios[type].stop(); if (createdAudios[type]) createdAudios[type].stop();
createdAudios[type] = new WebAudioSound(type); createdAudios[type] = new WebAudioSound(type);
createdAudios[type].play(); createdAudios[type][loop ? "loop" : "play"]();
} }
isSuppressMentionsEnabled (guildId, channelId) { isSuppressMentionsEnabled (guildId, channelId) {