Skip to content
This repository has been archived by the owner on May 25, 2020. It is now read-only.

Commit

Permalink
Working search
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus-Rost committed Mar 21, 2019
1 parent bb46c40 commit 089ef47
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 55 deletions.
14 changes: 7 additions & 7 deletions i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
{ "cmd": "random", "desc": "I will answer with a link to a random page in the wiki." },
{ "cmd": "🎲", "desc": "I will answer with a link to a random page in the wiki.", "hide": true },
{ "cmd": "page <page name>", "desc": "I will answer with a direct link to the article in the wiki.", "hide": true },
{ "cmd": "search <search term>", "desc": "I will answer with a direct link to the search page for the article in the wiki." },
{ "cmd": "search <search term>", "desc": "I will answer with a direct link to the search page for the article in the wiki.", "hide": true },
{ "cmd": "info", "desc": "I will introduce myself." },
{ "cmd": "help", "desc": "I will list all the commands that I understand." },
{ "cmd": "help <bot command>", "desc": "Wonder how a command works? Let me explain it to you!" },
Expand Down Expand Up @@ -349,7 +349,7 @@
{ "cmd": "🎲", "desc": "Ich antworte mit einem Link auf eine zufällige Seite im Wiki.", "hide": true },
{ "cmd": "seite <Seitenname>", "desc": "Ich antworte mit einem direkten Link zu der angegebenen Seite im Wiki.", "hide": true },
{ "cmd": "page <Seitenname>", "desc": "Ich antworte mit einem direkten Link zu der angegebenen Seite im Wiki.", "hide": true },
{ "cmd": "suche <Suchbegriff>", "desc": "Ich antworte mit einem direkten Link auf die Suchseite zu diesem Begriff im Wiki." },
{ "cmd": "suche <Suchbegriff>", "desc": "Ich antworte mit einem direkten Link auf die Suchseite zu diesem Begriff im Wiki.", "hide": true },
{ "cmd": "search <Suchbegriff>", "desc": "Ich antworte mit einem direkten Link auf die Suchseite zu diesem Begriff im Wiki.", "hide": true },
{ "cmd": "info", "desc": "Ich erzähle etwas über mich." },
{ "cmd": "hilfe", "desc": "Ich liste alle Befehle auf." },
Expand Down Expand Up @@ -546,7 +546,7 @@
{ "cmd": "random", "desc": "Je vais répondre avec un lien vers une page du Wiki au hasard." },
{ "cmd": "🎲", "desc": "Je vais répondre avec un lien vers une page du Wiki au hasard.", "hide": true },
{ "cmd": "page <nom de la page>", "desc": "Je vais répondre avec un lien vers l'article sur Wiki.", "hide": true },
{ "cmd": "chercher <terme de recherche>", "desc": "Je vais répondre avec un lien vers la page de recherche de l'article sur Wiki." },
{ "cmd": "chercher <terme de recherche>", "desc": "Je vais répondre avec un lien vers la page de recherche de l'article sur Wiki.", "hide": true },
{ "cmd": "search <terme de recherche>", "desc": "Je vais répondre avec un lien vers la page de recherche de l'article sur Wiki.", "hide": true },
{ "cmd": "info", "desc": "Je vais me présenter." },
{ "cmd": "aide", "desc": "Je vais lister toutes les commandes que je comprends." },
Expand Down Expand Up @@ -721,7 +721,7 @@
{ "cmd": "🎲", "desc": "Ik zal antwoorden met een link naar een willekeurige pagina uit de wiki.", "hide": true },
{ "cmd": "pagina <pagina naam>", "desc": "Ik zal antwoorden met een rechtstreekse link naar het artikel uit de wiki.", "hide": true },
{ "cmd": "page <pagina naam>", "desc": "Ik zal antwoorden met een rechtstreekse link naar het artikel uit de wiki.", "hide": true },
{ "cmd": "zoeken <zoek term>", "desc": "Ik zal antwoorden met een rechtstreekse link naar de zoekpagina van het artikel uit de wiki." },
{ "cmd": "zoeken <zoek term>", "desc": "Ik zal antwoorden met een rechtstreekse link naar de zoekpagina van het artikel uit de wiki.", "hide": true },
{ "cmd": "search <zoek term>", "desc": "Ik zal antwoorden met een rechtstreekse link naar de zoekpagina van het artikel uit de wiki.", "hide": true },
{ "cmd": "info", "desc": "Ik zal mezelf introduceren." },
{ "cmd": "help", "desc": "Ik zal alle opdrachten opsommen die ik begrijp." },
Expand Down Expand Up @@ -892,7 +892,7 @@
{ "cmd": "🎲", "desc": "Odpowiem linkiem do losowej strony na wiki.", "hide": true },
{ "cmd": "strona <nazwa strony>", "desc": "Odpowiem linkiem do podanego artykułu na Wiki.", "hide": true },
{ "cmd": "page <nazwa strony>", "desc": "Odpowiem linkiem do podanego artykułu na Wiki.", "hide": true },
{ "cmd": "szukaj <wyszukiwana fraza>", "desc": "Odpowiem linkiem do strony wyszukiwania z podaną frazą na Wiki." },
{ "cmd": "szukaj <wyszukiwana fraza>", "desc": "Odpowiem linkiem do strony wyszukiwania z podaną frazą na Wiki.", "hide": true },
{ "cmd": "search <wyszukiwana fraza>", "desc": "Odpowiem linkiem do strony wyszukiwania z podaną frazą na Wiki.", "hide": true },
{ "cmd": "info", "desc": "Przedstawie sie." },
{ "cmd": "pomoc", "desc": "Wylistuję wszystkie komendy, które rozumiem." },
Expand Down Expand Up @@ -1075,7 +1075,7 @@
{ "cmd": "🎲", "desc": "Vou responder com um link para uma página aleatória na wiki.", "hide": true },
{ "cmd": "página <nome da página>", "desc": "Vou responder com um link direto para o artigo na wiki.", "hide": true },
{ "cmd": "page <nome da página>", "desc": "Vou responder com um link direto para o artigo na wiki.", "hide": true },
{ "cmd": "pesquisar <termo de pesquisa>", "desc": "Vou responder com um link direto para a página de busca de artigo na wiki." },
{ "cmd": "pesquisar <termo de pesquisa>", "desc": "Vou responder com um link direto para a página de busca de artigo na wiki.", "hide": true },
{ "cmd": "search <termo de pesquisa>", "desc": "Vou responder com um link direto para a página de busca de artigo na wiki.", "hide": true },
{ "cmd": "info", "desc": "Irei me apresentar." },
{ "cmd": "ajuda", "desc": "Vou listar todos os comandos que entendo." },
Expand Down Expand Up @@ -1257,7 +1257,7 @@
{ "cmd": "🎲", "desc": "Wiki'deki rastgele bir sayfanın bağlantısı ile cevap vereceğim.", "hide": true },
{ "cmd": "sayfa <sayfa adı>", "desc": "I will answer with a direct link to the article in the wiki.", "hide": true },
{ "cmd": "page <sayfa adı>", "desc": "I will answer with a direct link to the article in the wiki.", "hide": true },
{ "cmd": "arama <arama terimi>", "desc": "Wikideki makalenin arama sayfasına doğrudan bir bağlantı ile cevap vereceğim." },
{ "cmd": "arama <arama terimi>", "desc": "Wikideki makalenin arama sayfasına doğrudan bir bağlantı ile cevap vereceğim.", "hide": true },
{ "cmd": "search <arama terimi>", "desc": "Wikideki makalenin arama sayfasına doğrudan bir bağlantı ile cevap vereceğim.", "hide": true },
{ "cmd": "bilgi", "desc": "Kendimi tanıtacağım." },
{ "cmd": "info", "desc": "Kendimi tanıtacağım.", "hide": true },
Expand Down
109 changes: 62 additions & 47 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,65 +651,80 @@ function check_wiki(lang, msg, title, wiki, cmd, reaction, spoiler = '', queryst
}
else if ( ( querypage.missing !== undefined && querypage.known === undefined && !( noRedirect || querypage.categoryinfo ) ) || querypage.invalid !== undefined ) {
request( {
uri: wiki + 'api.php?action=query&prop=imageinfo|categoryinfo&generator=search&gsrnamespace=4|12|14|' + Object.values(body.query.namespaces).filter( ns => ns.content !== undefined ).map( ns => ns.id ).join('|') + '&gsrwhat=nearmatch&gsrlimit=1&gsrsearch=' + encodeURIComponent( title ) + '&format=json',
uri: wiki + 'api/v1/Search/List?minArticleQuality=0&namespaces=4,12,14,' + Object.values(body.query.namespaces).filter( ns => ns.content !== undefined ).map( ns => ns.id ).join(',') + '&limit=1&query=' + encodeURIComponent( title ) + '&format=json',
json: true
}, function( srerror, srresponse, srbody ) {
if ( srbody && srbody.warnings ) log_warn(srbody.warnings);
if ( srerror || !srresponse || srresponse.statusCode !== 200 || !srbody ) {
console.log( '- Fehler beim Erhalten der Suchergebnisse' + ( srerror ? ': ' + srerror : ( srbody ? ( srbody.error ? ': ' + srbody.error.info : '.' ) : '.' ) ) );
msg.sendChannelError( spoiler + '<' + wiki + 'wiki/Special:Search?search=' + encodeURIComponent( title ).replace( /%20/g, '+' ) + '>' + spoiler );
}, function( wserror, wsresponse, wsbody ) {
if ( wserror || !wsresponse || wsresponse.statusCode !== 200 || !wsbody || wsbody.exception || !wsbody.items ) {
if ( wsbody.exception && wsbody.exception.code === 404 ) msg.reactEmoji('🤷');
else {
console.log( '- Fehler beim Erhalten der Suchergebnisse' + ( wserror ? ': ' + wserror : ( wsbody ? ( srbody.exception ? ': ' + wsbody.exception.message : '.' ) : '.' ) ) );
msg.sendChannelError( spoiler + '<' + wiki + 'wiki/Special:Search?search=' + encodeURIComponent( title ).replace( /%20/g, '+' ) + '>' + spoiler );
}

if ( reaction ) reaction.removeEmoji();
}
else {
if ( !srbody.query ) {
msg.reactEmoji('🤷');
querypage = wsbody.items[0];
if ( querypage.ns && !querypage.title.startsWith(body.query.namespaces[querypage.ns]['*'] + ':') ) {
querypage.title = body.query.namespaces[querypage.ns]['*'] + ':' + querypage.title;
}

var text = '';
if ( title.replace( /\-/g, ' ' ).toTitle().toLowerCase() === querypage.title.replace( /\-/g, ' ' ).toTitle().toLowerCase() ) {
text = '';
}
else if ( wsbody.total === 1 ) {
text = '\n' + lang.search.infopage.replaceSave( '%s', '`' + process.env.prefix + cmd + lang.search.page + ' ' + title + linksuffix + '`' );
}
else {
querypage = Object.values(srbody.query.pages)[0];
var pagelink = wiki + 'wiki/' + querypage.title.toTitle() + linksuffix;
var text = '';
var embed = {};
if ( querypage.imageinfo && msg.uploadFiles() && !/\.(?:png|jpg|jpeg|gif)$/.test(querypage.title.toLowerCase()) ) {
var filename = querypage.title.replace( body.query.namespaces['6']['*'] + ':', '' );
embed = {files:[{
attachment: wiki + 'wiki/Special:FilePath/' + filename,
name: ( spoiler ? 'SPOILER ' : '' ) + filename
}]};
}

if ( title.replace( /\-/g, ' ' ).toTitle().toLowerCase() === querypage.title.replace( /\-/g, ' ' ).toTitle().toLowerCase() ) {
text = '';
}
else if ( !srbody.continue ) {
text = '\n' + lang.search.infopage.replaceSave( '%s', '`' + process.env.prefix + cmd + lang.search.page + ' ' + title + linksuffix + '`' );
text = '\n' + lang.search.infosearch.replaceSave( '%1$s', '`' + process.env.prefix + cmd + lang.search.page + ' ' + title + linksuffix + '`' ).replaceSave( '%2$s', '`' + process.env.prefix + cmd + lang.search.search + ' ' + title + linksuffix + '`' );
}
request( {
uri: wiki + 'api.php?action=query&prop=imageinfo|categoryinfo&titles=' + encodeURIComponent( querypage.title ) + '&format=json',
json: true
}, function( srerror, srresponse, srbody ) {
if ( srbody && srbody.warnings ) log_warn(srbody.warnings);
if ( srerror || !srresponse || srresponse.statusCode !== 200 || !srbody || !srbody.query || !srbody.query.pages ) {
console.log( '- Fehler beim Erhalten der Suchergebnisse' + ( srerror ? ': ' + srerror : ( srbody ? ( srbody.error ? ': ' + srbody.error.info : '.' ) : '.' ) ) );
msg.sendChannelError( spoiler + '<' + wiki + 'wiki/' + querypage.title.toTitle() + '>' + spoiler );
}
else {
text = '\n' + lang.search.infosearch.replaceSave( '%1$s', '`' + process.env.prefix + cmd + lang.search.page + ' ' + title + linksuffix + '`' ).replaceSave( '%2$s', '`' + process.env.prefix + cmd + lang.search.search + ' ' + title + linksuffix + '`' );
}

if ( querypage.categoryinfo ) {
var langCategory = lang.search.category;
var category = [langCategory.content];
if ( querypage.categoryinfo.size === 0 ) category.push(langCat.empty);
if ( querypage.categoryinfo.pages > 0 ) {
var pages = querypage.categoryinfo.pages;
category.push(langCategory.pages[( pages in langCategory.pages ? pages : 'default' )].replaceSave( '%s', pages ));
querypage = Object.values(srbody.query.pages)[0];
var pagelink = wiki + 'wiki/' + querypage.title.toTitle() + linksuffix;
var embed = {};
if ( querypage.imageinfo && msg.uploadFiles() && !/\.(?:png|jpg|jpeg|gif)$/.test(querypage.title.toLowerCase()) ) {
var filename = querypage.title.replace( body.query.namespaces['6']['*'] + ':', '' );
embed = {files:[{
attachment: wiki + 'wiki/Special:FilePath/' + filename,
name: ( spoiler ? 'SPOILER ' : '' ) + filename
}]};
}
if ( querypage.categoryinfo.files > 0 ) {
var files = querypage.categoryinfo.files;
category.push(langCategory.files[( files in langCategory.files ? files : 'default' )].replaceSave( '%s', files ));

if ( querypage.categoryinfo ) {
var langCategory = lang.search.category;
var category = [langCategory.content];
if ( querypage.categoryinfo.size === 0 ) category.push(langCat.empty);
if ( querypage.categoryinfo.pages > 0 ) {
var pages = querypage.categoryinfo.pages;
category.push(langCategory.pages[( pages in langCategory.pages ? pages : 'default' )].replaceSave( '%s', pages ));
}
if ( querypage.categoryinfo.files > 0 ) {
var files = querypage.categoryinfo.files;
category.push(langCategory.files[( files in langCategory.files ? files : 'default' )].replaceSave( '%s', files ));
}
if ( querypage.categoryinfo.subcats > 0 ) {
var subcats = querypage.categoryinfo.subcats;
category.push(langCategory.subcats[( subcats in langCategory.subcats ? subcats : 'default' )].replaceSave( '%s', subcats ));
}
text += '\n\n' + category.join('\n');
}
if ( querypage.categoryinfo.subcats > 0 ) {
var subcats = querypage.categoryinfo.subcats;
category.push(langCategory.subcats[( subcats in langCategory.subcats ? subcats : 'default' )].replaceSave( '%s', subcats ));
}
text += '\n\n' + category.join('\n');

msg.sendChannel( spoiler + pagelink + text + spoiler, embed );
}

msg.sendChannel( spoiler + pagelink + text + spoiler, embed );
}
if ( reaction ) reaction.removeEmoji();
} );
}

if ( reaction ) reaction.removeEmoji();
} );
}
else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fandom-wiki-bot",
"version": "1.3.0",
"version": "1.4.0",
"description": "Wikia-Bot is a bot for Discord with the purpose to easily link to Fandom wikis. He resolves redirects and follows interwiki links.",
"main": "main.js",
"scripts": {
Expand Down

0 comments on commit 089ef47

Please sign in to comment.