BetterDiscordAddons/Plugins/CustomQuoter/CustomQuoter.plugin.js

383 lines
17 KiB
JavaScript
Raw Normal View History

2020-02-27 08:44:03 +01:00
//META{"name":"CustomQuoter","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/CustomQuoter","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/CustomQuoter/CustomQuoter.plugin.js"}*//
2020-02-23 18:55:20 +01:00
var CustomQuoter = (_ => {
2020-04-15 09:14:05 +02:00
var _this;
2020-07-01 16:37:43 +02:00
var settings = {}, formats = {}, format = null;
2020-04-15 09:14:05 +02:00
2020-08-20 13:37:11 +02:00
const PreviewMessageComponent = class PreviewMessage extends BdApi.React.Component {
2020-02-23 19:40:36 +01:00
render() {
let spoofChannel = new BDFDB.DiscordObjects.Channel({
id: "126223823845647771",
2020-02-23 20:29:09 +01:00
guild_id: "850725684241078788",
2020-02-23 19:40:36 +01:00
name: "Test Channel"
});
let spoofQuotedMessage = new BDFDB.DiscordObjects.Message({
2020-03-05 20:37:34 +01:00
id: "562432230424221059",
2020-02-23 19:40:36 +01:00
author: new BDFDB.DiscordObjects.User({
id: "230422432565221049",
username: "Quoted User"
}),
channel_id: spoofChannel.id,
content: "This is a test message\nto showcase what the quote would look like"
});
let spoofMessage = new BDFDB.DiscordObjects.Message({
author: new BDFDB.DiscordObjects.User({
id: "222242304256531049",
username: "Test User"
}),
channel_id: spoofChannel.id,
2020-07-01 16:37:43 +02:00
content: _this.parseQuote(spoofQuotedMessage, spoofChannel, this.props.format)
2020-02-23 19:40:36 +01:00
});
return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.MessageGroup, {
className: BDFDB.disCNS.message + BDFDB.disCN.messagecozymessage,
message: spoofMessage,
channel: spoofChannel
});
}
};
2020-02-23 18:55:20 +01:00
return class CustomQuoter {
getName () {return "CustomQuoter";}
2020-08-28 16:05:49 +02:00
getVersion () {return "1.1.5";}
2020-02-23 18:55:20 +01:00
getAuthor () {return "DevilBro";}
2020-04-13 20:21:09 +02:00
getDescription () {return "Let's you customize the output of the native quote feature of Discord.";}
2020-02-23 18:55:20 +01:00
initConstructor () {
2020-04-15 09:14:05 +02:00
_this = this;
2020-02-23 18:55:20 +01:00
this.defaults = {
settings: {
2020-04-13 20:21:09 +02:00
quoteOnlySelected: {value:true, description:"Only insert selected text in a quoted message"},
2020-02-23 18:55:20 +01:00
ignoreMentionInDM: {value:true, description:"Do not add a mention in DM channels"},
forceZeros: {value:false, description:"Force leading Zeros"}
}
};
}
getSettingsPanel (collapseStates = {}) {
if (!window.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return;
2020-09-04 11:25:48 +02:00
settings = BDFDB.DataUtils.get(this, "settings");
formats = Object.assign({"Standard": "$quote $mention"}, BDFDB.DataUtils.load(this, "formats"));
2020-03-28 07:55:39 +01:00
let settingsPanel, settingsItems = [], innerItems = [];
2020-02-23 18:55:20 +01:00
2020-03-28 07:55:39 +01:00
for (let key in settings) innerItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
2020-02-23 18:55:20 +01:00
className: BDFDB.disCN.marginbottom8,
type: "Switch",
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key],
2020-06-12 15:23:14 +02:00
onChange: (value, instance) => {
settings[key] = value;
2020-02-23 18:55:20 +01:00
}
}));
2020-03-28 07:55:39 +01:00
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
2020-02-23 18:55:20 +01:00
title: "Settings",
collapseStates: collapseStates,
2020-03-28 07:55:39 +01:00
children: innerItems
2020-02-23 18:55:20 +01:00
}));
2020-03-28 07:55:39 +01:00
innerItems = [];
2020-02-23 18:55:20 +01:00
2020-07-01 16:37:43 +02:00
innerItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex, {
2020-02-23 18:55:20 +01:00
className: BDFDB.disCN.marginbottom8,
2020-07-01 16:37:43 +02:00
align: BDFDB.LibraryComponents.Flex.Align.END,
children: [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex.Child, {
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormItem, {
title: "Name:",
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextInput, {
className: "input-newquote input-name",
value: "",
placeholder: "Formatname"
})
})
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex.Child, {
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormItem, {
title: "Quote:",
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextInput, {
className: "input-newquote input-quote",
value: "",
placeholder: "Quote"
})
})
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Button, {
style: {marginBottom: 1},
onClick: _ => {
for (let input of settingsPanel.querySelectorAll(".input-newquote " + BDFDB.dotCN.input)) if (!input.value || input.value.length == 0 || input.value.trim().length == 0) return BDFDB.NotificationUtils.toast("Fill out all fields to add a new quote.", {type:"danger"});
let key = settingsPanel.querySelector(".input-name " + BDFDB.dotCN.input).value.trim();
let quote = settingsPanel.querySelector(".input-quote " + BDFDB.dotCN.input).value.trim();
if (formats[key]) return BDFDB.NotificationUtils.toast("A quote with the choosen name already exists. Please choose another name.", {type:"danger"});
else {
formats[key] = quote;
BDFDB.DataUtils.save(formats, this, "formats");
BDFDB.PluginUtils.refreshSettingsPanel(this, settingsPanel, collapseStates);
}
},
children: BDFDB.LanguageUtils.LanguageStrings.ADD
})
]
2020-02-23 18:55:20 +01:00
}));
2020-07-01 16:37:43 +02:00
innerItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormDivider, {
className: BDFDB.disCN.marginbottom20
2020-02-23 19:40:36 +01:00
}));
2020-07-01 16:37:43 +02:00
for (let key in formats) innerItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Card, {
cardId: key,
noRemove: key == "Standard",
onRemove: _ => {
delete formats[key];
BDFDB.DataUtils.save(formats, this, "formats");
BDFDB.PluginUtils.refreshSettingsPanel(this, settingsPanel, collapseStates);
},
2020-08-20 13:37:11 +02:00
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex, {
direction: BDFDB.LibraryComponents.Flex.Direction.VERTICAL,
2020-07-01 16:37:43 +02:00
children: [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
className: BDFDB.disCN.marginbottom8,
type: "TextInput",
plugin: this,
keys: ["formats", key],
label: key + ":",
basis: "70%",
value: formats[key],
onChange: (value, instance) => {
formats[key] = value;
BDFDB.ReactUtils.forceUpdate(BDFDB.ReactUtils.findOwner(instance._reactInternalFiber.return, {key: "PREVIEW_MESSAGE_" + key.replace(/\s/g, "_")}));
}
}),
2020-08-20 13:37:11 +02:00
BDFDB.ReactUtils.createElement(PreviewMessageComponent, {
2020-07-01 16:37:43 +02:00
key: "PREVIEW_MESSAGE_" + key.replace(/\s/g, "_"),
format: key
})
]
})
2020-02-23 19:40:36 +01:00
}));
2020-02-23 18:55:20 +01:00
2020-03-28 07:55:39 +01:00
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
2020-07-01 16:37:43 +02:00
title: "Formats",
2020-02-23 18:55:20 +01:00
collapseStates: collapseStates,
2020-03-28 07:55:39 +01:00
children: innerItems
2020-02-23 18:55:20 +01:00
}));
2020-03-28 07:55:39 +01:00
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.CollapseContainer, {
2020-02-23 18:55:20 +01:00
title: "Placeholder Guide",
collapseStates: collapseStates,
children: [
"$quote will be replaced with the quoted message content",
2020-08-20 13:37:11 +02:00
"$rawQuote will be replaced with the raw quoted message content",
2020-02-23 18:55:20 +01:00
"$mention will be replaced with a mention of the message author",
2020-03-05 20:37:34 +01:00
"$link will be replaced with a discord direct link pointing to the message",
2020-02-23 18:55:20 +01:00
"$authorId will be replaced with the ID of the message author",
2020-06-29 19:48:51 +02:00
"$authorName will be replaced with the nickname or username of the message author",
"$authorAccount will be replaced with the accountname of the message author (username#discriminator)",
2020-02-23 18:55:20 +01:00
"$channel will be replaced with a mention of the channel (ignored for DMs)",
"$channelId will be replaced with the ID of the channel",
"$channelName will be replaced with the Name of the channel",
"$serverId will be replaced with the ID of the server",
"$serverName will be replaced with the name of the server",
"$hour will be replaced with the quote hour",
"$minute will be replaced with the quote minutes",
"$second will be replaced with the quote seconds",
"$msecond will be replaced with the quote milliseconds",
"$timemode will change $hour to a 12h format and will be replaced with AM/PM",
"$year will be replaced with the quote year",
"$month will be replaced with the quote month",
"$day will be replaced with the quote day",
"$monthnameL will be replaced with the monthname in long format based on the Discord Language",
"$monthnameS will be replaced with the monthname in short format based on the Discord Language",
"$weekdayL will be replaced with the weekday in long format based on the Discord Language",
"$weekdayS will be replaced with the weekday in short format based on the Discord Language"
].map(string => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormText, {
type: BDFDB.LibraryComponents.FormComponents.FormTextTypes.DESCRIPTION,
children: string
}))
}));
2020-03-28 07:55:39 +01:00
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, settingsItems);
2020-02-23 18:55:20 +01:00
}
2020-04-11 19:32:58 +02:00
// Legacy
2020-08-21 16:17:47 +02:00
load () {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) BDFDB.PluginUtils.load(this);
}
2020-02-23 18:55:20 +01:00
start () {
if (!window.BDFDB) window.BDFDB = {myPlugins:{}};
if (window.BDFDB && window.BDFDB.myPlugins && typeof window.BDFDB.myPlugins == "object") window.BDFDB.myPlugins[this.getName()] = this;
let libraryScript = document.querySelector("head script#BDFDBLibraryScript");
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
libraryScript.setAttribute("id", "BDFDBLibraryScript");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js");
libraryScript.setAttribute("date", performance.now());
libraryScript.addEventListener("load", _ => {this.initialize();});
document.head.appendChild(libraryScript);
}
else if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
this.startTimeout = setTimeout(_ => {
try {return this.initialize();}
catch (err) {console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not initiate plugin! " + err);}
}, 30000);
}
initialize () {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
if (this.started) return;
BDFDB.PluginUtils.init(this);
2020-09-11 19:31:36 +02:00
BDFDB.PatchUtils.patch(this, BDFDB.LibraryModules.QuoteUtils, "createQuotedText", {instead: e => {
2020-02-23 18:55:20 +01:00
return this.parseQuote(e.methodArguments[0], e.methodArguments[1]);
}});
2020-06-10 19:21:54 +02:00
this.forceUpdateAll();
2020-02-23 18:55:20 +01:00
}
else console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not load BD functions!");
}
stop () {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
this.stopping = true;
BDFDB.PluginUtils.clear(this);
}
}
2020-04-11 19:32:58 +02:00
// Begin of own functions
2020-02-23 18:55:20 +01:00
onSettingsClosed () {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
this.forceUpdateAll();
}
}
2020-07-01 16:37:43 +02:00
onMessageContextMenu (e) {
if (e.instance.props.message && e.instance.props.channel) {
2020-07-29 00:32:40 +02:00
let item = null, action = (choice, copy) => {
2020-07-01 16:37:43 +02:00
format = choice;
2020-07-29 00:32:40 +02:00
if (copy || !BDFDB.LibraryModules.QuoteUtils.canQuote(e.instance.props.message, e.instance.props.channel)) {
2020-07-01 16:37:43 +02:00
BDFDB.LibraryRequires.electron.clipboard.write({text:this.parseQuote(e.instance.props.message, e.instance.props.channel)});
BDFDB.NotificationUtils.toast("Quote has been copied to clipboard.", {type:"success"});
}
else BDFDB.LibraryModules.MessageManageUtils.quoteMessage(e.instance.props.channel, e.instance.props.message);
format = null;
};
let [children, index] = BDFDB.ContextMenuUtils.findItem(e.returnvalue, {id: "quote"});
2020-07-29 00:32:40 +02:00
if (index > -1) {
item = children[index];
item.props.action = event => {action(null, event.shiftKey);};
}
2020-07-01 16:37:43 +02:00
else {
item = BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
label: BDFDB.LanguageUtils.LanguageStrings.QUOTE,
id: "quote",
2020-07-29 00:32:40 +02:00
action: _ => {action(null, event.shiftKey);}
2020-07-01 16:37:43 +02:00
});
let [unreadChildren, unreadIndex] = BDFDB.ContextMenuUtils.findItem(e.returnvalue, {id: "mark-unread"});
unreadChildren.splice(unreadIndex > -1 ? unreadIndex - 1 : unreadChildren.length, 0, item);
}
let addedFormats = BDFDB.ObjectUtils.exclude(formats, "Standard");
if (!BDFDB.ObjectUtils.isEmpty(addedFormats)) item.props.children = BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuGroup, {
children: Object.keys(addedFormats).map(key => BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
label: key,
id: BDFDB.ContextMenuUtils.createItemId(this.name, "added-quote", key),
2020-07-29 00:32:40 +02:00
action: event => {action(key, event.shiftKey);}
2020-07-01 16:37:43 +02:00
}))
});
}
}
parseQuote (message, channel, choice = format) {
2020-02-23 18:55:20 +01:00
let languageId = BDFDB.LanguageUtils.getLanguage().id;
2020-07-01 16:37:43 +02:00
let quoteFormat = formats[choice] || formats.Standard || "";
let guild = channel.guild_id ? (BDFDB.LibraryModules.GuildStore.getGuild(channel.guild_id) || {id: channel.guild_id, name: "Test Server"}) : {id: BDFDB.DiscordConstants.ME, name: BDFDB.LanguageUtils.LanguageStrings.DIRECT_MESSAGES};
let member = guild && BDFDB.LibraryModules.MemberStore.getMember(guild.id, message.author.id);
2020-02-23 18:55:20 +01:00
let timestamp = new Date(message.editedTimestamp || message.timestamp);
let hour = timestamp.getHours(), minute = timestamp.getMinutes(), second = timestamp.getSeconds(), msecond = timestamp.getMilliseconds(), day = timestamp.getDate(), month = timestamp.getMonth()+1, timemode = "";
2020-07-01 16:37:43 +02:00
if (quoteFormat.indexOf("$timemode") > -1) {
2020-02-23 18:55:20 +01:00
timemode = hour >= 12 ? "PM" : "AM";
hour = hour % 12;
hour = hour ? hour : 12;
}
2020-04-13 20:21:09 +02:00
let content = message.content;
let selectedText = settings.quoteOnlySelected && document.getSelection().toString().trim();
2020-07-01 16:37:43 +02:00
if (selectedText) content = BDFDB.StringUtils.extractSelection(content, selectedText);
2020-08-20 13:37:11 +02:00
if (content) {
content = content.replace(/(@everyone|@here)/g, "`$1`").replace(/``(@everyone|@here)``/g, "`$1`");
content = content.replace(/<@[!&]{0,1}([0-9]{10,})>/g, (string, match) => {
2020-07-01 16:37:43 +02:00
let user = BDFDB.LibraryModules.UserStore.getUser(match);
if (user) {
2020-07-29 00:32:40 +02:00
let userMember = channel.guild_id && BDFDB.LibraryModules.MemberStore.getMember(guild.id, match);
2020-07-01 16:37:43 +02:00
return `\`@${userMember && userMember.nick || user.username}\``;
}
2020-07-29 00:32:40 +02:00
else if (channel.guild_id && guild.roles[match] && guild.roles[match].name) return `\`${guild.roles[match].name.indexOf("@") == 0 ? "" : "@"}${guild.roles[match].name}\``;
2020-07-01 16:37:43 +02:00
return string;
});
}
2020-08-20 13:37:11 +02:00
let unquotedLines = content.split("\n").filter(line => !line.startsWith("> "));
let quotedLines = unquotedLines.slice(unquotedLines.findIndex(line => line.trim().length > 0)).map(line => "> " + line + "\n").join("");
2020-02-23 20:24:40 +01:00
2020-07-01 16:37:43 +02:00
return BDFDB.StringUtils.insertNRST(quoteFormat)
2020-02-23 18:55:20 +01:00
.replace("$mention", settings.ignoreMentionInDM && channel.isDM() ? "" : `<@!${message.author.id}>`)
2020-08-10 23:28:14 +02:00
.replace("$link", `<https://discordapp.com/channels/${guild.id}/${channel.id}/${message.id}>`)
2020-03-21 10:23:14 +01:00
.replace("$authorName", member && member.nick || message.author.username || "")
2020-03-23 18:19:39 +01:00
.replace("$authorAccount", `${message.author.username}#${message.author.discriminator}`)
2020-02-23 19:44:50 +01:00
.replace("$authorId", message.author.id || "")
2020-02-23 19:40:36 +01:00
.replace("$channelName", channel.name || "")
2020-02-23 19:44:50 +01:00
.replace("$channelId", channel.id || "")
.replace("$channel", channel.isDM() ? "" : `<#${channel.id}>`)
2020-02-23 19:40:36 +01:00
.replace("$serverId", guild.id || "")
.replace("$serverName", guild.name || "")
2020-02-23 18:55:20 +01:00
.replace("$hour", settings.forceZeros && hour < 10 ? "0" + hour : hour)
.replace("$minute", minute < 10 ? "0" + minute : minute)
.replace("$second", second < 10 ? "0" + second : second)
.replace("$msecond", settings.forceZeros ? (msecond < 10 ? "00" + msecond : (msecond < 100 ? "0" + msecond : msecond)) : msecond)
.replace("$timemode", timemode)
.replace("$weekdayL", timestamp.toLocaleDateString(languageId, {weekday: "long"}))
.replace("$weekdayS", timestamp.toLocaleDateString(languageId, {weekday: "short"}))
.replace("$monthnameL", timestamp.toLocaleDateString(languageId, {month: "long"}))
.replace("$monthnameS", timestamp.toLocaleDateString(languageId, {month: "short"}))
.replace("$day", settings.forceZeros && day < 10 ? "0" + day : day)
.replace("$month", settings.forceZeros && month < 10 ? "0" + month : month)
.replace("$year", timestamp.getFullYear())
2020-07-01 16:37:43 +02:00
.replace("$quote", quotedLines || "")
2020-08-28 16:05:49 +02:00
.replace("$rawQuote", unquotedLines.join("\n") || "")
.replace(/\$/g, "$$$$");
2020-02-23 18:55:20 +01:00
}
addLeadingZeros (timestring) {
2020-06-08 20:31:00 +02:00
let charArray = timestring.split("");
2020-02-23 18:55:20 +01:00
let numreg = /[0-9]/;
2020-06-08 20:31:00 +02:00
for (let i = 0; i < charArray.length; i++) if (!numreg.test(charArray[i-1]) && numreg.test(charArray[i]) && !numreg.test(charArray[i+1])) charArray[i] = "0" + charArray[i];
2020-02-23 18:55:20 +01:00
2020-06-08 20:31:00 +02:00
return charArray.join("");
2020-02-23 18:55:20 +01:00
}
forceUpdateAll() {
2020-06-08 20:31:00 +02:00
settings = BDFDB.DataUtils.get(this, "settings");
2020-07-01 16:37:43 +02:00
formats = Object.assign({"Standard": "$quote $mention"}, BDFDB.DataUtils.load(this, "formats"));
2020-06-08 20:31:00 +02:00
2020-09-11 19:31:36 +02:00
BDFDB.PatchUtils.forceAllUpdates(this);
2020-02-23 18:55:20 +01:00
BDFDB.MessageUtils.rerenderAll();
}
}
2020-06-10 19:21:54 +02:00
})();
2020-07-26 16:39:51 +02:00
module.exports = CustomQuoter;