BetterDiscordAddons/Plugins/ChatFilter/ChatFilter.plugin.js

583 lines
26 KiB
JavaScript
Raw Normal View History

2020-10-20 23:25:34 +02:00
/**
* @name ChatFilter
2021-03-05 13:26:41 +01:00
* @author DevilBro
2020-10-20 23:25:34 +02:00
* @authorId 278543574059057154
2023-02-22 15:34:45 +01:00
* @version 3.5.8
2021-03-05 13:26:41 +01:00
* @description Allows you to censor Words or block complete Messages/Statuses
2020-10-20 23:25:34 +02:00
* @invite Jx3TjNS
* @donate https://www.paypal.me/MircoWittrien
* @patreon https://www.patreon.com/MircoWittrien
2021-03-09 15:10:55 +01:00
* @website https://mwittrien.github.io/
* @source https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/ChatFilter/
2021-03-10 09:17:37 +01:00
* @updateUrl https://mwittrien.github.io/BetterDiscordAddons/Plugins/ChatFilter/ChatFilter.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 = (_ => {
2022-09-01 14:40:11 +02:00
const changeLog = {
2022-09-02 12:37:10 +02:00
2020-09-19 20:49:33 +02:00
};
2020-11-13 19:47:44 +01:00
2022-02-05 21:14:17 +01:00
return !window.BDFDB_Global || (!window.BDFDB_Global.loaded && !window.BDFDB_Global.started) ? class {
2022-09-01 14:55:22 +02:00
constructor (meta) {for (let key in meta) this[key] = meta[key];}
getName () {return this.name;}
getAuthor () {return this.author;}
getVersion () {return this.version;}
getDescription () {return `The Library Plugin needed for ${this.name} is missing. Open the Plugin Settings to download it. \n\n${this.description}`;}
2021-02-01 17:13:13 +01:00
downloadLibrary () {
require("request").get("https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js", (e, r, b) => {
2021-03-05 13:14:18 +01:00
if (!e && b && r.statusCode == 200) require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0BDFDB.plugin.js"), b, _ => BdApi.showToast("Finished downloading BDFDB Library", {type: "success"}));
2021-03-06 14:59:48 +01:00
else BdApi.alert("Error", "Could not download BDFDB Library Plugin. Try again later or download it manually from GitHub: https://mwittrien.github.io/downloader/?library");
2021-02-01 17:13:13 +01:00
});
}
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;
2022-09-01 14:55:22 +02:00
BdApi.showConfirmationModal("Library Missing", `The Library Plugin needed for ${this.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
});
}
2022-09-01 14:55:22 +02:00
if (!window.BDFDB_Global.pluginQueue.includes(this.name)) window.BDFDB_Global.pluginQueue.push(this.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");
2022-09-01 14:55:22 +02: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 ${this.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-09-19 20:49:33 +02:00
var oldBlockedMessages, oldCensoredMessages, words;
2021-06-25 12:08:06 +02:00
const configs = {
empty: {value: false, noBlocked: false, description: "Allow the Replacement Value to be empty (ignoring the default)"},
case: {value: false, noBlocked: false, description: "Handle the Word Value case sensitive"},
exact: {value: true, noBlocked: false, description: "Handle the Word Value as an exact Word and not as part of a Word"},
segment: {value: false, noBlocked: true, description: "Only replace the caught Segment of the Word with the Replacement"},
regex: {value: false, noBlocked: false, description: "Handle the Word Value as a RegExp String"}
};
2020-10-09 21:09:35 +02:00
return class ChatFilter extends Plugin {
2021-01-06 12:38:36 +01:00
onLoad () {
2020-09-19 20:49:33 +02:00
this.defaults = {
replaces: {
blocked: {value: "~~BLOCKED~~", description: "Default Replacement Value for blocked Messages: "},
censored: {value: "$!%&%!&", description: "Default Replacement Value for censored Messages: "}
2020-02-09 14:38:05 +01:00
},
2021-06-25 12:08:06 +02:00
general: {
2022-12-01 15:51:28 +01:00
addContextMenu: {value: true, description: "Adds a Context Menu Entry to faster add new blocked/censored Words"},
targetMessages: {value: true, description: "Checks Messages for blocked/censored Words"},
targetStatuses: {value: true, description: "Checks Custom Statuses for blocked/censored Words"},
targetOwn: {value: true, description: "Filters/Blocks your own Messages/Custom Status"}
2020-09-19 20:49:33 +02:00
}
};
2020-02-09 14:38:05 +01:00
2022-11-01 18:03:20 +01:00
this.modulePatches = {
before: [
"MemberListItem",
"Message",
"MessageContent",
"Messages",
"PrivateChannel",
"UserInfo"
],
after: [
"Embed",
"MessageContent"
]
2020-09-19 20:49:33 +02:00
};
2021-01-14 17:40:45 +01:00
this.css = `
${BDFDB.dotCN._chatfilterblocked} {
2023-02-22 15:34:45 +01:00
color: ${BDFDB.DiscordConstants.Colors.RED} !important;
2021-01-14 17:40:45 +01:00
}
${BDFDB.dotCN.messagerepliedmessagecontentclickable}:hover ${BDFDB.dotCN._chatfilterblocked} {
filter: saturate(2);
2020-09-19 20:49:33 +02:00
}
`;
2020-02-09 14:38:05 +01:00
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
onStart () {
2020-02-09 14:38:05 +01:00
words = BDFDB.DataUtils.load(this, "words");
2020-08-29 15:57:35 +02:00
for (let rType in this.defaults.replaces) if (!BDFDB.ObjectUtils.is(words[rType])) words[rType] = {};
2020-02-09 14:38:05 +01:00
2022-09-27 16:53:10 +02:00
BDFDB.PatchUtils.patch(this, BDFDB.LibraryStores.PresenceStore, "findActivity", {after: e => {
2021-06-25 12:08:06 +02:00
if (this.settings.general.targetStatuses && e.returnValue && e.returnValue.state && e.returnValue.type === BDFDB.DiscordConstants.ActivityTypes.CUSTOM_STATUS) {
let {content} = this.parseMessage({
content: e.returnValue.state,
embeds: [],
id: "status",
2022-09-27 16:53:10 +02:00
author: BDFDB.LibraryStores.UserStore.getUser(e.methodArguments[0])
2021-06-25 12:08:06 +02:00
});
if (content) return Object.assign({}, e.returnValue, {state: content});
else if (!e.returnValue.emoji) return null;
}
}});
2020-06-08 20:07:08 +02:00
this.forceUpdateAll();
2020-02-09 14:38:05 +01:00
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
onStop () {
2020-06-08 20:07:08 +02:00
this.forceUpdateAll();
2020-02-09 14:38:05 +01:00
}
2018-10-11 10:21:26 +02:00
2020-09-19 20:49:33 +02:00
getSettingsPanel (collapseStates = {}) {
2021-02-19 11:54:23 +01:00
let settingsPanel;
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, {
collapseStates: collapseStates,
2021-02-19 11:54:23 +01:00
children: _ => {
let settingsItems = [];
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
title: "Settings",
collapseStates: collapseStates,
2021-06-25 12:08:06 +02:00
children: Object.keys(this.defaults.general).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
2021-02-19 11:54:23 +01:00
type: "Switch",
plugin: this,
2021-06-25 12:08:06 +02:00
keys: ["general", key],
label: this.defaults.general[key].description,
value: this.settings.general[key]
})).concat(Object.keys(this.defaults.replaces).map(rType => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
2021-02-19 11:54:23 +01:00
type: "TextInput",
plugin: this,
keys: ["replaces", rType],
label: this.defaults.replaces[rType].description,
2021-06-25 12:08:06 +02:00
value: this.settings.replaces[rType],
2021-02-19 11:54:23 +01:00
placeholder: this.defaults.replaces[rType].value
})))
}));
let values = {wordValue: "", replaceValue: "", choice: "blocked"};
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
title: `Add new blocked/censored word`,
collapseStates: collapseStates,
2020-09-19 20:49:33 +02:00
children: [
2021-02-19 11:54:23 +01:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "Button",
label: "Pick a Word Value and Replacement Value",
disabled: !Object.keys(values).every(valuename => values[valuename]),
children: BDFDB.LanguageUtils.LanguageStrings.ADD,
ref: instance => {if (instance) values.addButton = instance;},
onClick: _ => {
this.saveWord(values);
BDFDB.PluginUtils.refreshSettingsPanel(this, settingsPanel, collapseStates);
2020-09-19 20:49:33 +02:00
}
}),
2021-02-19 11:54:23 +01:00
this.createInputs(values)
].flat(10).filter(n => n)
}));
2021-06-25 12:08:06 +02:00
for (let rType in this.defaults.replaces) if (!BDFDB.ObjectUtils.isEmpty(words[rType])) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
2021-02-19 11:54:23 +01:00
title: `Added ${rType} Words`,
collapseStates: collapseStates,
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsList, {
2022-09-27 11:53:04 +02:00
settings: Object.keys(configs).filter(n => !configs[n]["no" + BDFDB.StringUtils.upperCaseFirstChar(rType)]),
2021-02-19 11:54:23 +01:00
data: Object.keys(words[rType]).map(wordValue => Object.assign({}, words[rType][wordValue], {
key: wordValue,
label: wordValue
})),
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 => {
words[rType][value] = words[rType][data.label];
delete words[rType][data.label];
data.label = value;
BDFDB.DataUtils.save(words, this, "words");
}
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextInput, {
value: data.replace,
placeholder: data.replace,
size: BDFDB.LibraryComponents.TextInput.Sizes.MINI,
maxLength: 100000000000000000000,
onChange: value => {
words[rType][data.label].replace = value;
BDFDB.DataUtils.save(words, this, "words");
}
})
]
}),
onCheckboxChange: (value, instance) => {
words[rType][instance.props.cardId][instance.props.settingId] = value;
BDFDB.DataUtils.save(words, this, "words");
},
onRemove: (e, instance) => {
delete words[rType][instance.props.cardId];
BDFDB.DataUtils.save(words, this, "words");
BDFDB.PluginUtils.refreshSettingsPanel(this, settingsPanel, collapseStates);
}
})
}));
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
title: "Remove All",
collapseStates: collapseStates,
2021-06-25 12:08:06 +02:00
children: Object.keys(this.defaults.replaces).map(rType => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
2021-02-19 11:54:23 +01:00
type: "Button",
color: BDFDB.LibraryComponents.Button.Colors.RED,
label: `Remove all ${rType} Words`,
onClick: _ => {
BDFDB.ModalUtils.confirm(this, `Are you sure you want to remove all ${rType} Words?`, _ => {
words[rType] = {};
BDFDB.DataUtils.remove(this, "words", rType);
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: [
"Case: Will block/censor Words while comparing lowercase/uppercase. apple => apple, not APPLE or AppLe",
"Not Case: Will block/censor Words while ignoring lowercase/uppercase. apple => apple, APPLE and AppLe",
"Exact: Will block/censor Words that are exactly the selected Word. apple => apple, not applepie or pineapple",
"Not Exact: Will block/censor all Words containing the selected Word. apple => apple, applepie and pineapple",
"Segment: Will only replace the caught segment in the censored Word. apple with peach => applepie => peachpie",
"Not Segment: Will replae the whole censored Word. apple with peach => applepie => peach",
"Empty: Ignores the default/choosen Replacement Value and removes the Word/Message instead.",
[
"Regex: Will treat the entered Word Value as a Regular Expression. ",
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Anchor, {href: "https://regexr.com/", children: BDFDB.LanguageUtils.LanguageStrings.HELP + "?"})
],
].map(string => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormText, {
2022-10-01 15:07:31 +02:00
type: BDFDB.LibraryComponents.FormComponents.FormText.Types.DESCRIPTION,
2021-02-19 11:54:23 +01:00
children: string
}))
}));
return settingsItems;
}
});
2020-09-19 20:49:33 +02:00
}
2019-10-22 11:37:23 +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-06-25 12:08:06 +02:00
forceUpdateAll () {
2020-09-19 20:49:33 +02:00
oldBlockedMessages = {};
oldCensoredMessages = {};
2020-02-09 14:38:05 +01:00
2020-09-19 20:49:33 +02:00
BDFDB.PatchUtils.forceAllUpdates(this);
BDFDB.MessageUtils.rerenderAll();
2020-02-09 14:38:05 +01:00
}
2019-01-26 22:45:19 +01:00
2022-12-01 14:19:16 +01:00
onTextAreaContextMenu (e) {
2020-09-19 20:49:33 +02:00
let text = document.getSelection().toString().trim();
2021-06-25 12:08:06 +02:00
if (text && this.settings.general.addContextMenu) this.injectItem(e, text);
2020-09-19 20:49:33 +02:00
}
2020-09-19 20:49:33 +02:00
onMessageContextMenu (e) {
let text = document.getSelection().toString().trim();
2021-06-25 12:08:06 +02:00
if (text && this.settings.general.addContextMenu) this.injectItem(e, text);
2020-09-19 20:49:33 +02:00
}
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", "ChatFilter"),
2020-09-19 20:49:33 +02:00
id: BDFDB.ContextMenuUtils.createItemId(this.name, "add-filter"),
action: _ => {
this.openAddModal(text.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t"));
}
})
}));
}
2020-09-19 20:49:33 +02:00
processMessages (e) {
2022-11-01 18:03:20 +01:00
if (!this.settings.general.targetMessages) return;
e.instance.props.channelStream = [].concat(e.instance.props.channelStream);
for (let i in e.instance.props.channelStream) {
let message = e.instance.props.channelStream[i].content;
if (message) {
if (BDFDB.ArrayUtils.is(message.attachments)) this.checkMessage(e.instance.props.channelStream[i], message);
else if (BDFDB.ArrayUtils.is(message)) for (let j in message) {
let childMessage = message[j].content;
if (childMessage && BDFDB.ArrayUtils.is(childMessage.attachments)) this.checkMessage(message[j], childMessage);
2020-09-19 20:49:33 +02:00
}
2020-07-02 11:43:05 +02:00
}
2020-01-17 13:12:34 +01:00
}
2020-07-02 11:43:05 +02:00
}
2020-09-19 20:49:33 +02:00
checkMessage (stream, message) {
2021-01-13 18:54:21 +01:00
let {blocked, censored, content, embeds} = this.parseMessage(message);
let changeMessage = (change, cache) => {
if (change) {
if (!cache[message.id]) cache[message.id] = new BDFDB.DiscordObjects.Message(message);
stream.content.content = content;
stream.content.embeds = embeds;
}
else if (cache[message.id] && Object.keys(message).some(key => !BDFDB.equals(cache[message.id][key], message[key]))) {
stream.content.content = cache[message.id].content;
stream.content.embeds = cache[message.id].embeds;
delete cache[message.id];
}
};
changeMessage(blocked, oldBlockedMessages);
changeMessage(censored, oldCensoredMessages);
2020-02-09 14:38:05 +01:00
}
2019-01-26 22:45:19 +01:00
2020-09-19 20:49:33 +02:00
processMessage (e) {
2022-11-01 18:03:20 +01:00
if (!this.settings.general.targetMessages) return;
let repliedMessage = e.instance.props.childrenRepliedMessage;
if (repliedMessage && repliedMessage.props && repliedMessage.props.children && repliedMessage.props.children.props && repliedMessage.props.children.props.referencedMessage && repliedMessage.props.children.props.referencedMessage.message && (oldBlockedMessages[repliedMessage.props.children.props.referencedMessage.message.id] || oldCensoredMessages[repliedMessage.props.children.props.referencedMessage.message.id])) {
let {blocked, censored, content, embeds} = this.parseMessage(repliedMessage.props.children.props.referencedMessage.message);
repliedMessage.props.children.props.referencedMessage.message = new BDFDB.DiscordObjects.Message(Object.assign({}, repliedMessage.props.children.props.referencedMessage.message, {content, embeds}));
2020-09-19 20:49:33 +02:00
}
2020-02-09 14:38:05 +01:00
}
2019-01-26 22:45:19 +01:00
2020-09-19 20:49:33 +02:00
processMessageContent (e) {
2022-11-01 18:03:20 +01:00
if (!this.settings.general.targetMessages || !e.instance.props.message) return;
if (!e.returnvalue) {
if (oldBlockedMessages[e.instance.props.message.id]) e.instance.props.className = BDFDB.DOMUtils.formatClassName(e.instance.props.className, BDFDB.disCN._chatfilterblocked);
if (oldCensoredMessages[e.instance.props.message.id] && e.instance.props.message.content != oldCensoredMessages[e.instance.props.message.id].content) e.instance.props.className = BDFDB.DOMUtils.formatClassName(e.instance.props.className, BDFDB.disCN._chatfiltercensored);
}
else {
if (oldBlockedMessages[e.instance.props.message.id]) e.returnvalue.props.children.push(this.createStamp(oldBlockedMessages[e.instance.props.message.id].content, "blocked"));
if (oldCensoredMessages[e.instance.props.message.id]) e.returnvalue.props.children.push(this.createStamp(oldCensoredMessages[e.instance.props.message.id].content, "censored"));
2021-01-13 18:54:21 +01:00
}
}
processEmbed (e) {
2022-11-01 18:03:20 +01:00
if (!this.settings.general.targetMessages || !e.instance.props.embed || !e.instance.props.embed.censored || !oldCensoredMessages[e.instance.props.embed.message_id]) return;
let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, {props: [["className", BDFDB.disCN.embeddescription]]});
if (index > -1) children[index].props.children = [
children[index].props.children,
this.createStamp(oldCensoredMessages[e.instance.props.embed.message_id].embeds[e.instance.props.embed.index].rawDescription, "censored")
].flat(10).filter(n => n);
2021-01-23 22:41:16 +01:00
}
processUserInfo (e) {
this.checkActivities(e);
}
processMemberListItem (e) {
this.checkActivities(e);
}
processPrivateChannel (e) {
this.checkActivities(e);
}
checkActivities (e) {
2022-11-01 18:03:20 +01:00
if (!this.settings.general.targetStatuses || !e.instance.props.activities || !e.instance.props.activities.length || !e.instance.props.user) return;
let index = e.instance.props.activities.findIndex(n => n && n.type == BDFDB.DiscordConstants.ActivityTypes.CUSTOM_STATUS);
if (index > -1 && e.instance.props.activities[index].state) {
let {content} = this.parseMessage({content: e.instance.props.activities[index].state, embeds: [], id: "status", author: e.instance.props.user});
if (content) e.instance.props.activities[index] = Object.assign({}, e.instance.props.activities[index], {state: content});
else if (!e.instance.props.activities[index].emoji) {
e.instance.props.activities = [].concat(e.instance.props.activities);
e.instance.props.activities.splice(index, 1);
2021-01-23 22:41:16 +01:00
}
2020-09-19 20:49:33 +02:00
}
}
createStamp (tooltipText, label) {
return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
text: tooltipText,
tooltipConfig: {style: "max-width: 400px"},
2021-05-03 20:30:07 +02:00
children: BDFDB.ReactUtils.createElement("span", {
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN.messagetimestamp, BDFDB.disCN.messagetimestampinline, BDFDB.disCN[`_chatfilter${label}stamp`]),
children: BDFDB.ReactUtils.createElement("span", {
className: BDFDB.disCN.messageedited,
children: `(${label})`
})
2020-09-19 20:49:33 +02:00
})
});
2020-02-09 14:38:05 +01:00
}
2019-01-26 22:45:19 +01:00
2020-09-19 20:49:33 +02:00
parseMessage (message) {
2021-01-13 18:54:21 +01:00
let blocked = false, censored = false;
let content = (oldBlockedMessages[message.id] || oldCensoredMessages[message.id] || {}).content || message.content;
let embeds = [].concat((oldBlockedMessages[message.id] || oldCensoredMessages[message.id] || {}).embeds || message.embeds);
let isContent = content && typeof content == "string";
2021-06-25 12:08:06 +02:00
if ((isContent || embeds.length) && (message.author.id != BDFDB.UserUtils.me.id || this.settings.general.targetOwn)) {
2020-09-19 20:49:33 +02:00
let blockedReplace;
for (let bWord in words.blocked) {
2021-01-13 18:54:21 +01:00
let compareContent = [isContent && content, embeds.map(e => e.rawDescription)].flat(10).filter(n => n).join(" ");
2021-06-25 12:08:06 +02:00
blockedReplace = words.blocked[bWord].empty ? "" : (words.blocked[bWord].replace || this.settings.replaces.blocked);
2020-09-19 20:49:33 +02:00
let reg = this.createReg(bWord, words.blocked[bWord]);
if (words.blocked[bWord].regex || bWord.indexOf(" ") > -1) {
2021-01-13 18:54:21 +01:00
if (isContent && this.testWord(compareContent, reg)) blocked = true;
2020-08-29 15:57:35 +02:00
}
2021-01-13 18:54:21 +01:00
else for (let word of compareContent.replace(/([\n\t\r])/g, " $1 ").split(" ")) {
2020-09-19 20:49:33 +02:00
if (this.testWord(word, reg)) {
blocked = true;
break;
2020-08-29 15:57:35 +02:00
}
}
2020-09-19 20:49:33 +02:00
if (blocked) break;
}
2021-01-13 18:54:21 +01:00
if (blocked) return {blocked, censored, content: blockedReplace, embeds: []};
2020-09-19 20:49:33 +02:00
else {
2021-05-21 14:09:49 +02:00
const checkCensor = string => {
2021-01-13 18:54:21 +01:00
let singleCensored = false;
string = string.replace(/([\n\t\r])/g, " $1 ");
for (let cWord in words.censored) {
2021-06-25 12:08:06 +02:00
let censoredReplace = words.censored[cWord].empty ? "" : (words.censored[cWord].replace || this.settings.replaces.censored);
2021-01-13 18:54:21 +01:00
let reg = this.createReg(cWord, words.censored[cWord]);
let newString = [];
2021-01-20 15:31:30 +01:00
if (words.censored[cWord].segment || words.censored[cWord].regex || cWord.indexOf(" ") > -1) {
2021-01-13 18:54:21 +01:00
if (this.testWord(string, reg)) {
singleCensored = true;
censored = true;
newString = [string.replace(reg, censoredReplace)];
}
else newString = [string];
2020-09-19 20:49:33 +02:00
}
2021-01-13 18:54:21 +01:00
else for (let word of string.split(" ")) {
if (this.testWord(word, reg)) {
singleCensored = true;
censored = true;
newString.push(censoredReplace);
}
else newString.push(word);
2020-09-19 20:49:33 +02:00
}
2021-01-13 18:54:21 +01:00
string = newString.join(" ");
2020-09-19 20:49:33 +02:00
}
2021-01-13 18:54:21 +01:00
return {parsedContent: string.replace(/ ([\n\t\r]) /g, "$1"), singleCensored: singleCensored};
};
if (isContent) {
let {parsedContent, singleCensored} = checkCensor(content);
if (singleCensored) content = parsedContent;
}
for (let i in embeds) if (embeds[i].rawDescription) {
let {parsedContent, singleCensored} = checkCensor(embeds[i].rawDescription);
if (singleCensored) embeds[i] = Object.assign({}, embeds[i], {rawDescription: parsedContent, index: i, message_id: message.id, censored: true});
2020-08-29 15:57:35 +02:00
}
2018-10-11 10:21:26 +02:00
}
}
2021-01-13 18:54:21 +01:00
return {blocked, censored, content, embeds};
2018-10-11 10:21:26 +02:00
}
2020-09-19 20:49:33 +02:00
testWord (word, reg) {
let nativeEmoji = BDFDB.LibraryModules.EmojiUtils.translateSurrogatesToInlineEmoji(word);
if (nativeEmoji != word) return this.regTest(nativeEmoji, reg);
else {
let customEmoji = (/<a{0,1}(:.*:)[0-9]{7,}>/i.exec(word) || [])[1];
2021-09-27 21:19:11 +02:00
if (customEmoji) return this.regTest(customEmoji, reg) || this.regTest(word, reg);
2020-09-19 20:49:33 +02:00
else return this.regTest(word, reg);
}
}
regTest (word, reg) {
2021-02-19 11:54:23 +01:00
let wordWithoutSpecial = word.replace(/[\?\¿\!\¡\.\"\*\-\_\~\u180E\u200B-\u200D\u2060\uFEFF]/g, "");
2020-09-19 20:49:33 +02:00
return word && reg.test(word) || wordWithoutSpecial && reg.test(wordWithoutSpecial);
2020-02-13 11:25:13 +01:00
}
2019-01-26 22:45:19 +01:00
2020-09-19 20:49:33 +02:00
createReg (word, config) {
2021-06-25 12:08:06 +02:00
let escapedWord = config.regex ? word : BDFDB.StringUtils.htmlEscape(BDFDB.StringUtils.regEscape(word));
2021-05-21 14:09:49 +02:00
return new RegExp(config.exact ? "^" + escapedWord + "$" : escapedWord, `${config.case ? "" : "i"}${config.exact ? "" : "g"}`);
2020-09-19 20:49:33 +02:00
}
2019-01-26 22:45:19 +01:00
2021-01-26 21:40:18 +01:00
openAddModal (wordValue) {
let values = {wordValue, replaceValue: "", choice: "blocked"};
2021-06-25 12:08:06 +02:00
let newConfigs = BDFDB.ObjectUtils.map(configs, n => n.value);
2021-01-29 20:57:25 +01:00
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", "ChatFilter"),
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",
2021-06-25 12:08:06 +02:00
label: configs[key].description,
value: newConfigs[key],
onChange: value => {newConfigs[key] = value;}
2020-09-19 20:49:33 +02:00
}))
].flat(10).filter(n => n),
buttons: [{
2021-01-26 21:40:18 +01:00
disabled: !values.wordValue,
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: _ => {
2021-06-25 12:08:06 +02:00
this.saveWord(values, newConfigs);
2021-01-13 18:54:21 +01:00
this.forceUpdateAll();
2020-07-02 10:42:56 +02:00
}
2020-09-19 20:49:33 +02:00
}]
});
}
createInputs (values) {
2021-01-29 20:57:25 +01:00
let wordValueInput;
2020-09-19 20:49:33 +02:00
return [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormItem, {
title: "Block/Censor:",
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" || words[values.choice][values.wordValue] && `Word Value already used, saving will overwrite old ${values.choice} Word`,
2021-01-29 20:57:25 +01:00
ref: instance => {if (instance) wordValueInput = instance;},
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 (words[values.choice][values.wordValue]) instance.props.errorMessage = `Word Value already used, saving will overwrite old ${values.choice} word`;
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 = !values.wordValue;
BDFDB.ReactUtils.forceUpdate(values.addButton);
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, {
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-29 20:57:25 +01:00
onChange: value => {values.replaceValue = value;}
2020-09-19 20:49:33 +02:00
})
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.RadioGroup, {
className: BDFDB.disCN.marginbottom8,
value: values.choice,
2020-11-19 16:51:14 +01:00
options: [{value: "blocked", name: "Block"}, {value: "censored", name: "Censor"}],
2021-01-29 20:57:25 +01:00
onChange: valueObj => {
values.choice = valueObj.value;
if (!values.wordValue) wordValueInput.props.errorMessage = "Choose a Word Value";
else if (words[values.choice][values.wordValue]) wordValueInput.props.errorMessage = `Word Value already used, saving will overwrite old ${values.choice} Word`;
else delete wordValueInput.props.errorMessage;
BDFDB.ReactUtils.forceUpdate(wordValueInput);
2020-07-02 10:42:56 +02:00
}
})
2020-09-19 20:49:33 +02:00
];
}
2020-07-26 17:02:25 +02:00
2021-06-25 12:08:06 +02:00
saveWord (values, wordConfigs = BDFDB.ObjectUtils.map(configs, n => n.value)) {
2021-01-26 21:40:18 +01:00
if (!values.wordValue || !values.choice) return;
2020-09-19 20:49:33 +02:00
if (!BDFDB.ObjectUtils.is(words[values.choice])) words[values.choice] = {};
2021-01-26 21:40:18 +01:00
words[values.choice][values.wordValue] = {
replace: values.replaceValue,
2020-09-19 20:49:33 +02:00
empty: wordConfigs.empty,
case: wordConfigs.case,
2021-01-26 21:40:18 +01:00
exact: values.wordValue.indexOf(" ") > -1 ? false : wordConfigs.exact,
2020-09-19 20:49:33 +02:00
regex: false
};
BDFDB.DataUtils.save(words, this, "words");
}
};
2022-09-01 14:40:11 +02:00
})(window.BDFDB_Global.PluginUtils.buildPlugin(changeLog));
2021-06-25 12:08:06 +02:00
})();