소스 검색

Minor improvements

Markus-Rost 4 년 전
부모
커밋
0e6e84cf7b
5개의 변경된 파일84개의 추가작업 그리고 23개의 파일을 삭제
  1. 66 7
      cmds/eval.js
  2. 3 9
      cmds/wiki/general.js
  3. 1 3
      cmds/wiki/random.js
  4. 1 2
      functions/parse_page.js
  5. 13 2
      util/functions.js

+ 66 - 7
cmds/eval.js

@@ -58,12 +58,12 @@ function database(sql, sqlargs = []) {
  */
 function checkWiki(wiki) {
 	wiki = Wiki.fromInput(wiki);
-	return got.get( wiki + 'api.php?&action=query&meta=siteinfo&siprop=general' + ( wiki.isFandom() ? '|variables' : '' ) + '&list=recentchanges&rcshow=!bot&rctype=edit|new|log|categorize&rcprop=ids&rclimit=1&format=json' ).then( response => {
+	return got.get( wiki + 'api.php?&action=query&meta=siteinfo&siprop=general' + ( wiki.isFandom() ? '|variables' : '' ) + '&list=recentchanges&rcshow=!bot&rctype=edit|new|log|categorize&rcprop=ids|timestamp&rclimit=100&format=json' ).then( response => {
 		if ( response.statusCode === 404 && typeof response.body === 'string' ) {
 			let api = cheerio.load(response.body)('head link[rel="EditURI"]').prop('href');
 			if ( api ) {
 				wiki = new Wiki(api.split('api.php?')[0], wiki);
-				return got.get( wiki + 'api.php?action=query&meta=siteinfo&siprop=general' + ( wiki.isFandom() ? '|variables' : '' ) + '&list=recentchanges&rcshow=!bot&rctype=edit|new|log|categorize&rcprop=ids&rclimit=1&format=json' );
+				return got.get( wiki + 'api.php?action=query&meta=siteinfo&siprop=general' + ( wiki.isFandom() ? '|variables' : '' ) + '&list=recentchanges&rcshow=!bot&rctype=edit|new|log|categorize&rcprop=ids|timestamp&rclimit=100&format=json' );
 			}
 		}
 		return response;
@@ -75,17 +75,46 @@ function checkWiki(wiki) {
 		wiki.updateWiki(body.query.general);
 		var result = {
 			wiki: wiki.href,
-			rcid: ( body.query.recentchanges[0]?.rcid || 0 ),
+			activity: [],
+			rcid: 0,
 			wikiid: ( body.query.variables?.find?.( variable => variable?.id === 'wgCityId' )?.['*'] || null ),
 			postid: null
 		}
+		var rc = body.query.recentchanges;
+		if ( rc.length ) {
+			result.rcid = rc[0].rcid;
+			let text = '';
+			let len = ( Date.parse(rc[0].timestamp) - Date.parse(rc[rc.length - 1].timestamp) ) / 60000;
+			len = Math.round(len);
+			let rdays = ( len / 1440 );
+			let days = Math.floor(rdays);
+			if ( days > 0 ) {
+				if ( days === 1 ) text += ` ${days} day`;
+				else text += ` ${days} days`;
+			}
+			let rhours = ( rdays - days ) * 24;
+			let hours = Math.floor(rhours);
+			if ( hours > 0 ) {
+				if ( text.length ) text += ' and';
+				if ( hours === 1 ) text += ` ${hours} hour`;
+				else text += ` ${hours} hours`;
+			}
+			let rminutes = ( rhours - hours ) * 60;
+			let minutes = Math.round(rminutes);
+			if ( minutes > 0 ) {
+				if ( text.length ) text += ' and';
+				if ( minutes === 1 ) text += ` ${minutes} minute`;
+				else text += ` ${minutes} minutes`;
+			}
+			result.activity.push(`${rc.length} edits in${text}`);
+		}
 		return Promise.all([
 			database('SELECT guild, lang, display, rcid, wikiid, postid FROM rcgcdw WHERE wiki = ?', [result.wiki]).then( rows => {
 				result.rcgcdb = rows;
 			}, error => {
 				result.rcgcdb = error.toString();
 			} ),
-			( result.wikiid ? got.get( 'https://services.fandom.com/discussion/' + result.wikiid + '/posts?limit=1&format=json&cache=' + Date.now(), {
+			( result.wikiid ? got.get( 'https://services.fandom.com/discussion/' + result.wikiid + '/posts?limit=100&format=json&cache=' + Date.now(), {
 				headers: {
 					Accept: 'application/hal+json'
 				}
@@ -93,8 +122,36 @@ function checkWiki(wiki) {
 				var dsbody = dsresponse.body;
 				if ( dsresponse.statusCode !== 200 || !dsbody || dsbody.title ) {
 					if ( dsbody?.title !== 'site doesn\'t exists' ) result.postid = dsresponse.statusCode + ': Error while getting the discussions: ' + dsbody?.title;
+					return;
+				}
+				var posts = dsbody._embedded?.['doc:posts'];
+				result.postid = ( posts[0]?.id || 0 );
+				if ( posts?.length ) {
+					let text = '';
+					let len = ( posts[0].creationDate.epochSecond - posts[posts.length - 1].creationDate.epochSecond ) / 60;
+					len = Math.round(len);
+					let rdays = ( len / 1440 );
+					let days = Math.floor(rdays);
+					if ( days > 0 ) {
+						if ( days === 1 ) text += ` ${days} day`;
+						else text += ` ${days} days`;
+					}
+					let rhours = ( rdays - days ) * 24;
+					let hours = Math.floor(rhours);
+					if ( hours > 0 ) {
+						if ( text.length ) text += ' and';
+						if ( hours === 1 ) text += ` ${hours} hour`;
+						else text += ` ${hours} hours`;
+					}
+					let rminutes = ( rhours - hours ) * 60;
+					let minutes = Math.round(rminutes);
+					if ( minutes > 0 ) {
+						if ( text.length ) text += ' and';
+						if ( minutes === 1 ) text += ` ${minutes} minute`;
+						else text += ` ${minutes} minutes`;
+					}
+					result.activity.push(`${posts.length} posts in${text}`);
 				}
-				else result.postid = ( dsbody._embedded?.['doc:posts']?.[0]?.id || 0 );
 			}, error => {
 				result.postid = 'Error while getting the discussions: ' + error;
 			} ) : null )
@@ -218,8 +275,10 @@ function removeSettings(msg) {
 					return dberror;
 				}
 				if ( !row.channel && !all_guilds.includes(row.guild) ) {
-					if ( row.guild in patreons ) msg.client.shard.broadcastEval( `delete global.patreons['${row.guild}']` );
-					if ( row.guild in voice ) delete voice[row.guild];
+					if ( row.guild in patreons || row.guild in voice ) {
+						msg.client.shard.broadcastEval( `delete global.patreons['${row.guild}'];
+						delete global.voice['${row.guild}'];` );
+					}
 					return guilds.push(row.guild);
 				}
 				if ( row.channel && all_guilds.includes(row.guild) && !all_channels.includes(row.channel) ) return channels.push(row.channel);

+ 3 - 9
cmds/wiki/general.js

@@ -264,9 +264,7 @@ function gamepedia_check_wiki(lang, msg, title, wiki, cmd, reaction, spoiler = '
 							if ( !embed.fields.length && querypage.pageprops && querypage.pageprops.infoboxes ) {
 								try {
 									var infobox = JSON.parse(querypage.pageprops.infoboxes)?.[0];
-									if ( infobox?.parser_tag_version === 2 ) infobox.data.forEach( group => {
-										parse_infobox(group, embed, new URL(body.query.general.logo, wiki).href);
-									} );
+									parse_infobox(infobox, embed, new URL(body.query.general.logo, wiki).href);
 								}
 								catch ( error ) {
 									console.log( '- Failed to parse the infobox: ' + error );
@@ -356,9 +354,7 @@ function gamepedia_check_wiki(lang, msg, title, wiki, cmd, reaction, spoiler = '
 				if ( !embed.fields.length && querypage.pageprops && querypage.pageprops.infoboxes ) {
 					try {
 						var infobox = JSON.parse(querypage.pageprops.infoboxes)?.[0];
-						if ( infobox?.parser_tag_version === 2 ) infobox.data.forEach( group => {
-							parse_infobox(group, embed, new URL(body.query.general.logo, wiki).href);
-						} );
+						parse_infobox(infobox, embed, new URL(body.query.general.logo, wiki).href);
 					}
 					catch ( error ) {
 						console.log( '- Failed to parse the infobox: ' + error );
@@ -444,9 +440,7 @@ function gamepedia_check_wiki(lang, msg, title, wiki, cmd, reaction, spoiler = '
 				if ( !embed.fields.length && querypage.pageprops && querypage.pageprops.infoboxes ) {
 					try {
 						var infobox = JSON.parse(querypage.pageprops.infoboxes)?.[0];
-						if ( infobox?.parser_tag_version === 2 ) infobox.data.forEach( group => {
-							parse_infobox(group, embed, '');
-						} );
+						parse_infobox(infobox, embed, '');
 					}
 					catch ( error ) {
 						console.log( '- Failed to parse the infobox: ' + error );

+ 1 - 3
cmds/wiki/random.js

@@ -61,9 +61,7 @@ function gamepedia_random(lang, msg, wiki, reaction, spoiler) {
 			if ( !embed.fields.length && querypage.pageprops && querypage.pageprops.infoboxes ) {
 				try {
 					var infobox = JSON.parse(querypage.pageprops.infoboxes)?.[0];
-					if ( infobox?.parser_tag_version === 2 ) infobox.data.forEach( group => {
-						parse_infobox(group, embed, new URL(body.query.general.logo, wiki).href);
-					} );
+					parse_infobox(infobox, embed, new URL(body.query.general.logo, wiki).href);
 				}
 				catch ( error ) {
 					console.log( '- Failed to parse the infobox: ' + error );

+ 1 - 2
functions/parse_page.js

@@ -44,8 +44,7 @@ function parse_page(msg, title, embed, wiki, thumbnail) {
 	if ( !msg || ( embed.description && embed.thumbnail?.url !== thumbnail && !embed.brokenInfobox ) ) {
 		return;
 	}
-	got.get( wiki + 'api.php?action=parse&prop=text|images|parsewarnings&section=0&disablelimitreport=true&disableeditsection=true&disabletoc=true&page=' + encodeURIComponent( title ) + '&format=json' ).then( response => {
-		if ( response?.body?.parse?.parsewarnings?.length ) log_warn(response.body.parse.parsewarnings);
+	got.get( wiki + 'api.php?action=parse&prop=text|images&section=0&disablelimitreport=true&disableeditsection=true&disabletoc=true&page=' + encodeURIComponent( title ) + '&format=json' ).then( response => {
 		if ( response.statusCode !== 200 || !response?.body?.parse?.text ) {
 			console.log( '- ' + response.statusCode + ': Error while parsing the page: ' + response?.body?.error?.info );
 			return;

+ 13 - 2
util/functions.js

@@ -13,9 +13,20 @@ const got = require('got').extend( {
  * @param {Object} infobox - The content of the infobox.
  * @param {import('discord.js').MessageEmbed} embed - The message embed.
  * @param {String} [thumbnail] - The default thumbnail for the wiki.
+ * @returns {import('discord.js').MessageEmbed?}
  */
 function parse_infobox(infobox, embed, thumbnail) {
-	if ( embed.fields.length >= 25 || embed.length > 5500 ) return;
+	if ( !infobox || embed.fields.length >= 25 || embed.length > 5500 ) return;
+	if ( infobox.parser_tag_version === 2 ) {
+		infobox.data.forEach( group => {
+			parse_infobox(group, embed, thumbnail);
+		} );
+		embed.fields = embed.fields.filter( (field, i, fields) => {
+			if ( field.name !== '\u200b' ) return true;
+			return ( fields[i + 1]?.name && fields[i + 1].name !== '\u200b' );
+		} );
+		return embed;
+	}
 	switch ( infobox.type ) {
 		case 'data':
 			var {label = '', value = '', source = ''} = infobox.data;
@@ -49,7 +60,7 @@ function parse_infobox(infobox, embed, thumbnail) {
 			var image = infobox.data.find( img => {
 				return ( /^(?:https?:)?\/\//.test(img.url) && /\.(?:png|jpg|jpeg|gif)$/.test(img.name) );
 			} );
-			if ( image ) embed.setThumbnail( image.url );
+			if ( image ) embed.setThumbnail( image.url.replace( /^(?:https?:)?\/\//, 'https://' ) );
 			break;
 	}
 }