MediaWiki:DiscordChat.js

/* * Name: DiscordChat * Author: DuckeyD */

/* global mw, $ */ window.DiscordChat = { ready: false, messages: {}, data: null, init: function { window.DiscordChat.ready = true var mwconfig = mw.config.get(['wgUserLanguage']) var api = new mw.Api api.get({     action: 'query',      meta: 'allmessages',      ammessages: ['chat-live2', 'chat-join-the-chat', 'awc-metrics-close', 'Custom-DiscordChat-id'].join('|'),      amlang: mwconfig.wgUserLanguage    }).done(function (data) {      if (data.error) {        return      }

data.query.allmessages.forEach(function (message) {       if (message['*']) {          window.DiscordChat.messages[message.name] = message['*']        }      })

$.get('https://discordapp.com/api/guilds/' + window.DiscordChat.messages['Custom-DiscordChat-id'] + '/widget.json', function (data) {       window.DiscordChat.data = data        if ($('#WikiaRail').length) {          if ($('#WikiaRail').hasClass('loaded')) {            window.DiscordChat.rail          } else {            $('#WikiaRail').on('afterLoad.rail', window.DiscordChat.rail)          }        }      }) }) },  rail: function  {    var i = 0    var appendedAvatars = 0    var avatarContainer = $(' ')    var members = window.DiscordChat.data.members    window.DiscordChat.shuffle(members)

while (appendedAvatars < 5 && i < members.length) { if (!members[i].bot) { avatarContainer.append('') appendedAvatars++ }     i++ }

var element = $('' +                     '' +                        '<path d="M189.5 20h-134C44.2 20 35 29.2 35 40.6v135.2c0 11.4 9.2 20.6 20.5 20.6h113.4l-5.3-18.5 12.8 11.9 12.1 11.2 21.5 19V40.6c0-11.4-9.2-20.6-20.5-20.6zm-38.6 130.6s-3.6-4.3-6.6-8.1c13.1-3.7 18.1-11.9 18.1-11.9-4.1 2.7-8 4.6-11.5 5.9-5 2.1-9.8 3.5-14.5 4.3-9.6 1.8-18.4 1.3-25.9-.1-5.7-1.1-10.6-2.7-14.7-4.3-2.3-.9-4.8-2-7.3-3.4-.3-.2-.6-.3-.9-.5-.2-.1-.3-.2-.4-.3-1.8-1-2.8-1.7-2.8-1.7s4.8 8 17.5 11.8c-3 3.8-6.7 8.3-6.7 8.3-22.1-.7-30.5-15.2-30.5-15.2 0-32.2 14.4-58.3 14.4-58.3 14.4-10.8 28.1-10.5 28.1-10.5l1 1.2c-18 5.2-26.3 13.1-26.3 13.1s2.2-1.2 5.9-2.9c10.7-4.7 19.2-6 22.7-6.3.6-.1 1.1-.2 1.7-.2 6.1-.8 13-1 20.2-.2 9.5 1.1 19.7 3.9 30.1 9.6 0 0-7.9-7.5-24.9-12.7l1.4-1.6s13.7-.3 28.1 10.5c0 0 14.4 26.1 14.4 58.3 0 0-8.5 14.5-30.6 15.2z"/> ' + window.DiscordChat.messages['chat-live2'] + ' – Discord' + ' ' +                     ' ' +                        ' ' +                          avatarContainer.prop('outerHTML') + ((members.length - appendedAvatars) > 0 ? ('+' + (members.length - appendedAvatars) + '') : '') + ' ' +                       '' + window.DiscordChat.messages['chat-join-the-chat'] + '' + ' ' +                   ' ')

$('#WikiaRail').prepend(element) $('.chat-module:not(.DiscordChat)').css('display', 'none')

$('a[href="#showDiscordUser"][data-discorduser]').click(function (event) {     event.preventDefault      window.DiscordChat.showDiscordUser(decodeURI($(this).attr('data-discorduser')))    })

$('a[href="#showDiscordUsers"]').click(function (event) {     event.preventDefault      window.DiscordChat.showDiscordUsers    })

mw.hook('DiscordIntegrator.added').fire(element) }, showDiscordUsers: function  { var usersContent = '' var members = window.DiscordChat.data.members for (var i = 0; i < members.length; i++) { usersContent += ' ' + (members[i].nick || members[i].username) + ' ' }   usersContent += '</ul>' require(['wikia.ui.factory'], function (ui) {     ui.init(['modal']).then(function (uimodal) { var modalconfig = { type: 'default', vars: { id: 'show-discord-users', size: 'small', title: window.DiscordChat.data.name + ' – ' + window.DiscordChat.data.members.length, content: usersContent, buttons: [{ vars: { value: window.DiscordChat.messages['awc-metrics-close'], data: [{ key: 'event', value: 'close' }]             }            }, {              type: 'link', vars: { value: window.DiscordChat.messages['chat-join-the-chat'], href: window.DiscordChat.data.instant_invite, title: '' }           }]          }        }

uimodal.createComponent(modalconfig, function (modal) {         modal.show        }) })   })  },  showDiscordUser: function (tag) { var members = window.DiscordChat.data.members var mi = null for (var i = 0; i < members.length; i++) { if (members[i].username + '#' + members[i].discriminator === tag) { mi = i       break }   }    require(['wikia.ui.factory'], function (ui) {      ui.init(['modal']).then(function (uimodal) { var modalconfig = { type: 'default', vars: { id: 'show-discord-user', size: 'small', title: (members[mi].nick || members[mi].username) + (members[mi].bot ? ' [BOT]' : ''), content: ' <img style="border-radius: 50%;" src="' + members[mi].avatar_url + '"><div style="position: absolute; width: 32px; height: 32px; border-radius: 50%; right: 0; bottom: 0; background-color: ' + (members[mi].status === 'online' ? 'lawngreen' : (members[mi].status === 'idle' ? 'gold' : 'firebrick')) + ';">  ' + tag + ' ', buttons: [{ vars: { value: window.DiscordChat.messages['awc-metrics-close'], data: [{ key: 'event', value: 'close' }]             }            }]          }        }

uimodal.createComponent(modalconfig, function (modal) {         modal.show        }) })   })  },  shuffle: function (a) { var j, x, i   for (i = a.length - 1; i > 0; i--) { j = Math.floor(Math.random * (i + 1)) x = a[i] a[i] = a[j] a[j] = x   } } }

mw.loader.using('mediawiki.api', function {  $(function  { if (window.DiscordChat && !window.DiscordChat.ready) { window.DiscordChat.init } }) })