//META{"name":"ChatAliases"}*//
class ChatAliases {
initConstructor () {
this.configs = ["case","exact","autoc","regex","file"];
this.defaults = {
settings: {
addAutoComplete: {value:true, description:"Add an Autocomplete-Menu for Non-Regex Aliases:"}
}
};
}
getName () {return "ChatAliases";}
getDescription () {return "Allows the user to configure their own chat-aliases which will automatically be replaced before the message is being sent.";}
getVersion () {return "1.9.2";}
getAuthor () {return "DevilBro";}
getSettingsPanel () {
if (!this.started || typeof BDFDB !== "object") return;
var settings = BDFDB.getAllData(this, "settings");
var settingshtml = `
${this.getName()}
`;
for (let key in settings) {
settingshtml += `
${this.defaults.settings[key].description}
`;
}
settingshtml += `
Replace:
With:
`;
settingshtml += `
List of Chataliases:
`;
for (let config of this.configs) {
settingshtml += `
${config.toUpperCase()}
`;
}
settingshtml += `
`;
for (let word in this.aliases) {
settingshtml += `
`;
for (let config of this.configs) {
settingshtml += ``;
console.log();
}
settingshtml += `
`;
}
settingshtml += `
`;
settingshtml += `
Remove all added words.
`;
var infoHidden = BDFDB.loadData("hideInfo", this, "hideInfo");
settingshtml += `
Information
`;
settingshtml += `
Case: Will replace words while comparing lowercase/uppercase. apple => apple, not APPLE or AppLe
Not Case: Will replace words while ignoring lowercase/uppercase. apple => apple, APPLE and AppLe
Exact: Will replace words that are exactly the replaceword. apple to pear => applepie stays applepie
Not Exact: Will replace words anywhere they appear. apple to pear => applepieapple to pearpiepear
Autoc: Will appear in the Autocomplete Menu (if enabled).
Regex: Will treat the entered wordvalue as a regular expression. Help
File: If the replacevalue is a filepath it will try to upload the file located at the filepath.
`;
settingshtml += `
`;
var settingspanel = $(settingshtml)[0];
BDFDB.initElements(settingspanel);
$(settingspanel)
.on("click", BDFDB.dotCN.switchinner, () => {this.updateSettings(settingspanel);})
.on("keypress", ".wordInputs", (e) => {if (e.which == 13) this.updateContainer(settingspanel, e.currentTarget);})
.on("keyup", BDFDB.dotCN.gamenameinput, (e) => {this.updateWord(e.currentTarget);})
.on("click", ".btn-addword, .remove-word, .remove-all", (e) => {this.updateContainer(settingspanel, e.currentTarget);})
.on("click", BDFDB.dotCN.checkboxinput, (e) => {this.updateConfig(e.currentTarget);})
.on("click", ".toggle-info", (e) => {this.toggleInfo(settingspanel, e.currentTarget);});
return settingspanel;
}
//legacy
load () {}
start () {
var libraryScript = null;
if (typeof BDFDB !== "object" || typeof BDFDB.isLibraryOutdated !== "function" || BDFDB.isLibraryOutdated()) {
libraryScript = document.querySelector('head script[src="https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"]');
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js");
document.head.appendChild(libraryScript);
}
this.startTimeout = setTimeout(() => {this.initialize();}, 30000);
if (typeof BDFDB === "object" && typeof BDFDB.isLibraryOutdated === "function") this.initialize();
else libraryScript.addEventListener("load", () => {this.initialize();});
}
initialize () {
if (typeof BDFDB === "object") {
BDFDB.loadMessage(this);
this.UploadModule = BDFDB.WebModules.findByProperties("instantBatchUpload");
this.CurrentUserPerms = BDFDB.WebModules.findByProperties("getChannelPermissions", "can");
this.Permissions = BDFDB.WebModules.findByProperties("Permissions", "ActivityTypes").Permissions;
var observer = null;
observer = new MutationObserver((changes, _) => {
changes.forEach(
(change, i) => {
if (change.removedNodes) {
change.removedNodes.forEach((node) => {
if (node.tagName && node.getAttribute("layer-id") == "user-settings") {
document.querySelectorAll("textarea" + BDFDB.dotCN.textarea).forEach(textarea => {this.bindEventToTextArea(textarea);});
}
});
}
}
);
});
BDFDB.addObserver(this, BDFDB.dotCN.layers, {name:"settingsWindowObserver",instance:observer}, {childList:true});
observer = new MutationObserver((changes, _) => {
changes.forEach(
(change, i) => {
if (change.addedNodes) {
change.addedNodes.forEach((node) => {
if (node.tagName && node.querySelector(BDFDB.dotCN.textareainner + ":not(" + BDFDB.dotCN.textareainnerdisabled + ")")) {
this.bindEventToTextArea(node.querySelector("textarea"));
}
});
}
}
);
});
BDFDB.addObserver(this, BDFDB.dotCN.appmount, {name:"textareaObserver",instance:observer}, {childList: true, subtree:true});
this.aliases = BDFDB.loadAllData(this, "words");
document.querySelectorAll("textarea" + BDFDB.dotCN.textarea).forEach(textarea => {this.bindEventToTextArea(textarea);});
$(document).off("click." + this.getName()).on("click." + this.getName(), (e) => {
if (!e.target.tagName === "TEXTAREA") $(".autocompleteAliases, .autocompleteAliasesRow").remove();
});
}
else {
console.error(this.getName() + ": Fatal Error: Could not load BD functions!");
}
}
stop () {
if (typeof BDFDB === "object") {
$(".autocompleteAliases, .autocompleteAliasesRow").remove();
BDFDB.unloadMessage(this);
}
}
// begin of own functions
updateSettings (settingspanel) {
var settings = {};
for (var input of settingspanel.querySelectorAll(BDFDB.dotCN.switchinner)) {
settings[input.value] = input.checked;
}
BDFDB.saveAllData(settings, this, "settings");
document.querySelectorAll("textarea" + BDFDB.dotCN.textarea).forEach(textarea => {this.bindEventToTextArea(textarea);});
}
updateContainer (settingspanel, ele) {
var update = false, wordvalue = null, replacevalue = null;
var action = ele.getAttribute("action");
if (action == "add") {
var wordinput = settingspanel.querySelector("#input-wordvalue");
var replaceinput = settingspanel.querySelector("#input-replacevalue");
var fileselection = settingspanel.querySelector("#input-file");
wordvalue = wordinput.value;
replacevalue = replaceinput.value;
if (wordvalue && wordvalue.trim().length > 0 && replacevalue && replacevalue.trim().length > 0) {
wordvalue = wordvalue.trim();
replacevalue = replacevalue.trim();
var filedata = null;
var fs = require("fs");
if (fileselection.files && fileselection.files[0] && fs.existsSync(replacevalue)) {
filedata = JSON.stringify({
data: fs.readFileSync(replacevalue).toString("base64"),
name: fileselection.files[0].name,
type: fileselection.files[0].type
});
}
this.aliases[wordvalue] = {
replace: replacevalue,
filedata: filedata,
case: false,
exact: wordvalue.indexOf(" ") == -1,
autoc: true,
regex: false,
file: filedata != null
};
wordinput.value = null;
replaceinput.value = null;
update = true;
}
}
else if (action == "remove") {
wordvalue = ele.getAttribute("word");
if (wordvalue) {
delete this.aliases[wordvalue];
update = true;
}
}
else if (action == "removeall") {
if (confirm("Are you sure you want to remove all added Words from your list?")) {
this.aliases = {};
update = true;
}
}
if (update) {
BDFDB.saveAllData(this.aliases, this, "words");
var containerhtml = ``;
for (let word in this.aliases) {
containerhtml += `
`;
for (let config of this.configs) {
containerhtml += ``;
}
containerhtml += `
`;
}
$(settingspanel).find(".alias-list").html(containerhtml);
BDFDB.initElements(settingspanel);
}
}
updateWord (ele) {
clearTimeout(ele.updateTimeout);
ele.updateTimeout = setTimeout(() => {
var card = ele.parentElement.parentElement;
var oldwordvalue = ele.getAttribute("word");
if (oldwordvalue && this.aliases[oldwordvalue]) {
var wordinput = card.querySelector(".word-name");
var replaceinput = card.querySelector(".replace-name");
var removebutton = card.querySelector(".remove-word");
var newwordvalue = wordinput.value;
var newreplacevalue = replaceinput.value;
wordinput.setAttribute("word", newwordvalue);
wordinput.setAttribute("value", newwordvalue);
replaceinput.setAttribute("word", newwordvalue);
replaceinput.setAttribute("value", newreplacevalue);
removebutton.setAttribute("word", newwordvalue);
this.aliases[newwordvalue] = this.aliases[oldwordvalue];
this.aliases[newwordvalue].replace = newreplacevalue;
if (newwordvalue != oldwordvalue) delete this.aliases[oldwordvalue];
BDFDB.saveAllData(this.aliases, this, "words");
}
},500);
}
updateConfig (ele) {
var wordvalue = ele.getAttribute("word");
var config = ele.getAttribute("config");
if (wordvalue && this.aliases[wordvalue] && config) {
this.aliases[wordvalue][config] = ele.checked;
BDFDB.saveAllData(this.aliases, this, "words");
}
}
toggleInfo (settingspanel, ele) {
ele.classList.toggle(BDFDB.disCN.categorywrappercollapsed);
ele.classList.toggle(BDFDB.disCN.categorywrapperdefault);
var svg = ele.querySelector(BDFDB.dotCN.categoryicontransition);
svg.classList.toggle(BDFDB.disCN.directionright);
svg.classList.toggle(BDFDB.disCN.categoryiconcollapsed);
svg.classList.toggle(BDFDB.disCN.categoryicondefault);
var visible = $(settingspanel).find(".info-container").is(":visible");
$(settingspanel).find(".info-container").toggle(!visible);
BDFDB.saveData("hideInfo", visible, this, "hideInfo");
}
bindEventToTextArea (textarea) {
if (!textarea) return;
var channelObj = BDFDB.getSelectedChannel();
var channel = channelObj ? channelObj.data : null;
if (!channel) return;
var settings = BDFDB.getAllData(this, "settings");
$(textarea)
.off("input." + this.getName())
.on("input." + this.getName(), () => {
if (this.format) {
this.format = false;
textarea.focus();
textarea.selectionStart = 0;
textarea.selectionEnd = textarea.value.length;
if (document.activeElement == textarea) {
var messageInput = this.formatText(textarea.value);
if (messageInput && messageInput.text != null) {
document.execCommand("insertText", false, messageInput.text ? messageInput.text + " " : "");
}
if (messageInput && messageInput.files.length > 0 && (channel.type == 1 || this.CurrentUserPerms.can(this.Permissions.ATTACH_FILES, channel))) {
this.UploadModule.instantBatchUpload(channel.id, messageInput.files);
}
}
}
})
.off("keydown." + this.getName())
.on("keydown." + this.getName(), e => {
let autocompletemenu = textarea.parentElement.querySelector(BDFDB.dotCN.autocomplete);
if ((e.which == 9 || e.which == 13) && autocompletemenu) {
if (autocompletemenu.querySelector(BDFDB.dotCN.autocompleteselected).parentElement.classList.contains("autocompleteAliasesRow")) {
e.preventDefault();
e.stopPropagation();
this.swapWordWithAlias(textarea);
}
}
else if ((e.which == 38 || e.which == 40) && autocompletemenu) {
let autocompleteitems = autocompletemenu.querySelectorAll(BDFDB.dotCN.autocompleteselectable + ":not(.autocompleteAliasesSelector)");
let selected = autocompletemenu.querySelector(BDFDB.dotCN.autocompleteselected);
if (selected.classList.contains("autocompleteAliasesSelector") || autocompleteitems[e.which == 38 ? 0 : (autocompleteitems.length-1)] == selected) {
e.preventDefault();
e.stopPropagation();
let next = this.getNextSelection(autocompletemenu, null, e.which == 38 ? false : true);
selected.classList.remove(BDFDB.disCN.autocompleteselected);
next.classList.add(BDFDB.disCN.autocompleteselected);
if (!next.classList.contains("autocompleteAliasesSelector")) {
// if next element is a default discord autocomplete item, trigger the keypress again so the item is internally selected
var press = new KeyboardEvent("keypress", e);
Object.defineProperty(press, "keyCode", {value: e.which});
Object.defineProperty(press, "which", {value: e.which});
textarea.dispatchEvent(press);
}
}
}
else if (textarea.value && !e.shiftKey && e.which == 13 && !autocompletemenu && textarea.value.indexOf("s/") != 0) {
this.format = true;
$(textarea).trigger("input");
}
else if (!e.ctrlKey && settings.addAutoComplete && textarea.selectionStart == textarea.selectionEnd && textarea.selectionEnd == textarea.value.length) {
clearTimeout(textarea.chataliastimeout);
textarea.chataliastimeout = setTimeout(() => {this.addAutoCompleteMenu(textarea);},100);
}
if (!e.ctrlKey && e.which != 38 && e.which != 40) {
if (!(e.which == 39 && textarea.selectionStart == textarea.selectionEnd && textarea.selectionEnd == textarea.value.length)) {
$(".autocompleteAliases, .autocompleteAliasesRow").remove();
}
}
})
.off("click." + this.getName())
.on("click." + this.getName(), e => {
if (settings.addAutoComplete && textarea.selectionStart == textarea.selectionEnd && textarea.selectionEnd == textarea.value.length) {
setImmediate(() => {this.addAutoCompleteMenu(textarea);});
}
});
}
addAutoCompleteMenu (textarea) {
if (textarea.parentElement.querySelector(".autocompleteAliasesRow")) return;
let words = textarea.value.split(" ");
let lastword = words[words.length-1].trim();
if (words.length == 1 && BDFDB.isPluginEnabled("WriteUpperCase")) {
let first = lastword.charAt(0);
if (first === first.toUpperCase() && lastword.toLowerCase().indexOf("http") == 0) {
lastword = lastword.charAt(0).toLowerCase() + lastword.slice(1);
}
else if (first === first.toLowerCase() && first !== first.toUpperCase() && lastword.toLowerCase().indexOf("http") != 0) {
lastword = lastword.charAt(0).toUpperCase() + lastword.slice(1);
}
}
if (lastword) {
let matchedaliases = {};
for (let word in this.aliases) {
let aliasdata = this.aliases[word];
if (!aliasdata.regex && aliasdata.autoc) {
if (aliasdata.exact) {
if (aliasdata.case && word.indexOf(lastword) == 0) matchedaliases[word] = aliasdata;
else if (!aliasdata.case && word.toLowerCase().indexOf(lastword.toLowerCase()) == 0) matchedaliases[word] = aliasdata;
}
else {
if (aliasdata.case && word.indexOf(lastword) > -1) matchedaliases[word] = aliasdata;
else if (!aliasdata.case && word.toLowerCase().indexOf(lastword.toLowerCase()) > -1) matchedaliases[word] = aliasdata;
}
}
}
if (!BDFDB.isObjectEmpty(matchedaliases)) {
let autocompletemenu = textarea.parentElement.querySelector(BDFDB.dotCNS.autocomplete + BDFDB.dotCN.autocompleteinner), amount = 15;
if (!autocompletemenu) {
autocompletemenu = $(`