BetterDiscordAddons/Plugins/RemoveNicknames/RemoveNicknames.plugin.js

267 lines
13 KiB
JavaScript
Raw Normal View History

2020-10-20 23:25:34 +02:00
/**
* @name RemoveNicknames
* @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/RemoveNicknames
* @source https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/RemoveNicknames/RemoveNicknames.plugin.js
2020-11-12 17:30:48 +01:00
* @updateUrl https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/RemoveNicknames/RemoveNicknames.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": "RemoveNicknames",
"author": "DevilBro",
2020-12-04 20:10:40 +01:00
"version": "1.3.4",
2020-10-16 10:25:30 +02:00
"description": "Replace all nicknames with the actual accountnames"
2020-10-15 13:08:07 +02:00
},
"changeLog": {
2020-12-04 20:10:40 +01:00
"improved": {
"Reactions": "Removes the nicknames in the tooltip of a reaction in the messages window"
2020-10-15 13:08:07 +02: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 {
2020-09-19 20:49:33 +02:00
getName () {return config.info.name;}
getAuthor () {return config.info.author;}
getVersion () {return config.info.version;}
getDescription () {return config.info.description;}
2020-10-09 21:09:35 +02: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;
BdApi.showConfirmationModal("Library Missing", `The library plugin needed for ${config.info.name} is missing. Please click "Download Now" to install it.`, {
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;
require("request").get("https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js", (e, r, b) => {
2020-10-20 23:25:34 +02:00
if (!e && b && b.indexOf(`* @name BDFDB`) > -1) require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0BDFDB.plugin.js"), b, _ => {});
2020-09-20 08:15:13 +02:00
else BdApi.alert("Error", "Could not download BDFDB library plugin, try again some time later.");
});
}
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
}
start() {this.load();}
stop() {}
2020-11-28 23:12:09 +01:00
getSettingsPanel() {
let template = document.createElement("template");
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>`;
template.content.firstElementChild.querySelector("a").addEventListener("click", _ => {
require("request").get("https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js", (e, r, b) => {
if (!e && b && b.indexOf(`* @name BDFDB`) > -1) require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0BDFDB.plugin.js"), b, _ => {});
else BdApi.alert("Error", "Could not download BDFDB library plugin, try again some time later.");
});
});
return template.content.firstElementChild;
}
2020-10-09 21:09:35 +02:00
} : (([Plugin, BDFDB]) => {
2020-09-19 20:49:33 +02:00
var settings = {};
2020-06-09 10:26:04 +02:00
2020-10-09 21:09:35 +02:00
return class RemoveNicknames extends Plugin {
2020-09-19 20:49:33 +02:00
onLoad() {
this.defaults = {
settings: {
2020-11-21 17:43:43 +01:00
replaceOwn: {value: false, inner: false, description: "Replace your own name: "},
replaceBots: {value: true, inner: false, description: "Replace the nickname of bots: "},
addNickname: {value: false, inner: false, description: "Add nickname as parentheses: "},
swapPositions: {value: false, inner: false, description: "Swap the position of username and nickname: "},
2020-11-19 16:51:14 +01:00
changeInChatWindow: {value: true, inner: true, description: "Messages"},
2020-12-04 20:10:40 +01:00
changeInReactions: {value: true, inner: true, description: "Reactions"},
2020-11-19 16:51:14 +01:00
changeInMentions: {value: true, inner: true, description: "Mentions"},
changeInVoiceChat: {value: true, inner: true, description: "Voice Channels"},
changeInMemberList: {value: true, inner: true, description: "Member List"},
changeInTyping: {value: true, inner: true, description: "Typing List"},
changeInAutoComplete: {value: true, inner: true, description: "Autocomplete Menu"}
2020-09-19 20:49:33 +02:00
}
};
2020-02-04 08:20:40 +01:00
2020-09-19 20:49:33 +02:00
this.patchedModules = {
before: {
AutocompleteUserResult: "render",
VoiceUser: "render",
MemberListItem: "render",
Message: "default",
2020-11-21 17:43:43 +01:00
MessageUsername: "default",
2020-12-04 20:10:40 +01:00
MessageContent: "type",
2020-09-19 20:49:33 +02:00
},
after: {
2020-10-15 13:08:07 +02:00
TypingUsers: "render",
2020-12-04 20:10:40 +01:00
Reaction: "render",
2020-12-04 22:16:16 +01:00
Mention: "default",
UserMention: "UserMention"
2020-09-19 20:49:33 +02:00
}
};
}
2020-02-04 08:20:40 +01:00
2020-09-19 20:49:33 +02:00
onStart() {
this.forceUpdateAll();
2018-10-11 10:21:26 +02:00
}
2020-09-19 20:49:33 +02:00
onStop() {
2020-02-04 08:20:40 +01:00
this.forceUpdateAll();
}
2018-10-11 10:21:26 +02:00
2020-09-19 20:49:33 +02:00
getSettingsPanel (collapseStates = {}) {
let settingsPanel, settingsItems = [], innerItems = [];
2020-02-04 08:20:40 +01:00
2020-09-19 20:49:33 +02:00
for (let key in settings) (!this.defaults.settings[key].inner ? settingsItems : innerItems).push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch",
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key]
}));
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelInner, {
title: "Remove Nicknames in:",
first: settingsItems.length == 0,
last: true,
children: innerItems
}));
2020-02-04 08:20:40 +01:00
2020-09-19 20:49:33 +02:00
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, settingsItems);
2020-02-04 08:20:40 +01:00
}
2018-10-11 10:21:26 +02:00
2020-09-19 20:49:33 +02:00
onSettingsClosed (e) {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
this.forceUpdateAll();
}
}
forceUpdateAll () {
settings = BDFDB.DataUtils.get(this, "settings");
BDFDB.PatchUtils.forceAllUpdates(this);
BDFDB.MessageUtils.rerenderAll();
2020-02-04 08:20:40 +01:00
}
2019-01-26 22:45:19 +01:00
2020-09-19 20:49:33 +02:00
processAutocompleteUserResult (e) {
if (e.instance.props.user && e.instance.props.nick && settings.changeInAutoComplete) {
let newName = this.getNewName(e.instance.props.user);
if (newName) e.instance.props.nick = newName;
}
2020-02-04 08:20:40 +01:00
}
2019-01-26 22:45:19 +01:00
2020-09-19 20:49:33 +02:00
processVoiceUser (e) {
if (e.instance.props.user && e.instance.props.nick && settings.changeInVoiceChat) {
let newName = this.getNewName(e.instance.props.user);
if (newName) e.instance.props.nick = newName;
}
2020-02-04 08:20:40 +01:00
}
2019-01-26 22:45:19 +01:00
2020-09-19 20:49:33 +02:00
processMemberListItem (e) {
if (e.instance.props.user && e.instance.props.nick && settings.changeInMemberList) {
let newName = this.getNewName(e.instance.props.user);
if (newName) e.instance.props.nick = newName;
}
2020-02-04 08:20:40 +01:00
}
2019-01-26 22:45:19 +01:00
2020-09-19 20:49:33 +02:00
processTypingUsers (e) {
if (BDFDB.ObjectUtils.is(e.instance.props.typingUsers) && Object.keys(e.instance.props.typingUsers).length && settings.changeInTyping) {
let users = Object.keys(e.instance.props.typingUsers).filter(id => id != BDFDB.UserUtils.me.id).filter(id => !BDFDB.LibraryModules.FriendUtils.isBlocked(id)).map(id => BDFDB.LibraryModules.UserStore.getUser(id)).filter(user => user);
if (users.length) {
let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, {props: [["className", BDFDB.disCN.typingtext]]});
if (index > -1 && BDFDB.ArrayUtils.is(children[index].props.children)) for (let child of children[index].props.children) if (child.type == "strong") {
let newName = this.getNewName(users.shift());
if (newName) BDFDB.ReactUtils.setChild(child, newName);
}
2019-03-22 20:49:46 +01:00
}
}
}
2020-02-04 08:20:40 +01:00
2020-09-19 20:49:33 +02:00
processMessage (e) {
let header = e.instance.props.childrenHeader;
if (header && header.props && header.props.message && header.props.message.nick) {
let newName = this.getNewName(header.props.message.author);
if (newName) header.props.message = new BDFDB.DiscordObjects.Message(Object.assign({}, header.props.message, {nick: newName}));
}
2020-11-21 17:43:43 +01:00
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 && repliedMessage.props.children.props.referencedMessage.message.nick) {
let newName = this.getNewName(repliedMessage.props.children.props.referencedMessage.message.author);
if (newName) repliedMessage.props.children.props.referencedMessage.message = new BDFDB.DiscordObjects.Message(Object.assign({}, repliedMessage.props.children.props.referencedMessage.message, {nick: newName}));
}
}
processMessageUsername (e) {
if (e.instance.props.message.nick && settings.changeInChatWindow) {
let newName = this.getNewName(e.instance.props.message.author);
if (newName) e.instance.props.message = new BDFDB.DiscordObjects.Message(Object.assign({}, e.instance.props.message, {nick: newName}));
}
2019-05-26 14:29:29 +02:00
}
2020-09-19 20:49:33 +02:00
processMessageContent (e) {
if (e.instance.props.message.type != BDFDB.DiscordConstants.MessageTypes.DEFAULT && e.instance.props.message.nick && settings.changeInChatWindow) {
let newName = this.getNewName(e.instance.props.message.author);
if (newName) {
e.instance.props.message = new BDFDB.DiscordObjects.Message(Object.assign({}, e.instance.props.message, {nick: newName}));
2020-12-04 22:16:16 +01:00
if (e.instance.props.children && e.instance.props.children.props) e.instance.props.children.props.message = e.instance.props.message;
2020-09-19 20:49:33 +02:00
}
2020-02-04 08:20:40 +01:00
}
}
2020-10-15 13:08:07 +02:00
2020-12-04 20:10:40 +01:00
processReaction (e) {
if (e.instance.props.reactions) {
let channel = BDFDB.LibraryModules.ChannelStore.getChannel(e.instance.props.message.channel_id);
let guildId = null == channel || channel.isPrivate() ? null : channel.getGuildId();
let users = e.instance.props.reactions.filter(user => !BDFDB.LibraryModules.FriendUtils.isBlocked(user.id)).slice(0, 3).map(user => this.getNewName(user) || guildId && BDFDB.LibraryModules.MemberStore.getNick(guildId, user.id) || user.username).filter(user => user);
if (users.length) {
let reaction = e.instance.props.message.getReaction(e.instance.props.emoji);
let others = Math.max(0, (reaction && reaction.count || 0) - users.length);
let emojiName = BDFDB.LibraryModules.ReactionEmojiUtils.getReactionEmojiName(e.instance.props.emoji);
e.returnvalue.props.text =
users.length == 1 ? others > 0 ? BDFDB.LanguageUtils.LanguageStringsFormat("REACTION_TOOLTIP_1_N", users[0], others, emojiName) :
BDFDB.LanguageUtils.LanguageStringsFormat("REACTION_TOOLTIP_1", users[0], emojiName) :
users.length == 2 ? others > 0 ? BDFDB.LanguageUtils.LanguageStringsFormat("REACTION_TOOLTIP_2_N", users[0], users[1], others, emojiName) :
BDFDB.LanguageUtils.LanguageStringsFormat("REACTION_TOOLTIP_2", users[0], users[1], emojiName) :
users.length == 3 ? others > 0 ? BDFDB.LanguageUtils.LanguageStringsFormat("REACTION_TOOLTIP_3_N", users[0], users[1], users[2], others, emojiName) :
BDFDB.LanguageUtils.LanguageStringsFormat("REACTION_TOOLTIP_3", users[0], users[1], users[2], emojiName) :
BDFDB.LanguageUtils.LanguageStringsFormat("REACTION_TOOLTIP_N", others, emojiName);
}
}
else BDFDB.LibraryModules.ReactionUtils.getReactions(e.instance.props.message.channel_id, e.instance.props.message.id, e.instance.props.emoji).then(reactions => {
e.instance.props.reactions = reactions;
BDFDB.ReactUtils.forceUpdate(e.instance);
});
}
2020-10-15 13:08:07 +02:00
processMention (e) {
if (e.instance.props.userId && settings.changeInMentions) {
let newName = this.getNewName(BDFDB.LibraryModules.UserStore.getUser(e.instance.props.userId));
if (newName) e.returnvalue.props.children[0] = "@" + newName;
}
}
2020-12-04 22:16:16 +01:00
processUserMention (e) {
if (e.instance.props.id && settings.changeInMentions && typeof e.returnvalue.props.children == "function") {
let newName = this.getNewName(BDFDB.LibraryModules.UserStore.getUser(e.instance.props.id));
if (newName) {
let renderChildren = e.returnvalue.props.children;
e.returnvalue.props.children = (...args) => {
let children = renderChildren(...args);
children.props.children = "@" + newName;
return children;
};
}
}
}
2019-01-26 22:45:19 +01:00
2020-12-04 20:10:40 +01:00
getNewName (user) {
2020-09-19 20:49:33 +02:00
if (!user) return null;
let member = BDFDB.LibraryModules.MemberStore.getMember(BDFDB.LibraryModules.LastGuildStore.getGuildId(), user.id) || {};
2020-12-04 20:10:40 +01:00
let origUser = BDFDB.LibraryModules.UserStore.getUser(user.id) || {};
let EditUsers = BDFDB.BDUtils.getPlugin("EditUsers", true);
let username = EditUsers && EditUsers.getUserData(user, true, false, origUser).username || user.username;
2020-12-04 22:16:41 +01:00
if (!member.nick || user.id == BDFDB.UserUtils.me.id && !settings.replaceOwn || user.bot && !settings.replaceBots) return username != origUser.username ? username : (member.nick || username);
2020-09-19 20:49:33 +02:00
return settings.addNickname ? (settings.swapPositions ? (member.nick + " (" + username + ")") : (username + " (" + member.nick + ")")) : username;
}
};
2020-10-09 21:09:35 +02:00
})(window.BDFDB_Global.PluginUtils.buildPlugin(config));
2020-09-19 20:49:33 +02:00
})();