Update NotificationSounds.plugin.js

This commit is contained in:
Mirco Wittrien 2020-03-10 15:58:09 +01:00
parent 86771a24ee
commit 9b1bf0dbed
1 changed files with 466 additions and 469 deletions

View File

@ -1,32 +1,12 @@
//META{"name":"NotificationSounds","authorId":"278543574059057154","invite":"Jx3TjNS","donate":"https://www.paypal.me/MircoWittrien","patreon":"https://www.patreon.com/MircoWittrien","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/NotificationSounds","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/NotificationSounds/NotificationSounds.plugin.js"}*//
class NotificationSounds {
getName () {return "NotificationSounds";}
var NotificationSounds = (_ => {
var audios, choices, firedEvents;
getVersion () {return "3.3.8";}
getAuthor () {return "DevilBro";}
getDescription () {return "Allows you to replace the native sounds of Discord with your own";}
constructor () {
this.changelog = {
"added":[["@here & @everyone","You can now set a unique sound for those two special mention cases"]],
"fixed":[["Incoming","Incoming call now works again"]],
"improved":[["New Library Structure & React","Restructured my Library and switched to React rendering instead of DOM manipulation"]]
};
this.patchedModules = {
after: {
Shakeable: "render"
}
};
}
initConstructor () {
const settingsaudio = new Audio();
/* NEVER CHANGE THE SRC LINKS IN THE PLUGIN FILE, TO ADD NEW SONGS ADD THEM IN THE SETTINGS GUI IN THE PLUGINS PAGE */
this.types = {
const types = {
"message1": {implemented:true, name:"New Chatmessage", src:"/assets/dd920c06a01e5bb8b09678581e29d56f.mp3", mute:true, focus:null, include:true},
"dm": {implemented:true, name:"Direct Message", src:"/assets/84c9fa3d07da865278bd77c97d952db4.mp3", mute:true, focus:true, include:false},
"mentioned": {implemented:true, name:"Mentioned", src:"/assets/a5f42064e8120e381528b14fd3188b72.mp3", mute:true, focus:true, include:false},
@ -65,6 +45,32 @@ class NotificationSounds {
"overlayunlock": {implemented:false, name:"Overlay Unlocked", src:"/assets/ad322ffe0a88436296158a80d5d11baa.mp3", mute:true, focus:null, include:true}
};
return class NotificationSounds {
getName () {return "NotificationSounds";}
getVersion () {return "3.3.9";}
getAuthor () {return "DevilBro";}
getDescription () {return "Allows you to replace the native sounds of Discord with your own";}
constructor () {
this.changelog = {
"fixed":[["@here & @everyone","No longer players unique message sound when @here & @everyone is suppressed in the channel"]]
};
this.patchedModules = {
after: {
Shakeable: "render"
}
};
}
initConstructor () {
audios = {};
choices = {};
firedEvents = {};
/* NEVER CHANGE THE SRC LINKS IN THE PLUGIN FILE, TO ADD NEW SONGS ADD THEM IN THE SETTINGS GUI IN THE PLUGINS PAGE */
this.defaults = {
"---": {
@ -82,18 +88,8 @@ class NotificationSounds {
"Discord": {}
};
for (let id in this.types) if (this.types[id].include) this.defaults.Discord[this.types[id].name] = this.types[id].src;
for (let id in types) if (types[id].include) this.defaults.Discord[types[id].name] = types[id].src;
this.defaults.Discord = BDFDB.ObjectUtils.sort(this.defaults.Discord);
this.orderTypes = {"category":true, "song":true};
this.settingsaudio = new Audio();
this.audios = {};
this.choices = {};
this.firedEvents = {};
}
getSettingsPanel (collapseStates = {}) {
@ -175,13 +171,13 @@ class NotificationSounds {
title: "Implemented Sounds",
collapseStates: collapseStates,
dividertop: true,
children: Object.keys(BDFDB.ObjectUtils.filter(this.types, typedata => typedata.implemented)).map(type => this.createSoundCard(type, settingspanel, collapseStates)).flat(10).filter(n => n)
children: Object.keys(BDFDB.ObjectUtils.filter(types, typedata => typedata.implemented)).map(type => this.createSoundCard(type, settingspanel, collapseStates)).flat(10).filter(n => n)
}));
settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
title: "Unimplemented Sounds",
collapseStates: collapseStates,
dividertop: true,
children: Object.keys(BDFDB.ObjectUtils.filter(this.types, typedata => !typedata.implemented)).map(type => this.createSoundCard(type, settingspanel, collapseStates)).flat(10).filter(n => n)
children: Object.keys(BDFDB.ObjectUtils.filter(types, typedata => !typedata.implemented)).map(type => this.createSoundCard(type, settingspanel, collapseStates)).flat(10).filter(n => n)
}));
settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
title: "Remove Songs",
@ -216,16 +212,16 @@ class NotificationSounds {
direction: BDFDB.LibraryComponents.Flex.Direction.HORIZONTAL,
children: [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsLabel, {
label: this.types[type].name
label: types[type].name
}),
this.types[type].focus != null ? BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
types[type].focus != null ? BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "Switch",
mini: true,
grow: 0,
label: "Mute when Channel focused:",
value: this.choices[type].focus,
value: choices[type].focus,
onChange: value => {
this.choices[type].focus = value;
choices[type].focus = value;
this.saveChoice(type, false);
}
}) : null,
@ -234,9 +230,9 @@ class NotificationSounds {
mini: true,
grow: 0,
label: "Mute in DnD:",
value: this.choices[type].mute,
value: choices[type].mute,
onChange: value => {
this.choices[type].mute = value;
choices[type].mute = value;
this.saveChoice(type, false);
}
})
@ -252,13 +248,13 @@ class NotificationSounds {
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormItem, {
title: "Category",
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Select, {
value: this.choices[type].category,
options: Object.keys(this.audios).map(name => {return {value:name, label:name}}),
value: choices[type].category,
options: Object.keys(audios).map(name => {return {value:name, label:name}}),
searchable: true,
onChange: category => {
this.choices[type].category = category.value;
this.choices[type].song = Object.keys(this.audios[category.value] || {})[0];
this.choices[type].src = this.audios[this.choices[type].category][this.choices[type].song] || this.types[type].src;
choices[type].category = category.value;
choices[type].song = Object.keys(audios[category.value] || {})[0];
choices[type].src = audios[choices[type].category][choices[type].song] || types[type].src;
this.saveChoice(type, true);
BDFDB.PluginUtils.refreshSettingsPanel(this, settingspanel.node, collapseStates);
}
@ -272,12 +268,12 @@ class NotificationSounds {
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormItem, {
title: "Song",
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Select, {
value: this.choices[type].song,
options: Object.keys(this.audios[this.choices[type].category] || {}).map(name => {return {value:name, label:name}}),
value: choices[type].song,
options: Object.keys(audios[choices[type].category] || {}).map(name => {return {value:name, label:name}}),
searchable: true,
onChange: song => {
this.choices[type].song = song.value;
this.choices[type].src = this.audios[this.choices[type].category][this.choices[type].song] || this.types[type].src;
choices[type].song = song.value;
choices[type].src = audios[choices[type].category][choices[type].song] || types[type].src;
this.saveChoice(type, true);
BDFDB.PluginUtils.refreshSettingsPanel(this, settingspanel.node, collapseStates);
}
@ -291,13 +287,13 @@ class NotificationSounds {
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormItem, {
title: "Volume",
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Slider, {
defaultValue: this.choices[type].volume,
defaultValue: choices[type].volume,
digits: 1,
onValueRender: value => {
return value + "%";
},
onValueChange: value => {
this.choices[type].volume = value;
choices[type].volume = value;
this.saveChoice(type, true);
}
})
@ -345,23 +341,23 @@ class NotificationSounds {
let message = e.methodArguments[0].message;
let guildId = message.guild_id || null;
if (!BDFDB.LibraryModules.MutedUtils.isGuildOrCategoryOrChannelMuted(guildId, message.channel_id) && message.author.id != BDFDB.UserUtils.me.id) {
if (!guildId && !(this.choices.dm.focus && document.hasFocus() && BDFDB.LibraryModules.LastChannelStore.getChannelId() == message.channel_id)) {
if (!guildId && !(choices.dm.focus && document.hasFocus() && BDFDB.LibraryModules.LastChannelStore.getChannelId() == message.channel_id)) {
this.fireEvent("dm");
this.playAudio("dm");
}
else if (message.mentions.length && !(this.choices.mentioned.focus && document.hasFocus() && BDFDB.LibraryModules.LastChannelStore.getChannelId() == message.channel_id)) {
else if (message.mentions.length && !(choices.mentioned.focus && document.hasFocus() && BDFDB.LibraryModules.LastChannelStore.getChannelId() == message.channel_id)) {
for (let mention of message.mentions) if (mention.id == BDFDB.UserUtils.me.id) {
this.fireEvent("mentioned");
this.playAudio("mentioned");
break;
}
}
else if (message.mention_everyone) {
if (message.content.indexOf("@everyone") > -1 && !(this.choices.everyone.focus && document.hasFocus() && BDFDB.LibraryModules.LastChannelStore.getChannelId() == message.channel_id)) {
else if (message.mention_everyone && !BDFDB.LibraryModules.MutedUtils.isSuppressEveryoneEnabled(guildId, message.channel_id)) {
if (message.content.indexOf("@everyone") > -1 && !(choices.everyone.focus && document.hasFocus() && BDFDB.LibraryModules.LastChannelStore.getChannelId() == message.channel_id)) {
this.fireEvent("everyone");
this.playAudio("everyone");
}
else if (message.content.indexOf("@here") > -1 && !(this.choices.here.focus && document.hasFocus() && BDFDB.LibraryModules.LastChannelStore.getChannelId() == message.channel_id)) {
else if (message.content.indexOf("@here") > -1 && !(choices.here.focus && document.hasFocus() && BDFDB.LibraryModules.LastChannelStore.getChannelId() == message.channel_id)) {
this.fireEvent("here");
this.playAudio("here");
}
@ -372,12 +368,12 @@ class NotificationSounds {
BDFDB.ModuleUtils.patch(this, BDFDB.LibraryModules.SoundUtils, "playSound", {instead: e => {
let type = e.methodArguments[0];
if (this.choices[type]) BDFDB.TimeUtils.timeout(_ => {
if (choices[type]) BDFDB.TimeUtils.timeout(_ => {
if (type == "message1") {
if (this.firedEvents["dm"]) this.firedEvents["dm"] = false;
else if (this.firedEvents["mentioned"]) this.firedEvents["mentioned"] = false;
else if (this.firedEvents["everyone"]) this.firedEvents["everyone"] = false;
else if (this.firedEvents["here"]) this.firedEvents["here"] = false;
if (firedEvents["dm"]) firedEvents["dm"] = false;
else if (firedEvents["mentioned"]) firedEvents["mentioned"] = false;
else if (firedEvents["everyone"]) firedEvents["everyone"] = false;
else if (firedEvents["here"]) firedEvents["here"] = false;
else this.playAudio(type);
}
else this.playAudio(type);
@ -387,8 +383,8 @@ class NotificationSounds {
BDFDB.ModuleUtils.patch(this, BDFDB.LibraryModules.SoundUtils, "createSound", {after: e => {
let type = e.methodArguments[0];
let audio = new Audio();
audio.src = this.choices[type].src;
audio.volume = this.choices[type].volume/100;
audio.src = choices[type].src;
audio.volume = choices[type].volume/100;
e.returnValue.play = _ => {
if (!audio.paused || this.dontPlayAudio(type)) return;
audio.loop = false;
@ -417,7 +413,7 @@ class NotificationSounds {
this.stopping = true;
BDFDB.PluginUtils.clear(this);
this.settingsaudio.pause();
settingsAudio.pause();
}
}
@ -429,7 +425,7 @@ class NotificationSounds {
delete this.SettingsUpdated;
this.repatchIncoming = true;
BDFDB.ModuleUtils.forceAllUpdates(this);
this.settingsaudio.pause();
settingsAudio.pause();
}
}
@ -449,22 +445,22 @@ class NotificationSounds {
successSavedAudio (settingspanel, collapseStates, data) {
BDFDB.NotificationUtils.toast(`Song ${data.song} was added to category ${data.category}.`, {type:"success"});
if (!this.audios[data.category]) this.audios[data.category] = {};
this.audios[data.category][data.song] = data.source;
BDFDB.DataUtils.save(this.audios, this, "audios");
if (!audios[data.category]) audios[data.category] = {};
audios[data.category][data.song] = data.source;
BDFDB.DataUtils.save(audios, this, "audios");
BDFDB.PluginUtils.refreshSettingsPanel(this, settingspanel, collapseStates);
}
loadAudios () {
this.audios = Object.assign({}, this.defaults, BDFDB.DataUtils.load(this, "audios"));
BDFDB.DataUtils.save(this.audios, this, "audios");
audios = Object.assign({}, this.defaults, BDFDB.DataUtils.load(this, "audios"));
BDFDB.DataUtils.save(audios, this, "audios");
}
loadChoices () {
for (let type in this.types) {
for (let type in types) {
let choice = BDFDB.DataUtils.load(this, "choices", type) || {}, songFound = false;
for (let category in this.audios) if (choice.category == category) for (var song in this.audios[category]) if (choice.song == song) {
for (let category in audios) if (choice.category == category) for (var song in audios[category]) if (choice.song == song) {
songFound = true;
break;
}
@ -472,21 +468,21 @@ class NotificationSounds {
category: "---",
song: "---",
volume: 100,
src: this.types[type].src,
mute: this.types[type].mute,
focus: this.types[type].focus
src: types[type].src,
mute: types[type].mute,
focus: types[type].focus
};
this.choices[type] = choice;
choices[type] = choice;
this.saveChoice(type, false);
}
}
saveChoice (type, play) {
if (!this.choices[type]) return;
BDFDB.DataUtils.save(this.choices[type], this, "choices", type);
if (!choices[type]) return;
BDFDB.DataUtils.save(choices[type], this, "choices", type);
if (play) {
this.SettingsUpdated = true;
this.playAudio(type, this.settingsaudio);
this.playAudio(type, settingsAudio);
}
}
@ -496,18 +492,19 @@ class NotificationSounds {
audio = new Audio();
}
else audio.pause();
audio.src = this.choices[type].src;
audio.volume = this.choices[type].volume/100;
audio.src = choices[type].src;
audio.volume = choices[type].volume/100;
audio.play();
}
dontPlayAudio (type) {
let status = BDFDB.UserUtils.getStatus();
return this.choices[type].mute && (status == "dnd" || status == "streaming");
return choices[type].mute && (status == "dnd" || status == "streaming");
}
fireEvent (type) {
this.firedEvents[type] = true;
BDFDB.TimeUtils.timeout(_ => {this.firedEvents[type] = false;},3000);
firedEvents[type] = true;
BDFDB.TimeUtils.timeout(_ => {firedEvents[type] = false;},3000);
}
}
})();