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 (!/codeeditor|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 = $("#StaffTaggerModal-rank").val; // array of ranks selected            var user = $("#StaffTaggerModal-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; }

// #1: Add staff message box to userpage 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 + '"');

// #2: Protect staff userpage 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');

// #3: Granting rights (Bureaucrat only) 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) { // Still #3 new mw.Api.post({                                       "action": "userrights",                                        "format": "json",                                        "user": user,                                        "add": rank + (rank2 ? "|" + rank2 : ""), // using rank code (e.g. "rollback" "threadmoderator") separated with the pipe character "|"                                       "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!');

// #4: Change Staff Data on Staff Page // Now should update Module:Staff/Members instead 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"),        editorCloseHandler: function  {            that.modal.hide;        },

createIntroSection: function { return ```Select the ranks to grant this user. Then, the Staff Tagger will perform the following:  Add StaffMsgBox to the user page Protect the user page Grant selected rights to the user on wiki (when applicable) Change staff data on Module:Staff/Members   ```       },

rolesAvailable: [ "bureaucrat", "administrator", "codeeditor", "content-moderator", "threadmoderator", "rollback", ],

createRolesSelection: function { return that.rolesAvailable.map(function(role) {               var name = "StaffTaggerModal-role-" + role                return {                    type: "div",                    attr: { id: "StaffTaggerModal-role-wrapper" },                    children: [{                        type: "input",                        attr: { type: "checkbox", id: name, "data-role": role },                    }, {                        type: "label",                        attr: { for: name },                        text: role,                    }],                }            }) },

showModal: function { that.modal.setContent({ children: [{				type: "form",				attr: {					id: "StaffTaggerModal-form",				},				children: [{					type: "span",					html: that.createIntroSection,				}, {					type: "div",					html: i18n.msg("current-model", this.MODELS[this.wg.wgPageContentModel]).parse,				}, {					type: "div",					id: "StaffTaggerModal-input",					children: [{						type: "div",						attr: { id: "StaffTaggerModal-user-wrapper" },						children: [{							type: "label",							id: "StaffTaggerModal-user-label",							text: "User:" + " ",						}, {							type: "div",							attr: { id: "StaffTaggerModal-user" },							children: [{                               type: "label",                                id: "StaffTaggerModal-user-label",                                text: "User: ",                            }, {                                type: "input",                                attr: { id: "StaffTaggerModal-user-input", type: "text", value: that.user || "" }, }]						}],					}, {						type: "div", attr: { id: "StaffTaggerModal-roles-wrapper" }, children: this.createRolesSelection, }],				}],			}] });           that.modal.show({ title: "Staff Tagger", buttons: [{ text: "Confirm", handler: that.processInformation, },                   {                        text: "Close", handler: that.editorCloseHandler, },               ],            });        },        init: function  {            $('span#tag-user > a[href*="prepend="].text').click(function (e) { that.user = $(this).parent.attr('data-user'); e.preventDefault; that.showModal(user); });           if (mw.config.get("wgNamespaceNumber") === 2) {                that.user = mw.config.get("wgPageName").match(mw.config.get("wgFormattedNamespaces")[2] + ":([^/]*)")[1];                $('.page-header__contribution-buttons .wds-list, .page-header__actions .wds-list').first.append( $('').append(                       $('', { text: "Staff Tagger", href: "#", click: that.showModal, })                   )                );            }        }    });    that.init; });