BetterDiscordAddons/Plugins/LastMessageDate/LastMessageDate.plugin.js

372 lines
14 KiB
JavaScript
Raw Normal View History

2020-10-20 23:25:34 +02:00
/**
* @name LastMessageDate
2021-03-05 13:26:41 +01:00
* @author DevilBro
2020-10-20 23:25:34 +02:00
* @authorId 278543574059057154
2022-08-31 12:25:50 +02:00
* @version 1.3.1
2021-03-05 13:26:41 +01:00
* @description Displays the Last Message Date of a Member for the current Server/DM in the UserPopout and UserModal
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/LastMessageDate/
2021-03-10 09:17:37 +01:00
* @updateUrl https://mwittrien.github.io/BetterDiscordAddons/Plugins/LastMessageDate/LastMessageDate.plugin.js
2020-10-20 23:25:34 +02:00
*/
2019-02-20 10:19:22 +01: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]) => {
2021-06-30 23:25:27 +02:00
var _this;
2021-07-05 21:34:35 +02:00
var loadedUsers, requestedUsers, queuedInstances, languages;
2021-06-08 23:14:34 +02:00
var currentPopout, currentProfile;
2020-09-19 20:49:33 +02:00
2020-10-09 21:09:35 +02:00
return class LastMessageDate extends Plugin {
2021-01-06 12:38:36 +01:00
onLoad () {
2021-06-30 23:25:27 +02:00
_this = this;
2020-09-19 20:49:33 +02:00
loadedUsers = {};
requestedUsers = {};
2021-07-05 21:34:35 +02:00
queuedInstances = {};
2019-02-20 10:19:22 +01:00
2020-09-19 20:49:33 +02:00
this.defaults = {
2021-03-15 13:28:17 +01:00
general: {
displayText: {value: true, description: "Display '{{presuffix}}' in the Date"}
2020-09-19 20:49:33 +02:00
},
2021-03-15 13:28:17 +01:00
places: {
userPopout: {value: true, description: "User Popouts"},
userProfile: {value: true, description: "User Profile Modal"}
2020-09-19 20:49:33 +02:00
},
2021-03-15 13:28:17 +01:00
dates: {
lastMessageDate: {value: {}, description: "Last Message Date"},
2020-09-19 20:49:33 +02:00
}
};
2020-04-14 18:27:51 +02:00
2020-09-19 20:49:33 +02:00
this.patchedModules = {
after: {
2022-08-31 12:25:50 +02:00
UserPopoutExperimentWrapper: "default",
2021-06-30 23:25:27 +02:00
UserPopoutContainer: "type",
2022-08-31 12:25:50 +02:00
UsernameSection: "default",
2021-06-25 10:14:18 +02:00
UserPopoutInfo: "UserPopoutInfo",
2021-06-30 23:25:27 +02:00
UserProfileModal: "default",
2021-06-08 23:14:34 +02:00
UserProfileModalHeader: "default"
2020-09-19 20:49:33 +02:00
}
};
2020-04-14 18:27:51 +02:00
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
onStart () {
2022-08-04 16:35:10 +02:00
BDFDB.PatchUtils.patch(this, BDFDB.LibraryModules.DispatchApiUtils, "dispatch", {after: e => {
2022-08-01 22:55:52 +02:00
if (BDFDB.ObjectUtils.is(e.methodArguments[0]) && e.methodArguments[0].type == "MESSAGE_CREATE" && e.methodArguments[0].message) {
2020-04-14 18:27:51 +02:00
let message = e.methodArguments[0].message;
let guildId = message.guild_id || message.channel_id;
if (guildId && loadedUsers[guildId] && loadedUsers[guildId][message.author.id]) {
loadedUsers[guildId][message.author.id] = new Date(message.timestamp);
}
}
}});
2019-02-20 10:19:22 +01:00
2021-03-15 13:28:17 +01:00
BDFDB.PatchUtils.forceAllUpdates(this);
2020-04-14 18:27:51 +02:00
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
onStop () {
2021-03-15 13:28:17 +01:00
BDFDB.PatchUtils.forceAllUpdates(this);
2020-04-14 18:27:51 +02:00
}
2019-02-20 10:19:22 +01:00
2020-09-19 20:49:33 +02:00
getSettingsPanel (collapseStates = {}) {
2021-03-15 13:28:17 +01:00
let settingsPanel;
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, {
2020-09-19 20:49:33 +02:00
collapseStates: collapseStates,
2021-03-15 13:28:17 +01:00
children: _ => {
let settingsItems = [];
settingsItems.push(Object.keys(this.defaults.general).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch",
plugin: this,
keys: ["general", key],
label: key == "displayText" ? this.defaults.general[key].description.replace("{{presuffix}}", this.labels.last_message.replace("{{time}}", "").trim()) : this.defaults.general[key].description,
value: this.settings.general[key]
})));
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormDivider, {
className: BDFDB.disCN.marginbottom8
}));
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelList, {
title: "Add Date in:",
children: Object.keys(this.defaults.places).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch",
plugin: this,
keys: ["places", key],
label: this.defaults.places[key].description,
value: this.settings.places[key]
}))
}));
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormDivider, {
className: BDFDB.disCN.marginbottom8
}));
settingsItems.push(Object.keys(this.defaults.dates).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.DateInput, Object.assign({}, this.settings.dates[key], {
label: this.defaults.dates[key].description,
prefix: _ => (this.settings.general.displayText && this.labels.last_message.split("{{time}}")[0] || "").trim(),
suffix: _ => (this.settings.general.displayText && this.labels.last_message.split("{{time}}")[1] || "").trim(),
onChange: valueObj => {
this.SettingsUpdated = true;
this.settings.dates[key] = valueObj;
BDFDB.DataUtils.save(this.settings.dates, this, "dates");
}
}))));
return settingsItems.flat(10);
}
});
2020-09-19 20:49:33 +02:00
}
2019-02-20 10:19:22 +01:00
2021-01-06 12:38:36 +01:00
onSettingsClosed () {
2020-09-19 20:49:33 +02:00
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
2021-03-15 13:28:17 +01:00
BDFDB.PatchUtils.forceAllUpdates(this);
2020-09-19 20:49:33 +02:00
}
}
2020-07-11 23:28:30 +02:00
2022-08-31 12:25:50 +02:00
processUserPopoutExperimentWrapper (e) {
currentPopout = e.instance;
}
2021-06-30 23:25:27 +02:00
processUserPopoutContainer (e) {
currentPopout = e.instance;
2021-05-21 16:12:36 +02:00
}
2021-06-30 23:25:27 +02:00
2022-08-31 12:25:50 +02:00
processUsernameSection (e) {
if (currentPopout && e.instance.props.user && this.settings.places.userPopout) {
let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, {name: ["CopiableField", "ColoredFluxTag"]});
if (index > -1) this.injectDate(children, index + 1, e.instance.props.user, currentPopout.props.guildId);
}
}
2021-06-03 16:27:44 +02:00
processUserPopoutInfo (e) {
2021-06-08 23:14:34 +02:00
if (currentPopout && e.instance.props.user && this.settings.places.userPopout) {
2021-06-25 10:14:18 +02:00
let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, {name: ["DiscordTag", "ColoredFluxTag"]});
2021-06-30 23:25:27 +02:00
if (index > -1) this.injectDate(children, index + 1, e.instance.props.user, currentPopout.props.guildId);
2020-09-19 20:49:33 +02:00
}
2020-04-14 18:27:51 +02:00
}
2019-02-22 21:39:10 +01:00
2021-06-30 23:25:27 +02:00
processUserProfileModal (e) {
currentProfile = e.instance;
}
2021-06-08 23:14:34 +02:00
processUserProfileModalHeader (e) {
if (currentProfile && e.instance.props.user && this.settings.places.userProfile) {
let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, {name: ["DiscordTag", "ColoredFluxTag"]});
2021-06-30 23:25:27 +02:00
if (index > -1) this.injectDate(children, index + 1, e.instance.props.user, currentProfile.props.guildId);
2020-09-19 20:49:33 +02:00
}
2020-04-14 18:27:51 +02:00
}
2019-02-20 10:19:22 +01:00
2021-07-05 21:24:43 +02:00
injectDate (children, index, user, guildId) {
2022-09-27 16:53:10 +02:00
if (!guildId) guildId = BDFDB.LibraryStores.SelectedGuildStore.getGuildId();
2021-07-05 21:16:01 +02:00
if (!BDFDB.ArrayUtils.is(children) || !user || user.isNonUserBot()) return;
2020-09-19 20:49:33 +02:00
let isGuild = guildId && guildId != BDFDB.DiscordConstants.ME;
2022-09-27 20:19:49 +02:00
guildId = isGuild ? guildId : BDFDB.LibraryStores.SelectedChannelStore.getChannelId();
2020-09-19 20:49:33 +02:00
if (!guildId) return;
2021-06-30 23:25:27 +02:00
2020-09-19 20:49:33 +02:00
if (!loadedUsers[guildId]) loadedUsers[guildId] = {};
if (!requestedUsers[guildId]) requestedUsers[guildId] = {};
2021-07-05 21:34:35 +02:00
if (!queuedInstances[guildId]) queuedInstances[guildId] = {};
2021-06-30 23:25:27 +02:00
2021-07-05 21:34:35 +02:00
if (loadedUsers[guildId][user.id] === undefined && !requestedUsers[guildId][user.id]) {
requestedUsers[guildId][user.id] = true;
queuedInstances[guildId][user.id] = [].concat(queuedInstances[guildId][user.id]).filter(n => n);
2021-01-10 15:47:51 +01:00
BDFDB.LibraryModules.APIUtils.get({
url: isGuild ? BDFDB.DiscordConstants.Endpoints.SEARCH_GUILD(guildId) : BDFDB.DiscordConstants.Endpoints.SEARCH_CHANNEL(guildId),
query: BDFDB.LibraryModules.APIEncodeUtils.stringify({author_id: user.id})
}).then(result => {
2021-07-05 21:34:35 +02:00
delete requestedUsers[guildId][user.id];
2020-09-19 20:49:33 +02:00
if (typeof result.body.retry_after != "number") {
if (result.body.messages && Array.isArray(result.body.messages[0])) {
2021-06-30 23:25:27 +02:00
for (let message of result.body.messages[0]) if (message.hit && message.author.id == user.id) loadedUsers[guildId][user.id] = new Date(message.timestamp);
2020-04-14 18:27:51 +02:00
}
2020-09-19 20:49:33 +02:00
else loadedUsers[guildId][user.id] = null;
2021-07-05 21:34:35 +02:00
BDFDB.ReactUtils.forceUpdate(queuedInstances[guildId][user.id]);
delete queuedInstances[guildId][user.id];
2020-09-19 20:49:33 +02:00
}
2021-07-05 21:24:43 +02:00
else BDFDB.TimeUtils.timeout(_ => this.injectDate(children, index, user, guildId), result.body.retry_after + 500);
2020-09-19 20:49:33 +02:00
});
}
2021-06-30 23:25:27 +02:00
children.splice(index, 0, BDFDB.ReactUtils.createElement(class extends BDFDB.ReactUtils.Component {
render() {
if (loadedUsers[guildId][user.id] === undefined) {
2021-07-05 21:34:35 +02:00
if (queuedInstances[guildId][user.id].indexOf(this) == -1) queuedInstances[guildId][user.id].push(this);
2021-06-30 23:25:27 +02:00
return null;
}
else {
let timestamp = loadedUsers[guildId][user.id] ? BDFDB.LibraryComponents.DateInput.format(_this.settings.dates.lastMessageDate, loadedUsers[guildId][user.id]) : "---";
return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextScroller, {
className: BDFDB.disCNS._lastmessagedatedate + BDFDB.disCNS.userinfodate + BDFDB.disCN.textrow,
children: _this.settings.general.displayText ? _this.labels.last_message.replace("{{time}}", timestamp) : timestamp
});
}
}
}));
2019-02-22 21:39:10 +01:00
}
2021-03-10 11:38: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 {
last_message: "Последно съобщение на {{time}}"
2020-09-19 20:49:33 +02:00
};
2021-07-05 18:38:04 +02:00
case "cs": // Czech
return {
last_message: "Poslední zpráva v {{time}}"
};
case "da": // Danish
2020-09-19 20:49:33 +02:00
return {
last_message: "Sidste besked den {{time}}"
2020-09-19 20:49:33 +02:00
};
case "de": // German
2020-09-19 20:49:33 +02:00
return {
last_message: "Letzte Nachricht am {{time}}"
2020-09-19 20:49:33 +02:00
};
case "el": // Greek
2020-09-19 20:49:33 +02:00
return {
last_message: "Τελευταίο μήνυμα στις {{time}}"
2020-09-19 20:49:33 +02:00
};
case "es": // Spanish
2020-09-19 20:49:33 +02:00
return {
last_message: "Último mensaje el {{time}}"
2020-09-19 20:49:33 +02:00
};
case "fi": // Finnish
2020-09-19 20:49:33 +02:00
return {
last_message: "Viimeinen viesti {{time}}"
2020-09-19 20:49:33 +02:00
};
case "fr": // French
2020-09-19 20:49:33 +02:00
return {
last_message: "Dernier message le {{time}}"
2020-09-19 20:49:33 +02:00
};
2021-07-05 18:38:04 +02:00
case "hi": // Hindi
return {
last_message: "अंतिम संदेश {{time}} को"
};
case "hr": // Croatian
2020-09-19 20:49:33 +02:00
return {
last_message: "Posljednja poruka {{time}}"
2020-09-19 20:49:33 +02:00
};
case "hu": // Hungarian
2020-09-19 20:49:33 +02:00
return {
last_message: "Utolsó üzenet: {{time}}"
2020-09-19 20:49:33 +02:00
};
case "it": // Italian
2020-09-19 20:49:33 +02:00
return {
last_message: "Ultimo messaggio il {{time}}"
2020-09-19 20:49:33 +02:00
};
case "ja": // Japanese
2020-09-19 20:49:33 +02:00
return {
last_message: "{{time}}の最後のメッセージ"
2020-09-19 20:49:33 +02:00
};
case "ko": // Korean
2020-09-19 20:49:33 +02:00
return {
last_message: "{{time}}의 마지막 메시지"
2020-09-19 20:49:33 +02:00
};
case "lt": // Lithuanian
2020-09-19 20:49:33 +02:00
return {
last_message: "Paskutinis pranešimas {{time}}"
2020-09-19 20:49:33 +02:00
};
case "nl": // Dutch
2020-09-19 20:49:33 +02:00
return {
last_message: "Laatste bericht op {{time}}"
2020-09-19 20:49:33 +02:00
};
case "no": // Norwegian
2020-09-19 20:49:33 +02:00
return {
last_message: "Siste melding {{time}}"
2020-09-19 20:49:33 +02:00
};
case "pl": // Polish
2020-09-19 20:49:33 +02:00
return {
last_message: "Ostatnia wiadomość {{time}}"
2020-09-19 20:49:33 +02:00
};
case "pt-BR": // Portuguese (Brazil)
2020-09-19 20:49:33 +02:00
return {
last_message: "Última mensagem em {{time}}"
2020-09-19 20:49:33 +02:00
};
case "ro": // Romanian
2020-09-19 20:49:33 +02:00
return {
last_message: "Ultimul mesaj pe {{time}}"
2020-09-19 20:49:33 +02:00
};
case "ru": // Russian
2020-09-19 20:49:33 +02:00
return {
last_message: "Последнее сообщение в {{time}}"
2020-09-19 20:49:33 +02:00
};
case "sv": // Swedish
2020-09-19 20:49:33 +02:00
return {
last_message: "Senaste meddelandet {{time}}"
2020-09-19 20:49:33 +02:00
};
case "th": // Thai
2020-09-19 20:49:33 +02:00
return {
last_message: "ข้อความล่าสุดเมื่อ {{time}}"
};
case "tr": // Turkish
return {
last_message: "{{time}} tarihindeki son mesaj"
};
case "uk": // Ukrainian
return {
last_message: "Останнє повідомлення {{time}}"
};
case "vi": // Vietnamese
return {
last_message: "Tin nhắn cuối cùng vào {{time}}"
};
2021-01-15 17:54:22 +01:00
case "zh-CN": // Chinese (China)
return {
last_message: "{{time}}上的最后一条消息"
};
2021-01-15 17:54:22 +01:00
case "zh-TW": // Chinese (Taiwan)
return {
last_message: "{{time}}上的最後一條消息"
};
default: // English
return {
2021-05-29 10:56:59 +02:00
last_message: "Last Message on {{time}}"
2020-09-19 20:49:33 +02:00
};
}
}
};
2022-09-01 14:40:11 +02:00
})(window.BDFDB_Global.PluginUtils.buildPlugin(changeLog));
2020-09-19 20:49:33 +02:00
})();