BetterDiscordAddons/Plugins/ChatAliases/ChatAliases.plugin.js

599 lines
27 KiB
JavaScript
Raw Normal View History

2020-10-20 23:25:34 +02:00
/**
* @name ChatAliases
* @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/ChatAliases
* @source https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/ChatAliases/ChatAliases.plugin.js
* @updateUrl https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/ChatAliases/ChatAliases.plugin.js
2020-10-20 23:25:34 +02:00
*/
2018-10-11 10:21:26 +02:00
2020-09-19 20:49:33 +02:00
module.exports = (_ => {
2020-10-09 21:09:35 +02:00
const config = {
2020-09-19 20:49:33 +02:00
"info": {
"name": "ChatAliases",
"author": "DevilBro",
2021-01-26 21:40:18 +01:00
"version": "2.2.4",
2021-03-04 11:31:05 +01:00
"description": "Allows you to configure your own Aliases/Commands"
2020-09-26 18:18:52 +02:00
},
"changeLog": {
2021-01-29 20:57:25 +01:00
"improved": {
"Canary Changes": "Preparing Plugins for the changes that are already done on Discord Canary"
2020-09-26 18:18:52 +02:00
}
2020-02-11 12:58:16 +01:00
}
2020-09-19 20:49:33 +02:00
};
2020-11-13 19:47:44 +01:00
2020-10-09 21:09:35 +02:00
return !window.BDFDB_Global || (!window.BDFDB_Global.loaded && !window.BDFDB_Global.started) ? class {
2021-01-06 12:38:36 +01:00
getName () {return config.info.name;}
getAuthor () {return config.info.author;}
getVersion () {return config.info.version;}
2021-02-01 17:13:13 +01:00
getDescription () {return `The Library Plugin needed for ${config.info.name} is missing. Open the Plugin Settings to download it. \n\n${config.info.description}`;}
downloadLibrary () {
require("request").get("https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js", (e, r, b) => {
2021-02-02 11:22:26 +01:00
if (!e && b && b.indexOf(`* @name BDFDB`) > -1) require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0BDFDB.plugin.js"), b, _ => BdApi.showToast("Finished downloading BDFDB Library", {type: "success"}));
2021-02-01 17:13:13 +01:00
else BdApi.alert("Error", "Could not download BDFDB Library Plugin, try again later or download it manually from GitHub: https://github.com/mwittrien/BetterDiscordAddons/tree/master/Library/");
});
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
load () {
2020-11-19 16:51:14 +01:00
if (!window.BDFDB_Global || !Array.isArray(window.BDFDB_Global.pluginQueue)) window.BDFDB_Global = Object.assign({}, window.BDFDB_Global, {pluginQueue: []});
2020-09-19 20:49:33 +02:00
if (!window.BDFDB_Global.downloadModal) {
window.BDFDB_Global.downloadModal = true;
2021-01-14 16:14:44 +01:00
BdApi.showConfirmationModal("Library Missing", `The Library Plugin needed for ${config.info.name} is missing. Please click "Download Now" to install it.`, {
2020-09-19 20:49:33 +02:00
confirmText: "Download Now",
cancelText: "Cancel",
onCancel: _ => {delete window.BDFDB_Global.downloadModal;},
2020-09-20 08:15:13 +02:00
onConfirm: _ => {
delete window.BDFDB_Global.downloadModal;
2021-02-01 17:13:13 +01:00
this.downloadLibrary();
2020-09-20 08:15:13 +02:00
}
2020-09-19 20:49:33 +02:00
});
}
if (!window.BDFDB_Global.pluginQueue.includes(config.info.name)) window.BDFDB_Global.pluginQueue.push(config.info.name);
2020-10-09 21:09:35 +02:00
}
2021-01-06 12:38:36 +01:00
start () {this.load();}
stop () {}
getSettingsPanel () {
2020-11-28 23:12:09 +01:00
let template = document.createElement("template");
2021-01-14 16:14:44 +01:00
template.innerHTML = `<div style="color: var(--header-primary); font-size: 16px; font-weight: 300; white-space: pre; line-height: 22px;">The Library Plugin needed for ${config.info.name} is missing.\nPlease click <a style="font-weight: 500;">Download Now</a> to install it.</div>`;
2021-02-01 17:13:13 +01:00
template.content.firstElementChild.querySelector("a").addEventListener("click", this.downloadLibrary);
2020-11-28 23:12:09 +01:00
return template.content.firstElementChild;
}
2020-10-09 21:09:35 +02:00
} : (([Plugin, BDFDB]) => {
2020-12-11 17:26:39 +01:00
var settings = {}, amounts = {}, configs = {}, aliases = {}, commandAliases = {}, commandSentinel;
2020-09-19 20:49:33 +02:00
2020-10-09 21:09:35 +02:00
return class ChatAliases extends Plugin {
2021-01-06 12:38:36 +01:00
onLoad () {
2020-09-19 20:49:33 +02:00
this.defaults = {
configs: {
2021-01-26 21:40:18 +01:00
case: {value: false, description: "Handle the Word Value case sensitive"},
exact: {value: true, description: "Handle the Word Value as an exact Word and not as part of a Word"},
autoc: {value: true, description: "Add this Alias in the Autocomplete Menu (not for RegExp)"},
regex: {value: false, description: "Handle the Word Value as a RegExp String"},
file: {value: false, description: "Handle the Replacement Value as a File Path"}
2020-02-11 12:58:16 +01:00
},
2020-09-19 20:49:33 +02:00
settings: {
2021-01-26 21:40:18 +01:00
replaceBeforeSend: {value: true, inner: false, description: "Replace Words with your Aliases before a Message is sent"},
addContextMenu: {value: true, inner: false, description: "Add a Context Menu Entry to faster add new Aliases"},
addAutoComplete: {value: true, inner: false, description: "Add an Autocomplete Menu for non-RegExp Aliases"},
2020-11-19 16:51:14 +01:00
triggerNormal: {value: true, inner: true, description: "Normal Message Textarea"},
triggerEdit: {value: true, inner: true, description: "Edit Message Textarea"},
triggerUpload: {value: true, inner: true, description: "Upload Message Prompt"}
2020-02-11 12:58:16 +01:00
},
2020-09-19 20:49:33 +02:00
amounts: {
2021-01-26 21:40:18 +01:00
minAliasLength: {value: 2, min: 1, description: "Minimal Character Length to open Autocomplete Menu: "}
2020-09-19 20:49:33 +02:00
}
};
this.patchedModules = {
before: {
ChannelTextAreaForm: "render",
MessageEditor: "render",
Upload: "render"
}
};
this.css = `
${BDFDB.dotCN.autocompleteicon} {
flex: 0 0 auto;
}
`;
2020-02-11 12:58:16 +01:00
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
onStart () {
2020-06-08 20:07:08 +02:00
aliases = BDFDB.DataUtils.load(this, "words");
2020-12-11 17:26:39 +01:00
commandSentinel = BDFDB.LibraryModules.AutocompleteSentinels && BDFDB.LibraryModules.AutocompleteSentinels.COMMAND_SENTINEL || "/";
commandAliases = BDFDB.ObjectUtils.filter(aliases, key => key.startsWith(commandSentinel), true);
2020-09-26 18:18:52 +02:00
if (BDFDB.LibraryModules.AutocompleteOptions && BDFDB.LibraryModules.AutocompleteOptions.AUTOCOMPLETE_OPTIONS) {
BDFDB.LibraryModules.AutocompleteOptions.AUTOCOMPLETE_OPTIONS.ALIASES = {
autoSelect: true,
getPlainText: (eventOrIndex, config, autocompletes) => {
let aliasData = eventOrIndex._targetInst ? eventOrIndex._targetInst.memoizedProps.alias : typeof eventOrIndex == "number" && autocompletes.aliases[eventOrIndex];
return aliasData.word;
},
getRawText: (eventOrIndex, config, autocompletes) => {
let aliasData = eventOrIndex._targetInst ? eventOrIndex._targetInst.memoizedProps.alias : typeof eventOrIndex == "number" && autocompletes.aliases[eventOrIndex];
return aliasData.file ? aliasData.word : BDFDB.StringUtils.insertNRST(aliasData.replace);
},
2020-12-11 17:26:39 +01:00
getSentinel: _ => {
2020-09-26 18:18:52 +02:00
return "";
},
matches: (channel, what, wordLowercase, what2, config, rawValue) => {
let currentLastWord = BDFDB.StringUtils.findMatchCaseless(wordLowercase, rawValue, true);
if (currentLastWord.length >= amounts.minAliasLength) for (let word in aliases) {
let aliasData = aliases[word];
if (!aliasData.regex && aliasData.autoc) {
if (aliasData.exact) {
if (aliasData.case && word.indexOf(currentLastWord) == 0) return true;
else if (!aliasData.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) == 0) return true;
}
else {
if (aliasData.case && word.indexOf(currentLastWord) > -1) return true;
else if (!aliasData.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) > -1) return true;
}
}
}
return false;
},
queryResults: (channel, wordLowercase, config, rawValue) => {
2021-01-08 21:25:40 +01:00
if (rawValue == commandSentinel) return;
2020-09-26 18:18:52 +02:00
let currentLastWord = BDFDB.StringUtils.findMatchCaseless(wordLowercase, rawValue, true);
let matches = [];
for (let word in aliases) {
if (matches.length >= BDFDB.DiscordConstants.MAX_AUTOCOMPLETE_RESULTS) break;
let aliasData = Object.assign({word}, aliases[word]);
if (!aliasData.regex && aliasData.autoc) {
if (aliasData.exact) {
if (aliasData.case && word.indexOf(currentLastWord) == 0) matches.push(aliasData);
else if (!aliasData.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) == 0) matches.push(aliasData);
}
else {
if (aliasData.case && word.indexOf(currentLastWord) > -1) matches.push(aliasData);
else if (!aliasData.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) > -1) matches.push(aliasData);
}
}
}
if (matches.length) return {aliases: matches};
},
2020-12-11 17:26:39 +01:00
renderResults: (channel, wordLowercase, currentSelected, setSelected, chooseSelected, state, config, autocompletes) => {
return autocompletes && autocompletes.aliases && [
2020-09-26 18:18:52 +02:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.AutocompleteItems.Title, {
title: [
2020-12-11 17:26:39 +01:00
"Aliases: ",
2020-09-26 18:18:52 +02:00
BDFDB.ReactUtils.createElement("strong", {
children: wordLowercase
})
]
}),
autocompletes.aliases.map((aliasData, i) => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.AutocompleteItems.Generic, {
onClick: chooseSelected,
onHover: setSelected,
index: i,
selected: currentSelected === i,
alias: aliasData,
text: aliasData.word,
2020-12-11 17:26:39 +01:00
description: BDFDB.StringUtils.insertNRST(aliasData.replace)
2020-09-26 18:18:52 +02:00
}))
].flat(10).filter(n => n);
}
};
2020-12-11 17:26:39 +01:00
BDFDB.PatchUtils.patch(this, BDFDB.LibraryModules.AutocompleteOptions.AUTOCOMPLETE_OPTIONS.COMMANDS, "renderResults", {before: e => {
let m = Array.from(e.methodArguments).find(n => n && n.commands);
if (m) {
let currentLastWord = commandSentinel + e.methodArguments[1];
if (currentLastWord.length >= amounts.minAliasLength) for (let word in commandAliases) {
if (m.commands.length >= BDFDB.DiscordConstants.MAX_AUTOCOMPLETE_RESULTS) break;
let aliasData = commandAliases[word];
let name = word.slice(1);
let command = {
id: "chatalias-" + name,
name: name,
description: BDFDB.StringUtils.insertNRST(aliasData.replace)
};
if (!aliasData.regex && aliasData.autoc) {
if (aliasData.exact) {
if (aliasData.case && word.indexOf(currentLastWord) == 0) m.commands.push(command);
else if (!aliasData.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) == 0) m.commands.push(command);
}
else {
if (aliasData.case && word.indexOf(currentLastWord) > -1) m.commands.push(command);
else if (!aliasData.case && word.toLowerCase().indexOf(currentLastWord.toLowerCase()) > -1) m.commands.push(command);
}
2020-09-26 18:18:52 +02:00
}
}
}
}});
}
2019-01-26 22:45:19 +01:00
2020-06-08 20:07:08 +02:00
this.forceUpdateAll();
2020-02-11 12:58:16 +01:00
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
onStop () {
2020-09-26 18:18:52 +02:00
if (BDFDB.LibraryModules.AutocompleteOptions && BDFDB.LibraryModules.AutocompleteOptions.AUTOCOMPLETE_OPTIONS) {
delete BDFDB.LibraryModules.AutocompleteOptions.AUTOCOMPLETE_OPTIONS.ALIASES;
}
2020-06-08 20:07:08 +02:00
this.forceUpdateAll();
2020-02-11 12:58:16 +01:00
}
2018-10-11 10:21:26 +02:00
2020-09-19 20:49:33 +02:00
getSettingsPanel (collapseStates = {}) {
2020-11-23 20:56:13 +01:00
let settingsPanel;
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, {
2020-09-19 20:49:33 +02:00
collapseStates: collapseStates,
2020-11-23 20:56:13 +01:00
children: _ => {
let settingsItems = [];
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
title: "Settings",
collapseStates: collapseStates,
children: Object.keys(settings).map(key => !this.defaults.settings[key].inner && BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch",
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key]
})).concat(Object.keys(amounts).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "TextInput",
childProps: {
type: "number"
},
plugin: this,
keys: ["amounts", key],
label: this.defaults.amounts[key].description,
basis: "20%",
min: this.defaults.amounts[key].min,
max: this.defaults.amounts[key].max,
value: amounts[key]
2020-12-15 10:35:30 +01:00
}))).concat(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelList, {
2021-01-26 21:40:18 +01:00
title: "Automatically replace Aliases in:",
2020-11-23 20:56:13 +01:00
children: Object.keys(settings).map(key => this.defaults.settings[key].inner && BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch",
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key]
}))
}))
}));
2021-01-26 21:40:18 +01:00
let values = {wordValue: "", replaceValue: ""};
2020-11-23 20:56:13 +01:00
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
2021-01-26 21:40:18 +01:00
title: "Add new Alias",
2020-11-23 20:56:13 +01:00
collapseStates: collapseStates,
2020-09-19 20:49:33 +02:00
children: [
2020-11-23 20:56:13 +01:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "Button",
2021-01-26 21:40:18 +01:00
label: "Pick a Word Value and Replacement Value:",
2021-01-29 20:57:25 +01:00
disabled: !Object.keys(values).every(valueName => values[valueName]),
2020-11-23 20:56:13 +01:00
children: BDFDB.LanguageUtils.LanguageStrings.ADD,
2021-01-29 20:57:25 +01:00
ref: instance => {if (instance) values.addButton = instance;},
2020-11-23 20:56:13 +01:00
onClick: _ => {
2021-01-29 20:57:25 +01:00
this.saveWord(values);
2020-11-23 20:56:13 +01:00
BDFDB.PluginUtils.refreshSettingsPanel(this, settingsPanel, collapseStates);
2020-09-19 20:49:33 +02:00
}
}),
2020-11-23 20:56:13 +01:00
this.createInputs(values)
].flat(10).filter(n => n)
}));
if (!BDFDB.ObjectUtils.isEmpty(aliases)) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
2021-01-26 21:40:18 +01:00
title: "Added Aliases",
2020-11-23 20:56:13 +01:00
collapseStates: collapseStates,
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsList, {
settings: Object.keys(this.defaults.configs),
2021-01-26 21:40:18 +01:00
data: Object.keys(aliases).map((wordValue, i) => Object.assign({}, aliases[wordValue], {
key: wordValue,
label: wordValue
2020-11-23 20:56:13 +01:00
})),
renderLabel: data => BDFDB.ReactUtils.createElement("div", {
style: {width: "100%"},
children: [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextInput, {
value: data.label,
placeholder: data.label,
size: BDFDB.LibraryComponents.TextInput.Sizes.MINI,
maxLength: 100000000000000000000,
onChange: value => {
aliases[value] = aliases[data.label];
delete aliases[data.label];
data.label = value;
BDFDB.DataUtils.save(aliases, this, "words");
}
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextInput, {
value: data.replace,
placeholder: data.replace,
size: BDFDB.LibraryComponents.TextInput.Sizes.MINI,
maxLength: 100000000000000000000,
onChange: value => {
aliases[data.label].replace = value;
BDFDB.DataUtils.save(aliases, this, "words");
}
})
]
}),
onCheckboxChange: (value, instance) => {
aliases[instance.props.cardId][instance.props.settingId] = value;
BDFDB.DataUtils.save(aliases, this, "words");
},
onRemove: (e, instance) => {
delete aliases[instance.props.cardId];
BDFDB.DataUtils.save(aliases, this, "words");
BDFDB.PluginUtils.refreshSettingsPanel(this, settingsPanel, collapseStates);
}
})
}));
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
title: "Remove All",
collapseStates: collapseStates,
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "Button",
color: BDFDB.LibraryComponents.Button.Colors.RED,
2021-01-26 21:40:18 +01:00
label: "Remove all added Aliases",
2020-11-23 20:56:13 +01:00
onClick: _ => {
2021-01-26 21:40:18 +01:00
BDFDB.ModalUtils.confirm(this, "Are you sure you want to remove all added Aliases?", _ => {
2020-11-23 20:56:13 +01:00
aliases = {};
commandAliases = {};
BDFDB.DataUtils.remove(this, "words");
BDFDB.PluginUtils.refreshSettingsPanel(this, settingsPanel, collapseStates);
});
},
children: BDFDB.LanguageUtils.LanguageStrings.REMOVE
})
}));
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
title: "Config Guide",
collapseStates: collapseStates,
children: [
2021-01-26 21:40:18 +01:00
"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 Replacement Value. apple to pear => applepie stays applepie",
"Not Exact: Will replace Words anywhere they appear. apple to pear => applepieapple to pearpiepear",
2020-11-23 20:56:13 +01:00
"Autoc: Will appear in the Autocomplete Menu (if enabled)",
[
2021-01-26 21:40:18 +01:00
"Regex: Will treat the entered Word Value as a Regular Expression - ",
2020-11-23 20:56:13 +01:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Anchor, {href: "https://regexr.com/", children: BDFDB.LanguageUtils.LanguageStrings.HELP + "?"})
],
2021-01-26 21:40:18 +01:00
"File: If the Replacement Value is a File Path it will try to upload the File located at the File Path"
2020-11-23 20:56:13 +01:00
].map(string => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormText, {
type: BDFDB.LibraryComponents.FormComponents.FormTextTypes.DESCRIPTION,
children: string
}))
}));
return settingsItems;
}
});
2020-09-19 20:49:33 +02:00
}
2018-10-11 10:21:26 +02:00
2021-01-06 12:38:36 +01:00
onSettingsClosed () {
2020-09-19 20:49:33 +02:00
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
this.forceUpdateAll();
}
}
2021-01-06 12:38:36 +01:00
forceUpdateAll () {
2020-09-19 20:49:33 +02:00
settings = BDFDB.DataUtils.get(this, "settings");
amounts = BDFDB.DataUtils.get(this, "amounts");
configs = BDFDB.DataUtils.get(this, "configs");
BDFDB.PatchUtils.forceAllUpdates(this);
2020-02-11 12:58:16 +01:00
}
2019-12-10 13:59:44 +01:00
2020-09-19 20:49:33 +02:00
onNativeContextMenu (e) {
if (e.instance.props.value && e.instance.props.value.trim()) {
if ((e.instance.props.type == "NATIVE_TEXT" || e.instance.props.type == "CHANNEL_TEXT_AREA") && settings.addContextMenu) this.injectItem(e, e.instance.props.value.trim());
}
2020-02-11 12:58:16 +01:00
}
2019-02-06 15:46:34 +01:00
2020-09-19 20:49:33 +02:00
onSlateContextMenu (e) {
let text = document.getSelection().toString().trim();
if (text && settings.addContextMenu) this.injectItem(e, text);
}
2020-01-15 10:45:35 +01:00
2020-09-19 20:49:33 +02:00
onMessageContextMenu (e) {
let text = document.getSelection().toString().trim();
if (text && settings.addContextMenu) this.injectItem(e, text);
}
injectItem (e, text) {
let [children, index] = BDFDB.ContextMenuUtils.findItem(e.returnvalue, {id: "devmode-copy-id", group: true});
children.splice(index > -1 ? index : children.length, 0, BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuGroup, {
children: BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
2020-12-22 20:55:39 +01:00
label: BDFDB.LanguageUtils.LibraryStringsFormat("add_to", "ChatAliases"),
2020-09-19 20:49:33 +02:00
id: BDFDB.ContextMenuUtils.createItemId(this.name, "add-alias"),
action: _ => {
this.openAddModal(text.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t"));
}
})
}));
}
2020-06-08 20:07:08 +02:00
2020-09-19 20:49:33 +02:00
processChannelTextAreaForm (e) {
if (!BDFDB.PatchUtils.isPatched(this, e.instance, "handleSendMessage")) BDFDB.PatchUtils.patch(this, e.instance, "handleSendMessage", {before: e2 => {
if (settings.triggerNormal) this.handleSubmit(e, e2, 0);
}}, {force: true, noCache: true});
2019-12-21 11:35:33 +01:00
}
2020-09-19 20:49:33 +02:00
processMessageEditor (e) {
if (!BDFDB.PatchUtils.isPatched(this, e.instance, "onSubmit")) BDFDB.PatchUtils.patch(this, e.instance, "onSubmit", {before: e2 => {
if (settings.triggerEdit) this.handleSubmit(e, e2, 0);
}}, {force: true, noCache: true});
2020-02-11 12:58:16 +01:00
}
2020-09-19 20:49:33 +02:00
processUpload (e) {
if (!BDFDB.PatchUtils.isPatched(this, e.instance, "submitUpload")) BDFDB.PatchUtils.patch(this, e.instance, "submitUpload", {before: e2 => {
if (settings.triggerUpload) this.handleSubmit(e, e2, 1);
}}, {force: true, noCache: true});
2020-02-11 12:58:16 +01:00
}
2020-09-19 20:49:33 +02:00
handleSubmit (e, e2, textIndex) {
if (!settings.replaceBeforeSend || BDFDB.LibraryModules.SlowmodeUtils.getSlowmodeCooldownGuess(e.instance.props.channel.id) > 0) return;
let messageData = this.formatText(e2.methodArguments[textIndex]);
if (messageData) {
if (messageData.text != null) {
e2.methodArguments[textIndex] = messageData.text;
e.instance.props.textValue = "";
if (e.instance.props.richValue) e.instance.props.richValue = BDFDB.SlateUtils.copyRichValue("", e.instance.props.richValue);
if (e.instance.state) {
e.instance.state.textValue = "";
if (e.instance.state.richValue) e.instance.state.richValue = BDFDB.SlateUtils.copyRichValue("", e.instance.state.richValue);
2020-02-11 12:58:16 +01:00
}
2020-09-19 20:49:33 +02:00
BDFDB.ReactUtils.forceUpdate(e.instance);
}
if (messageData.files.length > 0 && (BDFDB.DMUtils.isDMChannel(e.instance.props.channel.id) || BDFDB.UserUtils.can("ATTACH_FILES"))) {
2020-12-17 20:05:26 +01:00
let reply = BDFDB.LibraryModules.MessageReplyStore.getPendingReply(e.instance.props.channel.id);
if (reply && !messageData.text) BDFDB.LibraryModules.UploadUtils.upload(e.instance.props.channel.id, messageData.files.shift(), "", false);
2020-09-19 20:49:33 +02:00
BDFDB.LibraryModules.UploadUtils.instantBatchUpload(e.instance.props.channel.id, messageData.files);
2018-10-11 10:21:26 +02:00
}
2020-02-11 12:58:16 +01:00
}
2020-09-19 20:49:33 +02:00
}
formatText (text) {
text = text.replace(/([\n\t\r])/g, " $1 ");
let newText = [], files = [], wordAliases = {}, multiAliases = {};
for (let word in aliases) {
if (!aliases[word].regex && word.indexOf(" ") == -1) wordAliases[word] = aliases[word];
else multiAliases[word] = aliases[word];
}
for (let word of text.trim().split(" ")) {
newText.push(this.useAliases(word, wordAliases, files, true));
2018-10-11 10:21:26 +02:00
}
2020-09-19 20:49:33 +02:00
newText = newText.length == 1 ? newText[0] : newText.join(" ");
newText = newText.replace(/ ([\n\t\r]) /g, "$1");
newText = this.useAliases(newText, multiAliases, files, false);
2020-11-19 16:51:14 +01:00
return {text: newText, files};
2018-10-11 10:21:26 +02:00
}
2019-09-04 12:34:02 +02:00
2020-09-19 20:49:33 +02:00
useAliases (string, aliases, files, singleWord) {
for (let word in aliases) {
let result = true, replaced = false, tempString1 = string, tempString2 = "";
let config = aliases[word];
let escpAlias = config.regex ? word : BDFDB.StringUtils.regEscape(word);
let regString = config.exact ? "^" + escpAlias + "$" : escpAlias;
while (result != null) {
result = new RegExp(regString, `${config.case ? "" : "i"}${config.exact ? "" : "g"}`).exec(tempString1);
if (result) {
replaced = true;
let replace = config.file ? "" : BDFDB.StringUtils.insertNRST(config.replace);
if (result.length > 1) for (let i = 1; i < result.length; i++) replace = replace.replace(new RegExp("\\\\" + i + "|\\$" + i, "g"), result[i]);
tempString2 += tempString1.slice(0, result.index + result[0].length).replace(result[0], replace);
tempString1 = tempString1.slice(result.index + result[0].length);
if (config.file && typeof config.filedata == "string") {
let filedata = JSON.parse(config.filedata);
2020-11-19 16:51:14 +01:00
files.push(new File([Uint8Array.from(atob(filedata.data), c => c.charCodeAt(0))], filedata.name, {type: filedata.type}));
2020-09-19 20:49:33 +02:00
}
if (config.regex && regString.indexOf("^") == 0) result = null;
2020-02-11 12:58:16 +01:00
}
2020-09-19 20:49:33 +02:00
if (!result) tempString2 += tempString1;
2019-12-20 14:01:16 +01:00
}
2020-09-19 20:49:33 +02:00
if (replaced) {
string = tempString2;
if (singleWord) break;
}
}
return string;
}
2021-01-26 21:40:18 +01:00
openAddModal (wordValue) {
2021-01-29 20:57:25 +01:00
let values = {wordValue, replaceValue: ""};
let configs = BDFDB.ObjectUtils.map(BDFDB.ObjectUtils.filter(this.defaults.configs, key => key != "file", true), n => n.value);
2020-09-19 20:49:33 +02:00
BDFDB.ModalUtils.open(this, {
size: "MEDIUM",
2020-12-22 20:55:39 +01:00
header: BDFDB.LanguageUtils.LibraryStringsFormat("add_to", "ChatAliases"),
2021-01-23 18:50:24 +01:00
subHeader: "",
2020-09-19 20:49:33 +02:00
children: [
this.createInputs(values),
2021-01-29 20:57:25 +01:00
Object.keys(configs).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
2020-09-19 20:49:33 +02:00
type: "Switch",
label: this.defaults.configs[key].description,
2021-01-29 20:57:25 +01:00
value: configs[key],
onChange: value => {configs[key] = value;}
2020-09-19 20:49:33 +02:00
}))
].flat(10).filter(n => n),
buttons: [{
2021-01-29 20:57:25 +01:00
disabled: !Object.keys(values).every(valueName => values[valueName]),
2020-09-19 20:49:33 +02:00
contents: BDFDB.LanguageUtils.LanguageStrings.ADD,
color: "BRAND",
close: true,
2021-01-29 20:57:25 +01:00
ref: instance => {if (instance) values.addButton = instance;},
onClick: _ => {
this.saveWord(values, configs);
2021-01-13 18:54:21 +01:00
this.forceUpdateAll();
2020-02-11 12:58:16 +01:00
}
2020-09-19 20:49:33 +02:00
}]
});
}
createInputs (values) {
return [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormItem, {
title: "Replace:",
2021-01-13 18:54:21 +01:00
className: BDFDB.disCN.marginbottom8,
2020-09-19 20:49:33 +02:00
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextInput, {
2021-01-26 21:40:18 +01:00
value: values.wordValue,
placeholder: values.wordValue,
errorMessage: !values.wordValue && "Choose a Word Value" || aliases[values.wordValue] && "Word Value already used, saving will overwrite old Alias",
2020-09-19 20:49:33 +02:00
onChange: (value, instance) => {
2021-01-26 21:40:18 +01:00
values.wordValue = value.trim();
if (!values.wordValue) instance.props.errorMessage = "Choose a Word Value";
else if (aliases[values.wordValue]) instance.props.errorMessage = "Word Value already used, saving will overwrite old Alias";
2020-09-19 20:49:33 +02:00
else delete instance.props.errorMessage;
2021-01-29 20:57:25 +01:00
values.addButton.props.disabled = !Object.keys(values).every(valueName => values[valueName]);
BDFDB.ReactUtils.forceUpdate(values.addButton);
2020-07-02 10:42:56 +02:00
}
2020-09-19 20:49:33 +02:00
})
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormItem, {
title: "With:",
2021-01-13 18:54:21 +01:00
className: BDFDB.disCN.marginbottom8,
2020-09-19 20:49:33 +02:00
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextInput, {
type: "file",
useFilePath: true,
2021-01-26 21:40:18 +01:00
value: values.replaceValue,
placeholder: values.replaceValue,
2020-09-19 20:49:33 +02:00
autoFocus: true,
2021-01-26 21:40:18 +01:00
errorMessage: !values.replaceValue && "Choose a Replacement Value",
2021-01-29 20:57:25 +01:00
controlsRef: instance => {if (instance) values.fileSelection = instance;},
2020-09-19 20:49:33 +02:00
onChange: (value, instance) => {
2021-01-26 21:40:18 +01:00
values.replaceValue = value.trim();
if (!values.replaceValue) instance.props.errorMessage = "Choose a Replacement Value";
2020-09-19 20:49:33 +02:00
else delete instance.props.errorMessage;
2021-01-29 20:57:25 +01:00
values.addButton.props.disabled = !Object.keys(values).every(valueName => values[valueName]);
BDFDB.ReactUtils.forceUpdate(values.addButton);
2020-09-19 20:49:33 +02:00
}
})
2020-07-02 10:42:56 +02:00
})
2020-09-19 20:49:33 +02:00
];
2020-02-11 12:58:16 +01:00
}
2020-07-26 16:39:51 +02:00
2021-01-29 20:57:25 +01:00
saveWord (values, aliasConfigs = configs) {
if (!values.wordValue || !values.replaceValue || !values.fileSelection) return;
2020-09-19 20:49:33 +02:00
let filedata = null;
2021-01-29 20:57:25 +01:00
if (values.fileSelection.files && values.fileSelection.files[0] && BDFDB.LibraryRequires.fs.existsSync(values.replaceValue)) {
2020-09-19 20:49:33 +02:00
filedata = JSON.stringify({
2021-01-29 20:57:25 +01:00
data: BDFDB.LibraryRequires.fs.readFileSync(values.replaceValue).toString("base64"),
name: values.fileSelection.files[0].name,
type: values.fileSelection.files[0].type
2020-09-19 20:49:33 +02:00
});
}
2021-01-29 20:57:25 +01:00
aliases[values.wordValue] = {
replace: values.replaceValue,
2020-09-19 20:49:33 +02:00
filedata: filedata,
case: aliasConfigs.case,
2021-01-29 20:57:25 +01:00
exact: values.wordValue.indexOf(" ") > -1 ? false : aliasConfigs.exact,
2020-09-19 20:49:33 +02:00
autoc: aliasConfigs.regex ? false : aliasConfigs.autoc,
regex: aliasConfigs.regex,
file: filedata != null
};
BDFDB.DataUtils.save(aliases, this, "words");
}
};
2020-10-09 21:09:35 +02:00
})(window.BDFDB_Global.PluginUtils.buildPlugin(config));
2020-09-19 20:49:33 +02:00
})();