user.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. const htmlparser = require('htmlparser2');
  2. const {MessageEmbed} = require('discord.js');
  3. const global_block = require('../../../functions/global_block.js');
  4. const {timeoptions, usergroups} = require('../../../util/default.json');
  5. function fandom_user(lang, msg, namespace, username, wiki, querystring, fragment, querypage, contribs, reaction, spoiler) {
  6. if ( /^(?:(?:\d{1,3}\.){3}\d{1,3}(?:\/\d{2})?|(?:[\dA-F]{1,4}:){7}[\dA-F]{1,4}(?:\/\d{2,3})?)$/.test(username) ) {
  7. got.get( wiki + 'api.php?action=query&meta=siteinfo&siprop=general&list=blocks&bkprop=user|by|timestamp|expiry|reason&bkip=' + encodeURIComponent( username ) + '&format=json', {
  8. responseType: 'json'
  9. } ).then( response => {
  10. var body = response.body;
  11. if ( body && body.warnings ) log_warn(body.warnings);
  12. if ( response.statusCode !== 200 || !body || !body.query || !body.query.blocks ) {
  13. if ( body && body.error && ( body.error.code === 'param_ip' || body.error.code === 'cidrtoobroad' ) ) {
  14. if ( querypage.missing !== undefined || querypage.ns === -1 ) {
  15. msg.reactEmoji('error');
  16. if ( reaction ) reaction.removeEmoji();
  17. }
  18. else {
  19. var pagelink = wiki.toLink(querypage.title, querystring.toTitle(), fragment);
  20. var embed = new MessageEmbed().setTitle( querypage.title.escapeFormatting() ).setURL( pagelink );
  21. got.get( wiki.toDescLink(querypage.title) ).then( descresponse => {
  22. var descbody = descresponse.body;
  23. if ( descresponse.statusCode !== 200 || !descbody ) {
  24. console.log( '- ' + descresponse.statusCode + ': Error while getting the description.' );
  25. } else {
  26. var thumbnail = wiki.toLink('Special:FilePath/Wiki-wordmark.png');
  27. var parser = new htmlparser.Parser( {
  28. onopentag: (tagname, attribs) => {
  29. if ( tagname === 'meta' && attribs.property === 'og:description' ) {
  30. var description = attribs.content.escapeFormatting();
  31. if ( description.length > 2000 ) description = description.substring(0, 2000) + '\u2026';
  32. embed.setDescription( description );
  33. }
  34. if ( tagname === 'meta' && attribs.property === 'og:image' ) {
  35. thumbnail = attribs.content;
  36. }
  37. }
  38. }, {decodeEntities:true} );
  39. parser.write( descbody );
  40. parser.end();
  41. embed.setThumbnail( thumbnail );
  42. }
  43. }, error => {
  44. console.log( '- Error while getting the description: ' + error );
  45. } ).finally( () => {
  46. msg.sendChannel( spoiler + '<' + pagelink + '>' + spoiler, {embed} );
  47. if ( reaction ) reaction.removeEmoji();
  48. } );
  49. }
  50. }
  51. else {
  52. console.log( '- ' + response.statusCode + ': Error while getting the search results: ' + ( body && body.error && body.error.info ) );
  53. msg.sendChannelError( spoiler + '<' + wiki.toLink(( querypage.noRedirect ? namespace : contribs ) + username, querystring.toTitle(), fragment) + '>' + spoiler );
  54. if ( reaction ) reaction.removeEmoji();
  55. }
  56. }
  57. else {
  58. if ( !querypage.noRedirect || ( querypage.missing === undefined && querypage.ns !== -1 ) ) namespace = contribs;
  59. var blocks = body.query.blocks.map( block => {
  60. var isBlocked = false;
  61. var blockedtimestamp = new Date(block.timestamp).toLocaleString(lang.get('dateformat'), timeoptions);
  62. var blockexpiry = block.expiry;
  63. if ( blockexpiry === 'infinity' ) {
  64. blockexpiry = lang.get('user.block.until_infinity');
  65. isBlocked = true;
  66. } else if ( blockexpiry ) {
  67. if ( Date.parse(blockexpiry) > Date.now() ) isBlocked = true;
  68. blockexpiry = new Date(blockexpiry).toLocaleString(lang.get('dateformat'), timeoptions);
  69. }
  70. if ( isBlocked ) return {
  71. header: lang.get('user.block.header').replaceSave( '%s', block.user ).escapeFormatting(),
  72. text: lang.get('user.block.' + ( block.reason ? 'text' : 'noreason' )).replaceSave( '%1$s', blockedtimestamp ).replaceSave( '%2$s', blockexpiry ),
  73. by: block.by,
  74. reason: block.reason
  75. };
  76. } ).filter( block => block !== undefined );
  77. if ( username.includes( '/' ) ) {
  78. var rangeprefix = username;
  79. if ( username.includes( ':' ) ) {
  80. var range = parseInt(username.replace( /^.+\/(\d{2,3})$/, '$1' ), 10);
  81. if ( range === 128 ) username = username.replace( /^(.+)\/\d{2,3}$/, '$1' );
  82. else if ( range >= 112 ) rangeprefix = username.replace( /^((?:[\dA-F]{1,4}:){7}).+$/, '$1' );
  83. else if ( range >= 96 ) rangeprefix = username.replace( /^((?:[\dA-F]{1,4}:){6}).+$/, '$1' );
  84. else if ( range >= 80 ) rangeprefix = username.replace( /^((?:[\dA-F]{1,4}:){5}).+$/, '$1' );
  85. else if ( range >= 64 ) rangeprefix = username.replace( /^((?:[\dA-F]{1,4}:){4}).+$/, '$1' );
  86. else if ( range >= 48 ) rangeprefix = username.replace( /^((?:[\dA-F]{1,4}:){3}).+$/, '$1' );
  87. else if ( range >= 32 ) rangeprefix = username.replace( /^((?:[\dA-F]{1,4}:){2}).+$/, '$1' );
  88. else if ( range >= 19 ) rangeprefix = username.replace( /^((?:[\dA-F]{1,4}:){1}).+$/, '$1' );
  89. }
  90. else {
  91. var range = parseInt(username.substring(username.length - 2), 10);
  92. if ( range === 32 ) username = username.replace( /^(.+)\/\d{2}$/, '$1' );
  93. else if ( range >= 24 ) rangeprefix = username.replace( /^((?:\d{1,3}\.){3}).+$/, '$1' );
  94. else if ( range >= 16 ) rangeprefix = username.replace( /^((?:\d{1,3}\.){2}).+$/, '$1' );
  95. }
  96. }
  97. got.get( wiki + 'api.php?action=query&list=usercontribs&ucprop=&uclimit=50&ucuser=' + encodeURIComponent( username ) + '&format=json', {
  98. responseType: 'json'
  99. } ).then( ucresponse => {
  100. var ucbody = ucresponse.body;
  101. if ( rangeprefix && !username.includes( '/' ) ) username = rangeprefix;
  102. if ( ucbody && ucbody.warnings ) log_warn(ucbody.warnings);
  103. if ( ucresponse.statusCode !== 200 || !ucbody || !ucbody.query || !ucbody.query.usercontribs ) {
  104. if ( ucbody && ucbody.error && ucbody.error.code === 'baduser_ucuser' ) {
  105. msg.reactEmoji('error');
  106. }
  107. else {
  108. console.log( '- ' + ucresponse.statusCode + ': Error while getting the search results: ' + ( ucbody && ucbody.error && ucbody.error.info ) );
  109. msg.sendChannelError( spoiler + '<' + wiki.toLink(namespace + username, querystring.toTitle(), fragment, body.query.general) + '>' + spoiler );
  110. }
  111. }
  112. else {
  113. var editcount = [lang.get('user.info.editcount'), ( username.includes( '/' ) ? '~' : '' ) + ucbody.query.usercontribs.length + ( ucbody.continue ? '+' : '' )];
  114. var pagelink = wiki.toLink(namespace + username, querystring.toTitle(), fragment, body.query.general);
  115. if ( msg.showEmbed() ) {
  116. var text = '<' + pagelink + '>';
  117. var embed = new MessageEmbed().setAuthor( body.query.general.sitename ).setTitle( username ).setURL( pagelink ).addField( editcount[0], '[' + editcount[1] + '](' + wiki.toLink(contribs + username, '', '', body.query.general, true) + ')' );
  118. if ( blocks.length ) {
  119. block.text = block.text.replaceSave( '%3$s', '[' + block.by.escapeFormatting() + '](' + wiki.toLink('User:' + block.by, '', '', body.query.general, true) + ')' );
  120. if ( block.reason ) block.text = block.text.replaceSave( '%4$s', block.reason.toMarkdown(wiki, body.query.general) );
  121. embed.addField( block.header, block.text );
  122. }
  123. if ( msg.channel.type === 'text' && msg.guild.id in patreons ) embed.addField( '\u200b', '<a:loading:641343250661113886> **' + lang.get('user.info.loading') + '**' );
  124. }
  125. else {
  126. var embed = {};
  127. var text = '<' + pagelink + '>\n\n' + editcount.join(' ');
  128. if ( blocks.length ) {
  129. block.text = block.text.replaceSave( '%3$s', block.by.escapeFormatting() );
  130. if ( block.reason ) block.text = block.text.replaceSave( '%4$s', block.reason.toPlaintext() );
  131. text += '\n\n**' + block.header + '**\n' + block.text;
  132. }
  133. if ( msg.channel.type === 'text' && msg.guild.id in patreons ) text += '\n\n<a:loading:641343250661113886> **' + lang.get('user.info.loading') + '**';
  134. }
  135. msg.sendChannel( spoiler + text + spoiler, {embed} ).then( message => global_block(lang, message, username, text, embed, wiki, spoiler) );
  136. }
  137. }, error => {
  138. if ( rangeprefix && !username.includes( '/' ) ) username = rangeprefix;
  139. console.log( '- Error while getting the search results: ' + error );
  140. msg.sendChannelError( spoiler + '<' + wiki.toLink(namespace + username, querystring.toTitle(), fragment, body.query.general) + '>' + spoiler );
  141. } ).finally( () => {
  142. if ( reaction ) reaction.removeEmoji();
  143. } );
  144. }
  145. }, error => {
  146. console.log( '- Error while getting the search results: ' + error );
  147. msg.sendChannelError( spoiler + '<' + wiki.toLink(( querypage.noRedirect ? namespace : contribs ) + username, querystring.toTitle(), fragment) + '>' + spoiler );
  148. if ( reaction ) reaction.removeEmoji();
  149. } );
  150. } else {
  151. got.get( wiki + 'api.php?action=query&meta=allmessages|siteinfo&ammessages=custom-Wiki_Manager&amenableparser=true&siprop=general&list=users&usprop=blockinfo|groups|editcount|registration|gender&ususers=' + encodeURIComponent( username ) + '&format=json', {
  152. responseType: 'json'
  153. } ).then( response => {
  154. var body = response.body;
  155. if ( body && body.warnings ) log_warn(body.warnings);
  156. if ( response.statusCode !== 200 || !body || !body.query || !body.query.users ) {
  157. console.log( '- ' + response.statusCode + ': Error while getting the search results: ' + ( body && body.error && body.error.info ) );
  158. msg.sendChannelError( spoiler + '<' + wiki.toLink(namespace + username, querystring.toTitle(), fragment) + '>' + spoiler );
  159. if ( reaction ) reaction.removeEmoji();
  160. }
  161. else {
  162. var queryuser = body.query.users[0];
  163. if ( !queryuser ) {
  164. if ( querypage.missing !== undefined || querypage.ns === -1 ) {
  165. msg.reactEmoji('🤷');
  166. if ( reaction ) reaction.removeEmoji();
  167. }
  168. else {
  169. var pagelink = wiki.toLink(querypage.title, querystring.toTitle(), fragment, body.query.general);
  170. var embed = new MessageEmbed().setAuthor( body.query.general.sitename ).setTitle( querypage.title.escapeFormatting() ).setURL( pagelink );
  171. got.get( wiki.toDescLink(querypage.title) ).then( descresponse => {
  172. var descbody = descresponse.body;
  173. if ( descresponse.statusCode !== 200 || !descbody ) {
  174. console.log( '- ' + descresponse.statusCode + ': Error while getting the description.' );
  175. } else {
  176. var thumbnail = wiki.toLink('Special:FilePath/Wiki-wordmark.png', '', '', body.query.general);
  177. var parser = new htmlparser.Parser( {
  178. onopentag: (tagname, attribs) => {
  179. if ( tagname === 'meta' && attribs.property === 'og:description' ) {
  180. var description = attribs.content.escapeFormatting();
  181. if ( description.length > 2000 ) description = description.substring(0, 2000) + '\u2026';
  182. embed.setDescription( description );
  183. }
  184. if ( tagname === 'meta' && attribs.property === 'og:image' ) {
  185. thumbnail = attribs.content;
  186. }
  187. }
  188. }, {decodeEntities:true} );
  189. parser.write( descbody );
  190. parser.end();
  191. embed.setThumbnail( thumbnail );
  192. }
  193. }, error => {
  194. console.log( '- Error while getting the description: ' + error );
  195. } ).finally( () => {
  196. msg.sendChannel( spoiler + '<' + pagelink + '>' + spoiler, {embed} );
  197. if ( reaction ) reaction.removeEmoji();
  198. } );
  199. }
  200. }
  201. else {
  202. username = queryuser.name;
  203. var gender = [lang.get('user.info.gender')];
  204. switch (queryuser.gender) {
  205. case 'male':
  206. gender.push(lang.get('user.gender.male'));
  207. break;
  208. case 'female':
  209. gender.push(lang.get('user.gender.female'));
  210. break;
  211. default:
  212. gender.push(lang.get('user.gender.unknown'));
  213. }
  214. var registration = [lang.get('user.info.registration'), new Date(queryuser.registration).toLocaleString(lang.get('dateformat'), timeoptions)];
  215. var editcount = [lang.get('user.info.editcount'), queryuser.editcount];
  216. var groups = queryuser.groups;
  217. var group = [lang.get('user.info.group')];
  218. for ( var i = 0; i < usergroups.length; i++ ) {
  219. if ( groups.includes( usergroups[i] ) && ( group.length === 1 || !['autoconfirmed', 'user'].includes( usergroups[i] ) ) ) {
  220. if ( usergroups[i] === 'wiki-manager' && body.query.allmessages[0]['*'] === username ) {
  221. group.push('**' + lang.get('user.groups.' + usergroups[i]) + '**');
  222. }
  223. else group.push(lang.get('user.groups.' + usergroups[i]));
  224. }
  225. }
  226. var isBlocked = false;
  227. var blockexpiry = queryuser.blockexpiry;
  228. if ( blockexpiry === 'infinity' ) {
  229. blockexpiry = lang.get('user.block.until_infinity');
  230. isBlocked = true;
  231. } else if ( blockexpiry ) {
  232. var blockexpirydate = blockexpiry.replace( /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2,3})/, '$1-$2-$3T$4:$5:$6Z' );
  233. blockexpiry = new Date(blockexpirydate).toLocaleString(lang.get('dateformat'), timeoptions);
  234. if ( Date.parse(blockexpirydate) > Date.now() ) isBlocked = true;
  235. }
  236. var blockedby = '[[User:' + queryuser.blockedby + '|' + queryuser.blockedby + ']]';
  237. var blockreason = queryuser.blockreason;
  238. var block = {
  239. header: lang.get('user.block.header').replaceSave( '%s', username ).escapeFormatting(),
  240. text: lang.get('user.block.nofrom' + ( blockreason ? 'text' : 'noreason' )).replaceSave( '%2$s', blockexpiry ),
  241. by: blockedby,
  242. reason: blockreason
  243. };
  244. var pagelink = wiki.toLink(namespace + username, querystring.toTitle(), fragment, body.query.general);
  245. if ( msg.showEmbed() ) {
  246. var text = '<' + pagelink + '>';
  247. var embed = new MessageEmbed().setAuthor( body.query.general.sitename ).setTitle( username.escapeFormatting() ).setURL( pagelink ).addField( editcount[0], '[' + editcount[1] + '](' + wiki.toLink(contribs + username, '', '', body.query.general, true) + ')', true ).addField( group[0], group.slice(1).join(',\n'), true ).addField( gender[0], gender[1], true ).addField( registration[0], registration[1], true );
  248. }
  249. else {
  250. var embed = {};
  251. var text = '<' + pagelink + '>\n\n' + gender.join(' ') + '\n' + registration.join(' ') + '\n' + editcount.join(' ') + '\n' + group[0] + ' ' + group.slice(1).join(', ');
  252. }
  253. got.get( 'https://services.fandom.com/user-attribute/user/' + queryuser.userid + '?format=json&cache=' + Date.now(), {
  254. headers: {
  255. Accept: 'application/hal+json'
  256. },
  257. responseType: 'json'
  258. } ).then( presponse => {
  259. var pbody = presponse.body;
  260. if ( presponse.statusCode !== 200 || !pbody || pbody.title || !pbody._embedded || !pbody._embedded.properties ) {
  261. if ( !( pbody && pbody.status === 404 ) ) {
  262. console.log( '- ' + presponse.statusCode + ': Error while getting the user profile: ' + ( pbody && pbody.title ) );
  263. }
  264. }
  265. else {
  266. var profile = pbody._embedded.properties;
  267. var discordfield = profile.find( field => field.name === 'discordHandle' );
  268. var avatarfield = profile.find( field => field.name === 'avatar' );
  269. var biofield = profile.find( field => field.name === 'bio' );
  270. if ( discordfield && discordfield.value ) {
  271. discordfield.value = htmlToPlain( discordfield.value ).replace( /^\s*([^@#:]{2,32}?)\s*#(\d{4,6})\s*$/, '$1#$2' );
  272. if ( discordfield.value.length > 50 ) discordfield.value = discordfield.value.substring(0, 50) + '\u2026';
  273. if ( msg.channel.type === 'text' ) var discordmember = msg.guild.members.cache.find( member => {
  274. return member.user.tag.escapeFormatting() === discordfield.value;
  275. } );
  276. var discordname = [lang.get('user.info.discord'),discordfield.value];
  277. if ( discordmember ) discordname[1] = discordmember.toString();
  278. if ( msg.showEmbed() ) embed.addField( discordname[0], discordname[1], true );
  279. else text += '\n' + discordname.join(' ');
  280. }
  281. if ( msg.showEmbed() ) {
  282. if ( avatarfield && avatarfield.value ) embed.setThumbnail( avatarfield.value );
  283. if ( biofield && biofield.value && !embed.description ) {
  284. var bio = biofield.value.escapeFormatting();
  285. if ( bio.length > 2000 ) bio = bio.substring(0, 2000) + '\u2026';
  286. embed.setDescription( bio );
  287. }
  288. }
  289. }
  290. }, error => {
  291. console.log( '- Error while getting the user profile: ' + error );
  292. } ).finally( () => {
  293. if ( msg.showEmbed() ) {
  294. if ( isBlocked ) {
  295. block.text = block.text.replaceSave( '%3$s', '[' + block.by.escapeFormatting() + '](' + wiki.toLink('User:' + block.by, '', '', body.query.general, true) + ')' );
  296. if ( block.reason ) block.text = block.text.replaceSave( '%4$s', block.reason.toMarkdown(wiki, body.query.general) );
  297. embed.addField( block.header, block.text );
  298. }
  299. if ( msg.channel.type === 'text' && msg.guild.id in patreons ) embed.addField( '\u200b', '<a:loading:641343250661113886> **' + lang.get('user.info.loading') + '**' );
  300. }
  301. else {
  302. if ( isBlocked ) {
  303. block.text = block.text.replaceSave( '%3$s', block.by.escapeFormatting() );
  304. if ( block.reason ) block.text = block.text.replaceSave( '%4$s', block.reason.toPlaintext() );
  305. text += '\n\n**' + block.header + '**\n' + block.text;
  306. }
  307. if ( msg.channel.type === 'text' && msg.guild.id in patreons ) text += '\n\n<a:loading:641343250661113886> **' + lang.get('user.info.loading') + '**';
  308. }
  309. msg.sendChannel( spoiler + text + spoiler, {embed} ).then( message => global_block(lang, message, username, text, embed, wiki, spoiler) );
  310. if ( reaction ) reaction.removeEmoji();
  311. } );
  312. }
  313. }
  314. }, error => {
  315. console.log( '- Error while getting the search results: ' + error );
  316. msg.sendChannelError( spoiler + '<' + wiki.toLink(namespace + username, querystring.toTitle(), fragment) + '>' + spoiler );
  317. if ( reaction ) reaction.removeEmoji();
  318. } );
  319. }
  320. }
  321. function htmlToPlain(html) {
  322. var text = '';
  323. var parser = new htmlparser.Parser( {
  324. ontext: (htmltext) => {
  325. text += htmltext.escapeFormatting();
  326. }
  327. }, {decodeEntities:true} );
  328. parser.write( html );
  329. parser.end();
  330. return text;
  331. };
  332. module.exports = {
  333. name: 'user',
  334. run: fandom_user
  335. };