User:MonkeysHK/common.js

// https://hypixel-skyblock.fandom.com/wiki/Template:User_rights_request

/* Code to tag userpages with staff message boxes */

/* jshint esversion: 5, forin: true, immed: true, indent: 4, latedef: true, newcap: true, noarg: true, undef: true, undef: true, unused: true, browser: true, jquery: true, onevar: true, eqeqeq: true, multistr: true, maxerr: 999999, -W082, -W084 /* global mw */

"use strict"; $.when(	mw.loader.using(["mediawiki.util", "mediawiki.api"]),	$.Deferred(function(def) { if (mw.libs.QDmodal) { def.resolve(mw.libs.QDmodal); }		else { $.ajax({				cache: true,				dataType: "script",				url: "https://dev.fandom.com/load.php?mode=articles&only=scripts&articles=MediaWiki:QDmodal.js"			}).done(function {				def.resolve(mw.libs.QDmodal);			}); }	}),	$.Deferred(function(def) { if (mw.libs.StaffUtil) { def.resolve(mw.libs.StaffUtil); }		else { $.ajax({				cache: true,				dataType: "script",				url: "https://hypixel-skyblock.fandom.com/wiki/MediaWiki:Gadget-StaffColorsUpdater.js"			}).done(function {				def.resolve(mw.libs.StaffUtil);			}); }	}) ).then(function {	if (!/sysop|soap|staff|helper|wiki-manager|content-team-member|util/.test(mw.config.get('wgUserGroups').join('\n')) || (window.StaffTagger && window.StaffTagger.Loaded))		return;	window.StaffTagger = window.StaffTagger || {};	window.StaffTagger.Loaded = true;	var StaffTagger, that;	StaffTagger = that = window.StaffTagger = Object.assign(this, { processInformation: function { var rank = $("#stafftagger-rank").val; // array of ranks selected var user = $("#stafftagger-user").val; // var rank2; if (rank.match(/.+?\s*[\,\|\;]\s*.+/)) { rank = rank.split(/\s*[\,\|\;]\s*/); rank2 = rank[1]; rank = rank[0]; } else { rank2 = null; rank = rank; }			new mw.Api.post({				action: 'edit',				minor: true,				bot: true,				watchlist: 'nochange',				title: 'User:' + user,				summary: 'Adding message box to userpage',				prependtext: '',				token: mw.user.tokens.values.editToken,			}).always(function(data) {				if (data.edit) {					console.log("[StaffTagger]: Successfully tagged", user, 'With rank "' + rank + '"');					new mw.Api.post({ action: 'protect', bot: true, watchlist: 'nochange', title: 'User:' + user, protections: (								rank2 								? rank2 !== "threadmoderator" && rank2 !== "rollback" && rank2 !== "bot" 								: rank !== "threadmoderator" && rank !== "rollback" && rank !== "bot"							) ? 'edit=sysop|move=sysop' : 'edit=autoconfirmed|move=autoconfirmed', expiry: 'infinite', reason: 'Protecting wiki staff userpage', token: mw.user.tokens.values.editToken, }).always(function(data) { if (data.protect) { console.log("[StaffTagger]: Successfully protected ", user + '\'s userpage'); new mw.Api.get({									action: 'query',									list: 'users',									ususers: user,									ustoken: 'userrights',								}).always(function(data) {									var usToken = data.query.users[0].userrightstoken;									if (data.query) {										new mw.Api.post({ "action": "userrights", "format": "json", "user": user, "add": rank + (rank2 ? "|" + rank2 : ""), "expiry": "infinite", "reason": "Per request to be staff: Hypixel_SkyBlock_Wiki_talk:Requests_to_be_staff", "token": usToken, }).always(function(data) { if (data.userrights) { console.log('[StaffTagger]: Successfully added ranks!'); if (rank2) { new mw.Api.post({														"action": "userrights",														"format": "json",														"user": user,														"add": rank + (rank2 ? "|" + rank2 : ""),														"expiry": "infinite",														"reason": "Per request to be staff: Hypixel_SkyBlock_Wiki_talk:Requests_to_be_staff",														"token": usToken,													}).always(function(data) {														if (data.userrights) {															console.log('[StaffTagger]: Successfully added second rank!');														} else {															console.warn('[StaffTagger]: Failed to add ranks: ' + data);														}													}); new mw.Api.post({														action: "edit",														section: that.getSection(rank),														appendtext: '\n*',														token: mw.user.tokens.values.editToken,														title: 'Hypixel_SkyBlock_Wiki:Staff',														summary: 'Adding ' + user + ' to the staff list per request to be staff: Hypixel_SkyBlock_Wiki_talk:Requests_to_be_staff",														minor: 1,														bot: 1,														watchlist: 'nochange',													}).always(function(data) {														if (data.edit) {															console.log('[StaffTagger]: Successfully added ' + user + ' to staff member list');															new mw.Api.post({ action: "edit", section: that.getSection(rank2), appendtext: '\n*', token: mw.user.tokens.values.editToken, title: 'Hypixel_SkyBlock_Wiki:Staff', summary: 'Adding ' + user + ' to the staff list per request to be staff: Hypixel_SkyBlock_Wiki_talk:Requests_to_be_staff", minor: 1, bot: 1, watchlist: 'nochange', }).always(function(data) { if (data.userrights) { console.log('[StaffTagger]: Successfully added ' + user + ' to staff member list'); mw.hook('hsw.gadget.staffColorsUpdater').add(function(updater) {																		updater;																		console.log('[StaffTagger]: Updating Staff colors...');																	}); mw.hook('hsw.gadget.staffColorsUpdater').add(console.log); } else { console.warn('[StaffTagger]: Failed to Add user to staff member list: ' + data); }															});														} else {															console.warn('[StaffTagger]: Failed to Add user to staff member list: ' + data);														}													}); } else { new mw.Api.post({														action: "edit",														section: that.getSection(rank),														appendtext: '\n*',														token: mw.user.tokens.values.editToken,														title: 'Hypixel_SkyBlock_Wiki:Staff',														summary: 'Adding ' + user + ' to the staff list per request to be staff: Hypixel_SkyBlock_Wiki_talk:Requests_to_be_staff",														minor: 1,														bot: 1,														watchlist: 'nochange',													}).always(function(data) {														if (data.edit) {															console.log('[StaffTagger]: Successfully added ' + user + ' to staff member list');														} else {															console.warn('[StaffTagger]: Failed to Add user to staff member list: ' + data);														}													}); }											} else { console.warn('[StaffTagger]: Failed to give ranks: ' + data); }										});									} else {										console.warn("[StaffTagger]: API error in getting user rights token: " + data, '(API Error code \"' + data + '\")');									}								}); } else { console.warn("[StaffTagger]: API error in protecting ", user + '\'s userpage:', data, '(API Error code \"' + data + '\")'); }					});				} else {					console.warn('[StaffTagger]: API Error in Tagging', user + ':', data, '(API Error code "' + data + '")');				}			}); },		modal: new mw.libs.QDmodal("StaffTagger"), getSection: function (rank) { var matches = { 'bureaucrat': 4, 'sysop': 5, 'codeeditor': 6, 'content-moderator': 8, 'threadmoderator': 10, 'rollback': 11, };			return matches[rank] || null; },		editorCloseHandler: function { this.modal.hide; },		main: function(user) { this.modal.show({				title: "Staff Tagger",				buttons: [					{						text: "Confirm",						handler: this.processInformation.bind(this),					},					{						text: "Close",						handler: this.editorCloseHandler.bind(this),					},				],			}); },		init: function { $('span#tag-user > a[href*="prepend="].text').click(function(e) {				e.preventDefault;				that.main($(this).parent.attr('data-user'));			}); if (mw.config.get("wgCanonicalNamespace") === mw.config.get("wgFormattedNamespaces")[2]) { $('.page-header__contribution-buttons .wds-list, .page-header__actions .wds-list').first.append(					$('').append( $('', {							text: "Staff Tagger",							href: "#",							click: that.main,						}) )				);			}		}	}); });