123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- const htmlparser = require('htmlparser2');
- const {MessageEmbed, Util} = require('discord.js');
- const {limit: {discussion: discussionLimit}} = require('../util/default.json');
- /**
- * Processes discussion commands.
- * @param {import('../util/i18n.js')} lang - The user language.
- * @param {import('discord.js').Message} msg - The Discord message.
- * @param {import('../util/wiki.js')} wiki - The wiki for the page.
- * @param {String} title - The title of the discussion post.
- * @param {Object} query - The siteinfo from the wiki.
- * @param {import('discord.js').MessageReaction} reaction - The reaction on the message.
- * @param {String} spoiler - If the response is in a spoiler.
- */
- function fandom_discussion(lang, msg, wiki, title, query, reaction, spoiler) {
- var limit = discussionLimit[( msg?.guild?.id in patreons ? 'patreon' : 'default' )];
- if ( !title ) {
- var pagelink = wiki + 'f';
- var embed = new MessageEmbed().setAuthor( query.general.sitename ).setTitle( lang.get('discussion.main') ).setURL( pagelink );
- got.get( wiki + 'f', {
- responseType: 'text'
- } ).then( descresponse => {
- var descbody = descresponse.body;
- if ( descresponse.statusCode !== 200 || !descbody ) {
- console.log( '- ' + descresponse.statusCode + ': Error while getting the description.' );
- } else {
- var thumbnail = wiki.toLink('Special:FilePath/Wiki-wordmark.png');
- var parser = new htmlparser.Parser( {
- onopentag: (tagname, attribs) => {
- if ( tagname === 'meta' && attribs.property === 'og:description' ) {
- var description = attribs.content.escapeFormatting();
- if ( description.length > 1000 ) description = description.substring(0, 1000) + '\u2026';
- embed.setDescription( description );
- }
- if ( tagname === 'meta' && attribs.property === 'og:image' ) {
- thumbnail = attribs.content;
- }
- }
- } );
- parser.write( descbody );
- parser.end();
- embed.setThumbnail( thumbnail );
- }
- }, error => {
- console.log( '- Error while getting the description: ' + error );
- } ).finally( () => {
- msg.sendChannel( spoiler + '<' + pagelink + '>' + spoiler, {embed} );
-
- if ( reaction ) reaction.removeEmoji();
- } );
- }
- else if ( !query.wikidesc?.id ) {
- console.log( '- Error while getting the wiki id.' );
- msg.sendChannelError( spoiler + '<' + wiki + 'f' + '>' + spoiler );
-
- if ( reaction ) reaction.removeEmoji();
- return;
- }
- else if ( title.split(' ')[0].toLowerCase() === 'post' || title.split(' ')[0].toLowerCase() === lang.get('discussion.post') ) {
- title = title.split(' ').slice(1).join(' ');
- got.get( 'https://services.fandom.com/discussion/' + query.wikidesc.id + '/posts?limit=' + limit + '&format=json&cache=' + Date.now(), {
- headers: {
- Accept: 'application/hal+json'
- }
- } ).then( response => {
- var body = response.body;
- if ( response.statusCode !== 200 || !body || body.title || !body._embedded || !body._embedded['doc:posts'] ) {
- console.log( '- ' + response.statusCode + ': Error while getting the posts: ' + ( body && body.title ) );
- msg.sendChannelError( spoiler + '<' + wiki + 'f' + '>' + spoiler );
-
- if ( reaction ) reaction.removeEmoji();
- }
- else if ( body._embedded['doc:posts'].length ) {
- var posts = body._embedded['doc:posts'];
- var embed = new MessageEmbed().setAuthor( query.general.sitename );
-
- if ( posts.some( post => post.id === title ) ) {
- discussion_send(lang, msg, wiki, posts.find( post => post.id === title ), embed, spoiler);
-
- if ( reaction ) reaction.removeEmoji();
- }
- else if ( /^\d+$/.test(title) ) {
- got.get( 'https://services.fandom.com/discussion/' + query.wikidesc.id + '/posts/' + title + '?format=json&cache=' + Date.now(), {
- headers: {
- Accept: 'application/hal+json'
- }
- } ).then( presponse => {
- var pbody = presponse.body;
- if ( presponse.statusCode !== 200 || !pbody || pbody.id !== title ) {
- if ( pbody && pbody.title === 'The requested resource was not found.' ) {
- if ( posts.some( post => post.rawContent.toLowerCase().includes( title.toLowerCase() ) ) ) {
- discussion_send(lang, msg, wiki, posts.find( post => post.rawContent.toLowerCase().includes( title.toLowerCase() ) ), embed, spoiler);
- }
- else msg.reactEmoji('🤷');
- }
- else {
- console.log( '- ' + presponse.statusCode + ': Error while getting the post: ' + ( pbody && pbody.title ) );
- msg.sendChannelError( spoiler + '<' + wiki + 'f' + '>' + spoiler );
- }
-
- if ( reaction ) reaction.removeEmoji();
- }
- else if ( pbody.title ) {
- discussion_send(lang, msg, wiki, pbody, embed, spoiler);
-
- if ( reaction ) reaction.removeEmoji();
- }
- else got.get( 'https://services.fandom.com/discussion/' + query.wikidesc.id + '/threads/' + pbody.threadId + '?format=json&cache=' + Date.now(), {
- headers: {
- Accept: 'application/hal+json'
- }
- } ).then( thresponse => {
- var thbody = thresponse.body;
- if ( thresponse.statusCode !== 200 || !thbody || thbody.id !== pbody.threadId ) {
- console.log( '- ' + thresponse.statusCode + ': Error while getting the thread: ' + ( thbody && thbody.title ) );
- embed.setTitle( '~~' + pbody.threadId + '~~' );
- }
- else embed.setTitle( thbody.title.escapeFormatting() );
- }, error => {
- console.log( '- Error while getting the thread: ' + error );
- embed.setTitle( '~~' + pbody.threadId + '~~' );
- } ).finally( () => {
- discussion_send(lang, msg, wiki, pbody, embed, spoiler);
-
- if ( reaction ) reaction.removeEmoji();
- } );
- }, error => {
- console.log( '- Error while getting the post: ' + error );
- msg.sendChannelError( spoiler + '<' + wiki + 'f' + '>' + spoiler );
-
- if ( reaction ) reaction.removeEmoji();
- } );
- }
- else if ( posts.some( post => post.rawContent.toLowerCase().includes( title.toLowerCase() ) ) ) {
- discussion_send(lang, msg, wiki, posts.find( post => post.rawContent.toLowerCase().includes( title.toLowerCase() ) ), embed, spoiler);
-
- if ( reaction ) reaction.removeEmoji();
- }
- else {
- msg.reactEmoji('🤷');
-
- if ( reaction ) reaction.removeEmoji();
- }
- }
- else {
- msg.reactEmoji('🤷');
-
- if ( reaction ) reaction.removeEmoji();
- }
- }, error => {
- console.log( '- Error while getting the posts: ' + error );
- msg.sendChannelError( spoiler + '<' + wiki + 'f' + '>' + spoiler );
-
- if ( reaction ) reaction.removeEmoji();
- } );
- }
- else {
- got.get( 'https://services.fandom.com/discussion/' + query.wikidesc.id + '/threads?sortKey=trending&limit=' + limit + '&format=json&cache=' + Date.now(), {
- headers: {
- Accept: 'application/hal+json'
- }
- } ).then( response => {
- var body = response.body;
- if ( response.statusCode !== 200 || !body || body.title || !body._embedded || !body._embedded.threads ) {
- console.log( '- ' + response.statusCode + ': Error while getting the threads: ' + ( body && body.title ) );
- msg.sendChannelError( spoiler + '<' + wiki + 'f' + '>' + spoiler );
-
- if ( reaction ) reaction.removeEmoji();
- }
- else if ( body._embedded.threads.length ) {
- var threads = body._embedded.threads;
- var embed = new MessageEmbed().setAuthor( query.general.sitename );
-
- if ( threads.some( thread => thread.id === title ) ) {
- discussion_send(lang, msg, wiki, threads.find( thread => thread.id === title ), embed, spoiler);
-
- if ( reaction ) reaction.removeEmoji();
- }
- else if ( threads.some( thread => thread.title === title ) ) {
- discussion_send(lang, msg, wiki, threads.find( thread => thread.title === title ), embed, spoiler);
-
- if ( reaction ) reaction.removeEmoji();
- }
- else if ( threads.some( thread => thread.title.toLowerCase() === title.toLowerCase() ) ) {
- discussion_send(lang, msg, wiki, threads.find( thread => thread.title.toLowerCase() === title.toLowerCase() ), embed, spoiler);
-
- if ( reaction ) reaction.removeEmoji();
- }
- else if ( threads.some( thread => thread.title.includes( title ) ) ) {
- discussion_send(lang, msg, wiki, threads.find( thread => thread.title.includes( title ) ), embed, spoiler);
-
- if ( reaction ) reaction.removeEmoji();
- }
- else if ( threads.some( thread => thread.title.toLowerCase().includes( title.toLowerCase() ) ) ) {
- discussion_send(lang, msg, wiki, threads.find( thread => thread.title.toLowerCase().includes( title.toLowerCase() ) ), embed, spoiler);
-
- if ( reaction ) reaction.removeEmoji();
- }
- else if ( /^\d+$/.test(title) ) {
- got.get( 'https://services.fandom.com/discussion/' + query.wikidesc.id + '/threads/' + title + '?format=json&cache=' + Date.now(), {
- headers: {
- Accept: 'application/hal+json'
- }
- } ).then( thresponse => {
- var thbody = thresponse.body;
- if ( thresponse.statusCode !== 200 || !thbody || thbody.id !== title ) {
- if ( thbody && thbody.status === 404 ) {
- if (threads.some( thread => thread.rawContent.toLowerCase().includes( title.toLowerCase() ) ) ) {
- discussion_send(lang, msg, wiki, threads.find( thread => thread.rawContent.toLowerCase().includes( title.toLowerCase() ) ), embed, spoiler);
- }
- else msg.reactEmoji('🤷');
- }
- else {
- console.log( '- ' + thresponse.statusCode + ': Error while getting the thread: ' + ( thbody && thbody.title ) );
- msg.sendChannelError( spoiler + '<' + wiki + 'f/p/' + title + '>' + spoiler );
- }
- }
- else discussion_send(lang, msg, wiki, thbody, embed, spoiler);
- }, error => {
- console.log( '- Error while getting the thread: ' + error );
- msg.sendChannelError( spoiler + '<' + wiki + 'f/p/' + title + '>' + spoiler );
- } ).finally( () => {
- if ( reaction ) reaction.removeEmoji();
- } );
- }
- else if ( threads.some( thread => thread.rawContent.toLowerCase().includes( title.toLowerCase() ) ) ) {
- discussion_send(lang, msg, wiki, threads.find( thread => thread.rawContent.toLowerCase().includes( title.toLowerCase() ) ), embed, spoiler);
-
- if ( reaction ) reaction.removeEmoji();
- }
- else {
- msg.reactEmoji('🤷');
-
- if ( reaction ) reaction.removeEmoji();
- }
- }
- else {
- msg.reactEmoji('🤷');
-
- if ( reaction ) reaction.removeEmoji();
- }
- }, error => {
- console.log( '- Error while getting the threads: ' + error );
- msg.sendChannelError( spoiler + '<' + wiki + 'f' + '>' + spoiler );
-
- if ( reaction ) reaction.removeEmoji();
- } );
- }
- }
- /**
- * Send discussion posts.
- * @param {import('../util/i18n.js')} lang - The user language.
- * @param {import('discord.js').Message} msg - The Discord message.
- * @param {import('../util/wiki.js')} wiki - The wiki for the page.
- * @param {Object} discussion - The discussion post.
- * @param {import('discord.js').MessageEmbed} embed - The embed for the page.
- * @param {String} spoiler - If the response is in a spoiler.
- */
- function discussion_send(lang, msg, wiki, discussion, embed, spoiler) {
- if ( discussion.title ) {
- embed.setTitle( discussion.title.escapeFormatting() );
- var pagelink = wiki + 'f/p/' + ( discussion.threadId || discussion.id );
- }
- else {
- if ( discussion._embedded.thread ) embed.setTitle( discussion._embedded.thread[0].title.escapeFormatting() );
- var pagelink = wiki + 'f/p/' + discussion.threadId + '/r/' + discussion.id;
- }
- var text = '<' + pagelink + '>';
- embed.setURL( pagelink ).setFooter( discussion.createdBy.name, discussion.createdBy.avatarUrl ).setTimestamp( discussion.creationDate.epochSecond * 1000 );
- var description = '';
- switch ( discussion.funnel ) {
- case 'IMAGE':
- embed.setImage( discussion._embedded.contentImages[0].url );
- break;
- case 'POLL':
- discussion.poll.answers.forEach( answer => embed.addField( answer.text.escapeFormatting(), ( answer.image ? '[__' + lang.get('discussion.image').escapeFormatting() + '__](' + answer.image.url + ')\n' : '' ) + lang.get('discussion.votes', answer.votes), true ) );
- break;
- case 'QUIZ':
- description = discussion._embedded.quizzes[0].title.escapeFormatting();
- embed.setThumbnail( discussion._embedded.quizzes[0].image );
- break;
- default:
- if ( discussion.jsonModel ) {
- try {
- description = discussion_formatting(JSON.parse(discussion.jsonModel)).replace( /(?:\*\*\*\*|(?<!\\)\_\_)/g, '' ).replace( /{@wiki}/g, wiki );
- if ( discussion._embedded.contentImages.length ) {
- if ( description.trim().endsWith( '{@0}' ) ) {
- embed.setImage( discussion._embedded.contentImages[0].url );
- description = description.replace( '{@0}', '' ).trim();
- }
- else {
- description = description.replace( /\{\@(\d+)\}/g, (match, n) => {
- if ( n >= discussion._embedded.contentImages.length ) return '';
- else return '[__' + lang.get('discussion.image').escapeFormatting() + '__](' + discussion._embedded.contentImages[n].url + ')';
- } );
- embed.setThumbnail( discussion._embedded.contentImages[0].url );
- }
- }
- else embed.setThumbnail( wiki.toLink('Special:FilePath/Wiki-wordmark.png') );
- }
- catch ( jsonerror ) {
- console.log( '- Error while getting the formatting: ' + jsonerror );
- description = discussion.rawContent.escapeFormatting();
- if ( discussion._embedded.contentImages.length ) embed.setThumbnail( discussion._embedded.contentImages[0].url );
- }
- }
- else if ( discussion.renderedContent ) {
- var current_tag = '';
- var parser = new htmlparser.Parser( {
- onopentag: (tagname, attribs) => {
- if ( tagname === 'a' ) {
- current_tag = attribs.href;
- description += '[';
- }
- },
- ontext: (htmltext) => {
- description += htmltext.escapeFormatting();
- },
- onclosetag: (tagname) => {
- if ( tagname === 'a' ) {
- description += '](' + current_tag + ')';
- current_tag = '';
- }
- if ( tagname === 'p' ) description += '\n';
- }
- } );
- parser.write( discussion.renderedContent );
- parser.end();
- if ( discussion._embedded.contentImages.length ) embed.setThumbnail( discussion._embedded.contentImages[0].url );
- }
- else {
- description = discussion.rawContent.escapeFormatting();
- if ( discussion._embedded.contentImages.length ) embed.setThumbnail( discussion._embedded.contentImages[0].url );
- }
- }
- if ( description.length > 2000 ) description = description.substring(0, 2000) + '\u2026';
- embed.setDescription( description );
- if ( discussion.tags?.length ) {
- embed.addField( lang.get('discussion.tags'), Util.splitMessage( discussion.tags.map( tag => '[' + tag.articleTitle.escapeFormatting() + '](' + wiki.toLink(tag.articleTitle, '', '', true) + ')' ).join(', '), {char:', ',maxLength:1000} )[0], false );
- }
-
- msg.sendChannel( spoiler + text + spoiler, {embed} );
- }
- /**
- * Format discussion content
- * @param {Object} jsonModel - The content of the discussion post.
- * @returns {String}
- */
- function discussion_formatting(jsonModel) {
- var description = '';
- switch ( jsonModel.type ) {
- case 'doc':
- if ( jsonModel.content ) jsonModel.content.forEach( content => description += discussion_formatting(content) );
- break;
- case 'paragraph':
- if ( jsonModel.content ) jsonModel.content.forEach( content => description += discussion_formatting(content) );
- description += '\n';
- break;
- case 'openGraph':
- if ( !jsonModel.attrs.wasAddedWithInlineLink ) description += jsonModel.attrs.url + '\n';
- break;
- case 'text':
- var prepend = '';
- var append = '';
- if ( jsonModel.marks ) {
- jsonModel.marks.forEach( mark => {
- switch ( mark.type ) {
- case 'mention':
- prepend += '[';
- append = ']({@wiki}f/u/' + mark.attrs.userId + ')' + append;
- break;
- case 'link':
- prepend += '[';
- append = '](' + mark.attrs.href + ')' + append;
- break;
- case 'strong':
- prepend += '**';
- append = '**' + append;
- break;
- case 'em':
- prepend += '_';
- append = '_' + append;
- break;
- }
- } );
- }
- description += prepend + jsonModel.text.escapeFormatting() + append;
- break;
- case 'image':
- if ( jsonModel.attrs.id !== null ) description += '{@' + jsonModel.attrs.id + '}\n';
- break;
- case 'code_block':
- description += '```\n';
- if ( jsonModel.content ) jsonModel.content.forEach( content => description += discussion_formatting(content) );
- description += '\n```\n';
- break;
- case 'bulletList':
- jsonModel.content.forEach( listItem => {
- description += '\t• ';
- if ( listItem.content ) listItem.content.forEach( content => description += discussion_formatting(content) );
- } );
- break;
- case 'orderedList':
- var n = 1;
- jsonModel.content.forEach( listItem => {
- description += '\t' + n + '. ';
- n++;
- if ( listItem.content ) listItem.content.forEach( content => description += discussion_formatting(content) );
- } );
- break;
- }
- return description;
- }
- module.exports = fandom_discussion;
|