Browse Source

custom embed for `!wiki User:`

Markus-Rost 6 years ago
parent
commit
ef4a724709
2 changed files with 79 additions and 39 deletions
  1. 25 10
      i18n.json
  2. 54 29
      main.js

+ 25 - 10
i18n.json

@@ -95,8 +95,11 @@
 				"editcount": "Edit count:",
 				"editcount": "Edit count:",
 				"group": "Group:"
 				"group": "Group:"
 			},
 			},
-			"blocked": "**%1$s is currently blocked!**\nBlocked on %2$s until %3$s by %4$s with reason \"%5$s\".",
-			"until_infinity": "the end of all days"
+			"block": {
+				"header": "%s is currently blocked!",
+				"text": "Blocked on %1$s until %2$s by %3$s with reason \"%4$s\".",
+				"until_infinity": "the end of all days"
+			}
 		},
 		},
 		"diff": {
 		"diff": {
 			"badrev": "at least one revision doesn't exist!",
 			"badrev": "at least one revision doesn't exist!",
@@ -247,8 +250,11 @@
 				"editcount": "Bearbeitungen:",
 				"editcount": "Bearbeitungen:",
 				"group": "Gruppe:"
 				"group": "Gruppe:"
 			},
 			},
-			"blocked": "**%1$s ist derzeit gesperrt!**\nGesperrt am %2$s bis zum %3$s von %4$s mit der Begründung \"%5$s\".",
-			"until_infinity": "Ende aller Tage"
+			"block": {
+				"header": "%s ist derzeit gesperrt!",
+				"text": "Gesperrt am %1$s bis zum %2$s von %3$s mit der Begründung \"%4$s\".",
+				"until_infinity": "Ende aller Tage"
+			}
 		},
 		},
 		"diff": {
 		"diff": {
 			"badrev": "mindestens eine Version existiert nicht!",
 			"badrev": "mindestens eine Version existiert nicht!",
@@ -410,8 +416,11 @@
 				"editcount": "Nombre d'éditions :",
 				"editcount": "Nombre d'éditions :",
 				"group": "Groupe :"
 				"group": "Groupe :"
 			},
 			},
-			"blocked": "**%1$s est actuellement bloqué !**\nBloqué le %2$s jusqu'au %3$s par %4$s avec la raison \"%5$s\".",
-			"until_infinity": "moment de la fin des jours"
+			"block": {
+				"header": "%s est actuellement bloqué !",
+				"text": "Bloqué le %1$s jusqu'au %2$s par %3$s avec la raison \"%4$s\".",
+				"until_infinity": "moment de la fin des jours"
+			}
 		},
 		},
 		"diff": {
 		"diff": {
 			"badrev": "au moins une des révisions n'existe pas !",
 			"badrev": "au moins une des révisions n'existe pas !",
@@ -570,8 +579,11 @@
 				"editcount": "Ilość edycji:",
 				"editcount": "Ilość edycji:",
 				"group": "Grupa:"
 				"group": "Grupa:"
 			},
 			},
-			"blocked": "**%1$s jest obecnie zablokowany!**\nZablokowany %2$s do %3$s przez %4$s z powodu \"%5$s\".",
-			"until_infinity": "do końca wszechświata"
+			"block": {
+				"header": "%s jest obecnie zablokowany!",
+				"text": "Zablokowany %1$s do %2$s przez %3$s z powodu \"%4$s\".",
+				"until_infinity": "do końca wszechświata"
+			}
 		},
 		},
 		"diff": {
 		"diff": {
 			"badrev": "przynajmniej jedna zmiana nie istnieje!",
 			"badrev": "przynajmniej jedna zmiana nie istnieje!",
@@ -738,8 +750,11 @@
 				"editcount": "Contagem de edição:",
 				"editcount": "Contagem de edição:",
 				"group": "Grupo:"
 				"group": "Grupo:"
 			},
 			},
-			"blocked": "**%1$s está bloqueado no momento!**\nBloqueado em %2$s até %3$s pela %4$s razão \"%5$s\".",
-			"until_infinity": "o fim de todos os dias"
+			"block": {
+				"header": "%s está bloqueado no momento!",
+				"text": "Bloqueado em %1$s até %2$s pela %3$s razão \"%4$s\".",
+				"until_infinity": "o fim de todos os dias"
+			}
 		},
 		},
 		"diff": {
 		"diff": {
 			"badrev": "pelo menos uma revisão não existe!",
 			"badrev": "pelo menos uma revisão não existe!",

+ 54 - 29
main.js

@@ -637,7 +637,7 @@ function cmd_sendumfrage(lang, msg, args, reactions, imgs, i) {
 function cmd_user(lang, msg, namespace, username, wiki, linksuffix, reaction) {
 function cmd_user(lang, msg, namespace, username, wiki, linksuffix, reaction) {
 	if ( /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:\/\d\d)?$/.test(username) ) {
 	if ( /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:\/\d\d)?$/.test(username) ) {
 		request( {
 		request( {
-			uri: 'https://' + wiki + '.gamepedia.com/api.php?action=query&format=json&list=blocks&bkprop=user|by|timestamp|expiry|reason&bkip=' + encodeURIComponent( username ),
+			uri: 'https://' + wiki + '.gamepedia.com/api.php?action=query&format=json&meta=siteinfo&siprop=general&list=blocks&bkprop=user|by|timestamp|expiry|reason&bkip=' + encodeURIComponent( username ),
 			json: true
 			json: true
 		}, function( error, response, body ) {
 		}, function( error, response, body ) {
 			if ( error || !response || response.statusCode != 200 || !body || body.batchcomplete == undefined || !body.query || !body.query.blocks ) {
 			if ( error || !response || response.statusCode != 200 || !body || body.batchcomplete == undefined || !body.query || !body.query.blocks ) {
@@ -661,15 +661,14 @@ function cmd_user(lang, msg, namespace, username, wiki, linksuffix, reaction) {
 					var blockedtimestamp = (new Date(block.timestamp)).toLocaleString(lang.user.dateformat, timeoptions);
 					var blockedtimestamp = (new Date(block.timestamp)).toLocaleString(lang.user.dateformat, timeoptions);
 					var blockexpiry = block.expiry;
 					var blockexpiry = block.expiry;
 					if ( blockexpiry == 'infinity' ) {
 					if ( blockexpiry == 'infinity' ) {
-						blockexpiry = lang.user.until_infinity;
+						blockexpiry = lang.user.block.until_infinity;
 						isBlocked = true;
 						isBlocked = true;
 					} else if ( blockexpiry ) {
 					} else if ( blockexpiry ) {
 						if ( Date.parse(blockexpiry) > Date.now() ) isBlocked = true;
 						if ( Date.parse(blockexpiry) > Date.now() ) isBlocked = true;
 						blockexpiry = (new Date(blockexpiry)).toLocaleString(lang.user.dateformat, timeoptions);
 						blockexpiry = (new Date(blockexpiry)).toLocaleString(lang.user.dateformat, timeoptions);
 					}
 					}
-					if ( isBlocked ) return '\n\n' + lang.user.blocked.replace( '%1$s', block.user ).replace( '%2$s', blockedtimestamp ).replace( '%3$s', blockexpiry ).replace( '%4$s', block.by ).replace( '%5$s', block.reason.toPlaintext() );
-					else return '';
-				} ).join('');
+					if ( isBlocked ) return [lang.user.block.header.replace( '%s', block.user ), lang.user.block.text.replace( '%1$s', blockedtimestamp ).replace( '%2$s', blockexpiry ).replace( '%3$s', '[[User:' + block.by + '|' + block.by + ']]' ).replace( '%4$s', block.reason )];
+				} ).filter( block => block != undefined );
 				if ( username.includes( '/' ) ) {
 				if ( username.includes( '/' ) ) {
 					var rangeprefix = username;
 					var rangeprefix = username;
 					var range = parseInt(username.substr(-2, 2), 10);
 					var range = parseInt(username.substr(-2, 2), 10);
@@ -691,8 +690,21 @@ function cmd_user(lang, msg, namespace, username, wiki, linksuffix, reaction) {
 						}
 						}
 					}
 					}
 					else {
 					else {
-						var editcount = '\n' + lang.user.info.editcount + ' ' + ( username.includes( '/' ) && range != 24 && range != 16 ? '~' : '' ) + ucbody.query.usercontribs.length + ( ucbody.continue ? '+' : '' );
-						msg.channel.send( '<https://' + wiki + '.gamepedia.com/Special:Contributions/' + username.toTitle() + '>\n' + editcount + blocks );
+						var editcount = [lang.user.info.editcount, ( username.includes( '/' ) && range != 24 && range != 16 ? '~' : '' ) + ucbody.query.usercontribs.length + ( ucbody.continue ? '+' : '' )];
+						
+						var pagelink = 'https://' + wiki + '.gamepedia.com/Special:Contributions/' + username.toTitle();
+						if ( msg.showEmbed() ) {
+							var text = '<' + pagelink + '>';
+							var embed = new Discord.RichEmbed().setAuthor( body.query.general.sitename ).setTitle( username ).setURL( pagelink ).addField( editcount[0], editcount[1] );
+							if ( blocks.length ) blocks.forEach( block => embed.addField( block[0], block[1].toMarkdown(wiki) ) );
+						}
+						else {
+							var embed = {};
+							var text = '<' + pagelink + '>\n\n' + editcount.join(' ');
+							if ( blocks.length ) blocks.forEach( block => text += '\n\n**' + block[0] + '**\n' + block[1].toPlaintext() );
+						}
+						
+						msg.channel.send( text, embed );
 					}
 					}
 					
 					
 					if ( reaction ) reaction.removeEmoji();
 					if ( reaction ) reaction.removeEmoji();
@@ -701,7 +713,7 @@ function cmd_user(lang, msg, namespace, username, wiki, linksuffix, reaction) {
 		} );
 		} );
 	} else {
 	} else {
 		request( {
 		request( {
-			uri: 'https://' + wiki + '.gamepedia.com/api.php?action=query&format=json&list=users&usprop=blockinfo|groups|editcount|registration|gender&ususers=' + encodeURIComponent( username ),
+			uri: 'https://' + wiki + '.gamepedia.com/api.php?action=query&format=json&meta=siteinfo&siprop=general&list=users&usprop=blockinfo|groups|editcount|registration|gender&ususers=' + encodeURIComponent( username ),
 			json: true
 			json: true
 		}, function( error, response, body ) {
 		}, function( error, response, body ) {
 			if ( error || !response || response.statusCode != 200 || !body || body.batchcomplete == undefined || !body.query || !body.query.users[0] ) {
 			if ( error || !response || response.statusCode != 200 || !body || body.batchcomplete == undefined || !body.query || !body.query.users[0] ) {
@@ -720,26 +732,26 @@ function cmd_user(lang, msg, namespace, username, wiki, linksuffix, reaction) {
 				}
 				}
 				else {
 				else {
 					username = body.query.users[0].name;
 					username = body.query.users[0].name;
-					var gender = '\n' + lang.user.info.gender + ' ';
+					var gender = [lang.user.info.gender];
 					switch (body.query.users[0].gender) {
 					switch (body.query.users[0].gender) {
 						case 'male':
 						case 'male':
-							gender += lang.user.gender.male;
+							gender.push(lang.user.gender.male);
 							break;
 							break;
 						case 'female':
 						case 'female':
-							gender += lang.user.gender.female;
+							gender.push(lang.user.gender.female);
 							break;
 							break;
 						default: 
 						default: 
-							gender += lang.user.gender.unknown;
+							gender.push(lang.user.gender.unknown);
 					}
 					}
-					var registration = '\n' + lang.user.info.registration + ' ' + (new Date(body.query.users[0].registration)).toLocaleString(lang.user.dateformat, timeoptions);
-					var editcount = '\n' + lang.user.info.editcount + ' ' + body.query.users[0].editcount;
+					var registration = [lang.user.info.registration, (new Date(body.query.users[0].registration)).toLocaleString(lang.user.dateformat, timeoptions)];
+					var editcount = [lang.user.info.editcount, body.query.users[0].editcount];
 					var groups = body.query.users[0].groups;
 					var groups = body.query.users[0].groups;
-					var group = '\n' + lang.user.info.group + ' ';
+					var group = [lang.user.info.group];
 					for ( var i = 0; i < lang.user.groups.length; i++ ) {
 					for ( var i = 0; i < lang.user.groups.length; i++ ) {
 						if ( groups.includes( lang.user.groups[i][0] ) ) {
 						if ( groups.includes( lang.user.groups[i][0] ) ) {
 							var thisSite = allSites.find( site => site.wiki_domain == wiki + '.gamepedia.com' );
 							var thisSite = allSites.find( site => site.wiki_domain == wiki + '.gamepedia.com' );
-							if ( lang.user.groups[i][0] == 'hydra_staff' && thisSite && thisSite.wiki_managers.includes( username ) ) group += lang.user.manager;
-							else group += lang.user.groups[i][1];
+							if ( lang.user.groups[i][0] == 'hydra_staff' && thisSite && thisSite.wiki_managers.includes( username ) ) group.push(lang.user.manager);
+							else group.push(lang.user.groups[i][1]);
 							break;
 							break;
 						}
 						}
 					}
 					}
@@ -747,17 +759,30 @@ function cmd_user(lang, msg, namespace, username, wiki, linksuffix, reaction) {
 					var blockedtimestamp = (new Date(body.query.users[0].blockedtimestamp)).toLocaleString(lang.user.dateformat, timeoptions);
 					var blockedtimestamp = (new Date(body.query.users[0].blockedtimestamp)).toLocaleString(lang.user.dateformat, timeoptions);
 					var blockexpiry = body.query.users[0].blockexpiry;
 					var blockexpiry = body.query.users[0].blockexpiry;
 					if ( blockexpiry == 'infinity' ) {
 					if ( blockexpiry == 'infinity' ) {
-						blockexpiry = lang.user.until_infinity;
+						blockexpiry = lang.user.block.until_infinity;
 						isBlocked = true;
 						isBlocked = true;
 					} else if ( blockexpiry ) {
 					} else if ( blockexpiry ) {
 						var blockexpirydate = blockexpiry.replace(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2,3})/, '$1-$2-$3T$4:$5:$6Z');
 						var blockexpirydate = blockexpiry.replace(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2,3})/, '$1-$2-$3T$4:$5:$6Z');
 						blockexpiry = (new Date(blockexpirydate)).toLocaleString(lang.user.dateformat, timeoptions);
 						blockexpiry = (new Date(blockexpirydate)).toLocaleString(lang.user.dateformat, timeoptions);
 						if ( Date.parse(blockexpirydate) > Date.now() ) isBlocked = true;
 						if ( Date.parse(blockexpirydate) > Date.now() ) isBlocked = true;
 					}
 					}
-					var blockedby = body.query.users[0].blockedby;
+					var blockedby = '[[User:' + body.query.users[0].blockedby + '|' + body.query.users[0].blockedby + ']]';
 					var blockreason = body.query.users[0].blockreason;
 					var blockreason = body.query.users[0].blockreason;
-					var blocktext = ( isBlocked ? '\n\n' + lang.user.blocked.replace( '%1$s', username ).replace( '%2$s', blockedtimestamp ).replace( '%3$s', blockexpiry ).replace( '%4$s', blockedby ).replace( '%5$s', blockreason.toPlaintext() ) : '' );
-					msg.channel.send( '<https://' + wiki + '.gamepedia.com/' + namespace + username.toTitle() + linksuffix + '>\n' + gender + registration + editcount + group + blocktext );
+					var block = [lang.user.block.header.replace( '%s', username ), lang.user.block.text.replace( '%1$s', blockedtimestamp ).replace( '%2$s', blockexpiry ).replace( '%3$s', blockedby ).replace( '%4$s', blockreason )];
+					
+					var pagelink = 'https://' + wiki + '.gamepedia.com/' + namespace + username.toTitle() + linksuffix;
+					if ( msg.showEmbed() ) {
+						var text = '<' + pagelink + '>';
+						var embed = new Discord.RichEmbed().setAuthor( body.query.general.sitename ).setTitle( username ).setURL( pagelink ).addField( editcount[0], editcount[1], true ).addField( group[0], group[1], true ).addField( gender[0], gender[1], true ).addField( registration[0], registration[1], true );
+						if ( isBlocked ) embed.addField( block[0], block[1].toMarkdown(wiki) );
+					}
+					else {
+						var embed = {};
+						var text = '<' + pagelink + '>\n\n' + gender.join(' ') + '\n' + registration.join(' ') + '\n' + editcount.join(' ') + '\n' + group.join(' ');
+						if ( isBlocked ) text += '\n\n**' + block[0] + '**\n' + block[1].toPlaintext();
+					}
+					
+					msg.channel.send( text, embed );
 				}
 				}
 			}
 			}
 			
 			
@@ -1126,7 +1151,7 @@ Array.prototype.toEmojis = function() {
 
 
 String.prototype.toTitle = function(isMarkdown = false) {
 String.prototype.toTitle = function(isMarkdown = false) {
 	var title = this.replace( / /g, '_' ).replace( /\%/g, '%25' ).replace( /\?/g, '%3F' );
 	var title = this.replace( / /g, '_' ).replace( /\%/g, '%25' ).replace( /\?/g, '%3F' );
-	if ( isMarkdown ) title = title.replace( /\(/g, '%28' ).replace( /\)/g, '%29' );
+	if ( isMarkdown ) title = title.replace( /(\(|\))/g, '\\$1' );
 	return title;
 	return title;
 };
 };
 
 
@@ -1134,26 +1159,26 @@ String.prototype.toSection = function() {
 	return encodeURIComponent( this.replace( / /g, '_' ) ).replace( /\'/g, '%27' ).replace( /\(/g, '%28' ).replace( /\)/g, '%29' ).replace( /\%/g, '.' );
 	return encodeURIComponent( this.replace( / /g, '_' ) ).replace( /\'/g, '%27' ).replace( /\(/g, '%28' ).replace( /\)/g, '%29' ).replace( /\%/g, '.' );
 };
 };
 
 
-String.prototype.toMarkdown = function(wiki, title) {
+String.prototype.toMarkdown = function(wiki, title = '') {
 	var text = this;
 	var text = this;
 	while ( ( link = /\[\[(?:([^\|\]]+)\|)?([^\]]+)\]\]/g.exec(text) ) !== null ) {
 	while ( ( link = /\[\[(?:([^\|\]]+)\|)?([^\]]+)\]\]/g.exec(text) ) !== null ) {
 		if ( link[1] ) {
 		if ( link[1] ) {
-			var page = ( /^(#|\/)/.test(link[1]) ? ( /^#/.test(link[1]) ? title.toTitle(true) + '#' + link[1].substr(1).toSection() : ( title + link[1] ).toTitle(true) ) : link[1].toTitle(true) );
+			var page = ( /^(#|\/)/.test(link[1]) ? title.toTitle(true) + ( /^#/.test(link[1]) ? '#' + link[1].substr(1).toSection() : link[1].toTitle(true) ) : link[1].toTitle(true) );
 			text = text.replace( link[0], '[' + link[2] + '](https://' + wiki + '.gamepedia.com/' + page + ')');
 			text = text.replace( link[0], '[' + link[2] + '](https://' + wiki + '.gamepedia.com/' + page + ')');
 		} else {
 		} else {
-			var page = ( /^(#|\/)/.test(link[2]) ? ( /^#/.test(link[2]) ? title.toTitle(true) + '#' + link[2].substr(1).toSection() : ( title + link[2] ).toTitle(true) ) : link[2].toTitle(true) );
+			var page = ( /^(#|\/)/.test(link[2]) ? title.toTitle(true) + ( /^#/.test(link[2]) ? '#' + link[2].substr(1).toSection() : link[2].toTitle(true) ) : link[2].toTitle(true) );
 			text = text.replace( link[0], '[' + link[2] + '](https://' + wiki + '.gamepedia.com/' + page + ')');
 			text = text.replace( link[0], '[' + link[2] + '](https://' + wiki + '.gamepedia.com/' + page + ')');
 		}
 		}
 	}
 	}
-	while ( ( link = /\/\*\s*([^\*]+?)\s*\*\//g.exec(text) ) !== null ) {
+	while ( title != '' && ( link = /\/\*\s*([^\*]+?)\s*\*\/\s*(.)?/g.exec(text) ) !== null ) {
 		var page = title.toTitle(true) + '#' + link[1].toSection();
 		var page = title.toTitle(true) + '#' + link[1].toSection();
-		text = text.replace( link[0], '[→](https://' + wiki + '.gamepedia.com/' + page + ')' + link[1] + ( link[0] == text ? '' : ':' ) );
+		text = text.replace( link[0], '[→](https://' + wiki + '.gamepedia.com/' + page + ')' + link[1] + ( link[2] ? ': ' + link[2] : '' ) );
 	}
 	}
-	return text.replace( /(`|_|\*|~)/g, '\\$1' );
+	return text.replace( /(`|_|\*|~|<|>)/g, '\\$1' );
 };
 };
 
 
 String.prototype.toPlaintext = function() {
 String.prototype.toPlaintext = function() {
-	return this.replace( /\[\[(?:[^\|\]]+\|)?([^\]]+)\]\]/g, '$1' ).replace( /\/\*\s*([^\*]+?)\s*\*\//g, '→$1:' ).replace( /(`|_|\*|~)/g, '\\$1' );
+	return this.replace( /\[\[(?:[^\|\]]+\|)?([^\]]+)\]\]/g, '$1' ).replace( /\/\*\s*([^\*]+?)\s*\*\//g, '→$1:' ).replace( /(`|_|\*|~|<|>)/g, '\\$1' );
 };
 };
 
 
 Discord.Message.prototype.reactEmoji = function(name) {
 Discord.Message.prototype.reactEmoji = function(name) {