BetterDiscordAddons/Plugins/SplitLargeMessages/SplitLargeMessages.plugin.js

372 lines
14 KiB
JavaScript
Raw Permalink Normal View History

2020-10-20 23:25:34 +02:00
/**
2021-07-24 20:27:25 +02:00
* @name SplitLargeMessages
2021-03-05 13:26:41 +01:00
* @author DevilBro
2020-10-20 23:25:34 +02:00
* @authorId 278543574059057154
2024-02-05 18:34:03 +01:00
* @version 1.8.2
2021-03-05 13:26:41 +01:00
* @description Allows you to enter larger Messages, which will automatically split into several smaller Messages
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/
2021-07-24 20:27:25 +02:00
* @source https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/SplitLargeMessages/
* @updateUrl https://mwittrien.github.io/BetterDiscordAddons/Plugins/SplitLargeMessages/SplitLargeMessages.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 () {
2023-11-18 18:31:04 +01:00
BdApi.Net.fetch("https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js").then(r => {
if (!r || r.status != 200) throw new Error();
else return r.text();
}).then(b => {
if (!b) throw new Error();
else return require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0BDFDB.plugin.js"), b, _ => BdApi.showToast("Finished downloading BDFDB Library", {type: "success"}));
}).catch(error => {
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
});
2020-03-28 07:41:31 +01: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]) => {
2023-10-06 11:36:52 +02:00
const messageDelay = 2000; //changing at own risk, might result in bans or mutes
2021-06-03 22:04:18 +02:00
let maxMessageLength = 2000;
2020-09-19 20:49:33 +02:00
2021-07-24 20:27:25 +02:00
return class SplitLargeMessages extends Plugin {
2021-01-06 12:38:36 +01:00
onLoad () {
2021-02-25 12:37:04 +01:00
this.defaults = {
2021-06-03 22:04:18 +02:00
general: {
2023-11-18 18:31:04 +01:00
byNewlines: {value: false, description: "Try to split Messages on Newlines instead of Spaces", note: "This will stop Sentences from being cut, but might result in more Messages being sent"},
2022-12-31 12:43:36 +01:00
},
amounts: {
2023-11-18 18:31:04 +01:00
splitCounter: {value: 0, description: "Messages will be split after roughly X Characters"},
2024-02-05 18:34:03 +01:00
maxMessages: {value: 0, description: "Maximum Number of split Messages", note: "(0 for unlimited) Messages beyond this count will be discarded"}
2021-02-25 12:37:04 +01:00
}
};
2022-10-24 11:03:56 +02:00
this.modulePatches = {
before: [
"ChannelTextAreaContainer",
"ChannelTextAreaEditor"
],
after: [
"ChannelTextAreaContainer"
]
2020-09-19 20:49:33 +02:00
};
2022-04-08 21:51:57 +02:00
this.css = `
${BDFDB.dotCN.textareacharcounterupsell} {
display: none;
}
`;
2020-09-19 20:49:33 +02:00
}
2021-01-06 12:38:36 +01:00
onStart () {
2022-09-27 16:53:10 +02:00
maxMessageLength = BDFDB.LibraryModules.NitroUtils.canUseIncreasedMessageLength(BDFDB.LibraryStores.UserStore.getCurrentUser()) ? BDFDB.DiscordConstants.MAX_MESSAGE_LENGTH_PREMIUM : BDFDB.DiscordConstants.MAX_MESSAGE_LENGTH;
2021-06-03 22:04:18 +02:00
2022-10-07 14:11:36 +02:00
BDFDB.PatchUtils.patch(this, BDFDB.LibraryModules.ChatRestrictionUtils, "applyChatRestrictions", {before: e => {
if (e.methodArguments[0] && e.methodArguments[0].content && !this.isSlowDowned(e.methodArguments[0].channel)) e.methodArguments[0].content = "_";
}});
2021-06-03 22:04:18 +02:00
BDFDB.PatchUtils.forceAllUpdates(this);
2020-03-28 07:41:31 +01:00
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
onStop () {
2021-06-03 22:04:18 +02:00
BDFDB.PatchUtils.forceAllUpdates(this);
2021-02-25 12:37:04 +01:00
}
getSettingsPanel (collapseStates = {}) {
2021-06-03 22:04:18 +02:00
let settingsPanel;
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, {
collapseStates: collapseStates,
children: _ => {
let settingsItems = [];
2021-02-25 12:37:04 +01:00
2021-06-03 22:04:18 +02:00
for (let key in this.defaults.general) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch",
plugin: this,
keys: ["general", key],
label: this.defaults.general[key].description,
note: this.defaults.general[key].note,
value: this.settings.general[key]
}));
2022-12-31 12:43:36 +01:00
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "TextInput",
childProps: {
type: "number"
},
plugin: this,
keys: ["amounts", "splitCounter"],
label: this.defaults.amounts.splitCounter.description,
basis: "20%",
min: 1000,
max: maxMessageLength,
value: this.settings.amounts.splitCounter < 1000 || this.settings.amounts.splitCounter > maxMessageLength ? maxMessageLength : this.settings.amounts.splitCounter
}));
2023-10-06 11:36:52 +02:00
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "TextInput",
childProps: {
type: "number"
},
plugin: this,
2024-02-05 18:34:03 +01:00
keys: ["amounts", "maxMessages"],
label: this.defaults.amounts.maxMessages.description,
note: this.defaults.amounts.maxMessages.note,
2023-10-06 11:36:52 +02:00
min: 0,
max: 20,
2024-02-05 18:34:03 +01:00
value: this.settings.amounts.maxMessages
2023-10-06 11:36:52 +02:00
}));
2022-12-31 12:43:36 +01:00
2021-06-03 22:04:18 +02:00
return settingsItems;
}
});
2021-02-25 12:37:04 +01:00
}
onSettingsClosed (e) {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
2021-06-03 22:04:18 +02:00
BDFDB.PatchUtils.forceAllUpdates(this);
2021-02-25 12:37:04 +01:00
}
}
2020-09-19 20:49:33 +02:00
processChannelTextAreaContainer (e) {
2022-10-24 11:03:56 +02:00
if (e.instance.props.type != BDFDB.DiscordConstants.ChannelTextAreaTypes.NORMAL && e.instance.props.type != BDFDB.DiscordConstants.ChannelTextAreaTypes.NORMAL_WITH_ACTIVITY && e.instance.props.type != BDFDB.LibraryComponents.ChannelTextAreaTypes.SIDEBAR) return;
2022-12-31 12:43:36 +01:00
const splitMessageLength = this.settings.amounts.splitCounter < 1000 || this.settings.amounts.splitCounter > maxMessageLength ? maxMessageLength : this.settings.amounts.splitCounter;
2022-10-24 11:03:56 +02:00
if (!e.returnvalue) {
BDFDB.PatchUtils.patch(this, e.instance.props, "onSubmit", {instead: e2 => {
2022-12-31 12:43:36 +01:00
if (e2.methodArguments[0].value.length > splitMessageLength && !this.isSlowDowned(e.instance.props.channel)) {
2022-10-24 11:03:56 +02:00
e2.stopOriginalMethodCall();
let messages = this.formatText(e2.methodArguments[0].value).filter(n => n);
for (let i in messages) BDFDB.TimeUtils.timeout(_ => {
let last = i >= messages.length-1;
e2.originalMethod(last ? Object.assign({}, e2.methodArguments[0], {value: messages[i]}) : {stickers: [], uploads: [], value: messages[i]});
if (i >= messages.length-1) BDFDB.NotificationUtils.toast(this.labels.toast_allsent, {type: "success"});
}, messageDelay * i * (messages > 4 ? 2 : 1));
return Promise.resolve({
shouldClear: true,
shouldRefocus: true
});
}
else return e2.callOriginalMethodAfterwards();
2022-10-25 17:22:33 +02:00
}}, {noCache: true});
2022-10-24 11:03:56 +02:00
}
else {
let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, {name: "ChannelTextAreaCounter"});
2022-12-31 12:43:36 +01:00
if (index > -1 && children[index].props.textValue && children[index].props.textValue.length > splitMessageLength && !this.isSlowDowned(e.instance.props.channel)) children[index] = BDFDB.ReactUtils.createElement("div", {
2022-04-01 00:57:22 +02:00
className: BDFDB.disCNS.textareacharcounter + BDFDB.disCN.textareacharcountererror,
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
2022-12-31 12:43:36 +01:00
text: Math.ceil(children[index].props.textValue.length / splitMessageLength * (39/40)) + " " + BDFDB.LanguageUtils.LanguageStrings.MESSAGES,
2022-04-01 00:57:22 +02:00
children: BDFDB.ReactUtils.createElement("span", {
2022-12-31 12:43:36 +01:00
children: splitMessageLength - children[index].props.textValue.length
2020-03-28 07:41:31 +01:00
})
2022-04-01 00:57:22 +02:00
})
});
2020-03-28 07:41:31 +01:00
}
2019-12-28 17:39:14 +01:00
}
2019-01-26 22:45:19 +01:00
2022-10-24 10:42:20 +02:00
processChannelTextAreaEditor (e) {
2023-10-06 11:36:52 +02:00
if (e.instance.props.type == BDFDB.DiscordConstants.ChannelTextAreaTypes.NORMAL || e.instance.props.type == BDFDB.DiscordConstants.ChannelTextAreaTypes.NORMAL_WITH_ACTIVITY || e.instance.props.type == BDFDB.LibraryComponents.ChannelTextAreaTypes.SIDEBAR) e.instance.props.uploadPromptCharacterCount = 100000;
2018-10-11 10:21:26 +02:00
}
2022-04-27 14:55:19 +02:00
isSlowDowned (channel) {
2022-04-30 17:21:21 +02:00
return channel.rateLimitPerUser && !BDFDB.UserUtils.can("MANAGE_CHANNELS", BDFDB.UserUtils.me.id, channel.id) && !BDFDB.UserUtils.can("MANAGE_MESSAGES", BDFDB.UserUtils.me.id, channel.id);
2022-04-27 14:55:19 +02:00
}
2019-01-26 22:45:19 +01:00
2020-09-19 20:49:33 +02:00
formatText (text) {
2024-02-05 18:34:03 +01:00
const separator = this.settings.general.byNewlines ? "\n" : " ";
2022-12-31 12:43:36 +01:00
const splitMessageLength = this.settings.amounts.splitCounter < 1000 || this.settings.amounts.splitCounter > maxMessageLength ? maxMessageLength : this.settings.amounts.splitCounter;
2021-02-25 12:37:04 +01:00
2022-04-08 21:51:57 +02:00
text = text.replace(/\t/g, " ");
2022-12-31 12:43:36 +01:00
let longWords = text.match(new RegExp(`[^${separator.replace("\n", "\\n")}]{${splitMessageLength * (19/20)},}`, "gm"));
2020-09-19 20:49:33 +02:00
if (longWords) for (let longWord of longWords) {
let count1 = 0;
let shortWords = [];
longWord.split("").forEach(c => {
2022-12-31 12:43:36 +01:00
if (shortWords[count1] && (shortWords[count1].length >= splitMessageLength * (19/20) || (c == "\n" && shortWords[count1].length >= splitMessageLength * (19/20) - 100))) count1++;
2020-09-19 20:49:33 +02:00
shortWords[count1] = shortWords[count1] ? shortWords[count1] + c : c;
});
2021-02-25 12:37:04 +01:00
text = text.replace(longWord, shortWords.join(separator));
2020-03-28 07:41:31 +01:00
}
2020-09-19 20:49:33 +02:00
let messages = [];
let count2 = 0;
2021-02-25 12:37:04 +01:00
text.split(separator).forEach((word) => {
2022-12-31 12:43:36 +01:00
if (messages[count2] && (messages[count2] + "" + word).length > splitMessageLength * (39/40)) count2++;
2021-02-25 12:37:04 +01:00
messages[count2] = messages[count2] ? messages[count2] + separator + word : word;
2020-09-19 20:49:33 +02:00
});
2019-01-26 22:45:19 +01:00
2020-09-19 20:49:33 +02:00
let insertCodeBlock = null, insertCodeLine = null;
for (let j = 0; j < messages.length; j++) {
if (insertCodeBlock) {
messages[j] = insertCodeBlock + messages[j];
insertCodeBlock = null;
}
else if (insertCodeLine) {
messages[j] = insertCodeLine + messages[j];
insertCodeLine = null;
}
let codeBlocks = messages[j].match(/`{3,}[\S]*\n|`{3,}/gm);
let codeLines = messages[j].match(/[^`]{0,1}`{1,2}[^`]|[^`]`{1,2}[^`]{0,1}/gm);
if (codeBlocks && codeBlocks.length % 2 == 1) {
messages[j] = messages[j] + "```";
insertCodeBlock = codeBlocks[codeBlocks.length-1] + "\n";
}
else if (codeLines && codeLines.length % 2 == 1) {
insertCodeLine = codeLines[codeLines.length-1].replace(/[^`]/g, "");
messages[j] = messages[j] + insertCodeLine;
}
2020-03-28 07:41:31 +01:00
}
2024-02-05 18:34:03 +01:00
return this.settings.amounts.maxMessages ? messages.slice(0, this.settings.amounts.maxMessages) : messages;
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2021-01-06 12:38:36 +01:00
setLabelsByLanguage () {
2020-09-19 20:49:33 +02:00
switch (BDFDB.LanguageUtils.getLanguage().id) {
case "bg": // Bulgarian
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Всички изпратени съобщения"
2020-09-19 20:49:33 +02:00
};
case "da": // Danish
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Alle beskeder sendt"
2020-09-19 20:49:33 +02:00
};
case "de": // German
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Alle Nachrichten gesendet"
2020-09-19 20:49:33 +02:00
};
case "el": // Greek
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Όλα τα μηνύματα εστάλησαν"
2020-09-19 20:49:33 +02:00
};
case "es": // Spanish
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Todos los mensajes enviados"
2020-09-19 20:49:33 +02:00
};
case "fi": // Finnish
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Kaikki viestit lähetetty"
2020-09-19 20:49:33 +02:00
};
case "fr": // French
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Tous les messages envoyés"
2020-09-19 20:49:33 +02:00
};
case "hr": // Croatian
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Sve poruke poslane"
2020-09-19 20:49:33 +02:00
};
case "hu": // Hungarian
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Minden üzenet elküldve"
2020-09-19 20:49:33 +02:00
};
case "it": // Italian
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Tutti i messaggi inviati"
2020-09-19 20:49:33 +02:00
};
case "ja": // Japanese
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "送信されたすべてのメッセージ"
2020-09-19 20:49:33 +02:00
};
case "ko": // Korean
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "보낸 모든 메시지"
2020-09-19 20:49:33 +02:00
};
case "lt": // Lithuanian
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Visi pranešimai išsiųsti"
2020-09-19 20:49:33 +02:00
};
case "nl": // Dutch
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Alle berichten zijn verzonden"
2020-09-19 20:49:33 +02:00
};
case "no": // Norwegian
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Alle meldinger sendt"
2020-09-19 20:49:33 +02:00
};
case "pl": // Polish
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Wszystkie wiadomości wysłane"
2020-09-19 20:49:33 +02:00
};
case "pt-BR": // Portuguese (Brazil)
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Todas as mensagens enviadas"
2020-09-19 20:49:33 +02:00
};
case "ro": // Romanian
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Toate mesajele trimise"
2020-09-19 20:49:33 +02:00
};
case "ru": // Russian
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Все сообщения отправлены"
2020-09-19 20:49:33 +02:00
};
case "sv": // Swedish
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "Alla meddelanden skickade"
2020-09-19 20:49:33 +02:00
};
case "th": // Thai
2020-09-19 20:49:33 +02:00
return {
toast_allsent: "ส่งข้อความทั้งหมดแล้ว"
};
case "tr": // Turkish
return {
toast_allsent: "Tüm mesajlar gönderildi"
};
case "uk": // Ukrainian
return {
toast_allsent: "Усі повідомлення надіслано"
};
case "vi": // Vietnamese
return {
toast_allsent: "Tất cả tin nhắn đã gửi"
};
2021-01-15 17:54:22 +01:00
case "zh-CN": // Chinese (China)
return {
toast_allsent: "已发送所有消息"
};
2021-01-15 17:54:22 +01:00
case "zh-TW": // Chinese (Taiwan)
return {
toast_allsent: "已發送所有消息"
};
default: // English
return {
2021-06-14 15:24:55 +02:00
toast_allsent: "All Messages sent"
2020-09-19 20:49:33 +02:00
};
}
2020-03-28 07:41:31 +01:00
}
2020-09-19 20:49:33 +02:00
};
2022-09-01 14:40:11 +02:00
})(window.BDFDB_Global.PluginUtils.buildPlugin(changeLog));
2021-06-15 09:18:57 +02:00
})();