浏览代码

multiple minor improvements

Markus-Rost 4 年之前
父节点
当前提交
30935ebac3
共有 4 个文件被更改,包括 34 次插入10 次删除
  1. 3 2
      cmds/wiki/general.js
  2. 19 1
      util/functions.js
  3. 5 1
      util/logging.js
  4. 7 6
      util/newMessage.js

+ 3 - 2
cmds/wiki/general.js

@@ -1,7 +1,7 @@
 const {MessageEmbed} = require('discord.js');
 const parse_page = require('../../functions/parse_page.js');
 const logging = require('../../util/logging.js');
-const {parse_infobox, htmlToPlain, htmlToDiscord} = require('../../util/functions.js');
+const {parse_infobox, htmlToPlain, htmlToDiscord, partialURIdecode} = require('../../util/functions.js');
 const extract_desc = require('../../util/extract_desc.js');
 const {limit: {interwiki: interwikiLimit}, wikiProjects} = require('../../util/default.json');
 const Wiki = require('../../util/wiki.js');
@@ -44,7 +44,7 @@ fs.readdir( './cmds/minecraft', (error, files) => {
 function gamepedia_check_wiki(lang, msg, title, wiki, cmd, reaction, spoiler = '', querystring = new URLSearchParams(), fragment = '', interwiki = '', selfcall = 0) {
 	var full_title = title;
 	if ( title.includes( '#' ) ) {
-		fragment = title.split('#').slice(1).join('#');
+		fragment = title.split('#').slice(1).join('#').replace( /(?:%[\dA-F]{2})+/g, partialURIdecode );
 		title = title.split('#')[0];
 	}
 	if ( /\?\w+=/.test(title) ) {
@@ -52,6 +52,7 @@ function gamepedia_check_wiki(lang, msg, title, wiki, cmd, reaction, spoiler = '
 		querystring = new URLSearchParams(querystring + '&' + title.substring(querystart + 1));
 		title = title.substring(0, querystart);
 	}
+	title = title.replace( /(?:%[\dA-F]{2})+/g, partialURIdecode );
 	if ( title.length > 250 ) {
 		title = title.substring(0, 250);
 		msg.reactEmoji('⚠️');

+ 19 - 1
util/functions.js

@@ -350,6 +350,23 @@ function limitLength(text = '', limit = 1000, maxExtra = 20) {
 	return text.substring(0, limit) + suffix;
 };
 
+/**
+ * Try to URI decode.
+ * @param {String} m - The character to decode.
+ * @returns {String}
+ */
+function partialURIdecode(m) {
+	var text = '';
+	try {
+		text = decodeURIComponent( m );
+	}
+	catch ( replaceError ) {
+		if ( isDebug ) console.log( '- Failed to decode ' + m + ':' + replaceError );
+		text = m;
+	}
+	return text;
+};
+
 module.exports = {
 	got,
 	parse_infobox,
@@ -359,5 +376,6 @@ module.exports = {
 	htmlToPlain,
 	htmlToDiscord,
 	escapeFormatting,
-	limitLength
+	limitLength,
+	partialURIdecode
 };

+ 5 - 1
util/logging.js

@@ -6,6 +6,10 @@ if ( !process.env.usagelog ) {
 const fs = require('fs');
 const usageLog = fs.createWriteStream(process.env.usagelog, {flags:'a'});
 
+usageLog.on( 'error', (error) => {
+	console.log( '- ' + shardId + ': Error while logging the usage: ' + error );
+} );
+
 /**
  * Log wikis by usage.
  * @param {import('./wiki.js')} wiki - The wiki.
@@ -13,7 +17,7 @@ const usageLog = fs.createWriteStream(process.env.usagelog, {flags:'a'});
  * @returns {Boolean}
  */
 function logging(wiki, ...notes) {
-	return usageLog.write( `${new Date().toISOString()} ${wiki.href} ${notes.join(' ')}\n`, 'utf8' );
+	return usageLog.write( `${new Date().toISOString()}\t${wiki.href}\t${notes.join('\t')}\n`, 'utf8' );
 }
 
 module.exports = logging;

+ 7 - 6
util/newMessage.js

@@ -1,5 +1,6 @@
 const {Util} = require('discord.js');
 const logging = require('./logging.js');
+const {partialURIdecode} = require('./functions.js');
 const {limit: {command: commandLimit}, defaultSettings, wikiProjects} = require('./default.json');
 const Wiki = require('./wiki.js');
 const check_wiki = {
@@ -52,7 +53,7 @@ function newMessage(msg, lang, wiki = defaultSettings.wiki, prefix = process.env
 	var count = 0;
 	var maxcount = commandLimit[( msg?.guild?.id in patreons ? 'patreon' : 'default' )];
 	var breakLines = false;
-	cleanCont.replace( /\u200b/g, '' ).replace( /(?<!\\)```.+?```/gs, '<codeblock>' ).split('\n').forEach( line => {
+	cleanCont.replace( /\u200b/g, '' ).replace( /<a?(:\w+:)\d+>/g, '$1' ).replace( /(?<!\\)```.+?```/gs, '<codeblock>' ).split('\n').forEach( line => {
 		if ( line.startsWith( '>>> ' ) ) breakLines = true;
 		if ( !line.hasPrefix(prefix) || breakLines || count > maxcount ) return;
 		count++;
@@ -108,7 +109,7 @@ function newMessage(msg, lang, wiki = defaultSettings.wiki, prefix = process.env
 		var linkcount = 0;
 		var linkmaxcount = maxcount + 5;
 		var breakInline = false;
-		cleanCont.replace( /\u200b/g, '' ).replace( /(?<!\\)```.+?```/gs, '<codeblock>' ).replace( /(?<!\\)`.+?`/gs, '<code>' ).split('\n').forEach( line => {
+		cleanCont.replace( /\u200b/g, '' ).replace( /<a?(:\w+:)\d+>/g, '$1' ).replace( /(?<!\\)```.+?```/gs, '<codeblock>' ).replace( /(?<!\\)`.+?`/gs, '<code>' ).split('\n').forEach( line => {
 			if ( line.startsWith( '>>> ' ) ) breakInline = true;
 			if ( line.startsWith( '> ' ) || breakInline ) return;
 			if ( line.hasPrefix(prefix) || !( line.includes( '[[' ) || line.includes( '{{' ) ) ) return;
@@ -119,8 +120,8 @@ function newMessage(msg, lang, wiki = defaultSettings.wiki, prefix = process.env
 					if ( linkcount < linkmaxcount ) {
 						linkcount++;
 						console.log( ( channel.isGuild() ? msg.guild.id : '@' + author.id ) + ': ' + entry[0] );
-						let title = entry[2].split('#')[0];
-						let section = ( entry[2].includes( '#' ) ? entry[2].split('#').slice(1).join('#') : '' )
+						let title = entry[2].split('#')[0].replace( /(?:%[\dA-F]{2})+/g, partialURIdecode );
+						let section = ( entry[2].includes( '#' ) ? entry[2].split('#').slice(1).join('#').replace( /(?:%[\dA-F]{2})+/g, partialURIdecode ) : '' );
 						links.push({title,section,spoiler:entry[1]});
 					}
 					else if ( linkcount === linkmaxcount ) {
@@ -139,8 +140,8 @@ function newMessage(msg, lang, wiki = defaultSettings.wiki, prefix = process.env
 					if ( count < maxcount ) {
 						count++;
 						console.log( ( channel.isGuild() ? msg.guild.id : '@' + author.id ) + ': ' + entry[0] );
-						let title = entry[2].split('#')[0];
-						let section = ( entry[2].includes( '#' ) ? entry[2].split('#').slice(1).join('#') : '' )
+						let title = entry[2].split('#')[0].replace( /(?:%[\dA-F]{2})+/g, partialURIdecode );
+						let section = ( entry[2].includes( '#' ) ? entry[2].split('#').slice(1).join('#').replace( /(?:%[\dA-F]{2})+/g, partialURIdecode ) : '' );
 						embeds.push({title,section,spoiler:entry[1]});
 					}
 					else if ( count === maxcount ) {