Advertisement
Guest User

Untitled

a guest
Jul 12th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.58 KB | None | 0 0
  1. "anime": {
  2.  
  3. desc: "Gets details on an anime from MAL. Do ``" + config.command_prefix + "anime --help`` for more info",
  4. usage: "<anime name> [--help] [--recent | --popular | --airing | --unreleased]",
  5. deleteCommand: true,
  6. cooldown: 6,
  7. process: function(bot, msg, suffix) {
  8. if (suffix) {
  9. //add function for recent and popular, if both aren't set then do default recent
  10. var argv = yargs.parse(suffix);
  11. var bRecent = false;
  12. var bPopular = false;
  13. var bAiring = false;
  14. var bShowAllReleasedUnreleased = true;
  15. var strSearch = argv._.join(' ');
  16. if(argv.h || argv.help)
  17. {
  18. var helpMsg = [];
  19. helpMsg.push(":information_source: **Anime Search Help\n**");
  20. helpMsg.push("**Usage**: ``" + config.command_prefix + "anime <search_term> [ --recent | --popular | --airing | --unreleased]``");
  21. helpMsg.push("\n**Optional Flags:**\n Tags to include to search/filter.");
  22. helpMsg.push(":black_small_square:``--help / -h`` | Display this help");
  23. helpMsg.push(":black_small_square:``--recent / -r`` | Sort by most recent and return first entry based on the Start_Date. May show unreleased unless otherwise specified");
  24. helpMsg.push(":black_small_square:``--popular / -p`` | Sort by score and return first entry");
  25. helpMsg.push(":black_small_square:``--airing / --aired / -a`` | Displays only animes currently airing/already aired");
  26. helpMsg.push(":black_small_square:``--unreleased / -u`` | Displays only animes that aren't aired yet\n");
  27. helpMsg.push("NOTE: Using both ``--unreleased`` and ``--airing`` will pull the first result returned by the API unfiltered. Results may be inaccurate in this case.")
  28. bot.sendMessage(msg.author, helpMsg);
  29. return;
  30. }
  31. else
  32. {
  33. if(argv.r || argv.recent) bRecent = !bRecent; //recent, sortby start_date (airing date start)
  34. if(argv.p || argv.popular) bPopular = !bPopular; //popular, sortby score
  35. if((argv.a || argv.airing) && (argv.u || argv.unreleased)){
  36. bShowAllReleasedUnreleased = true;
  37. }
  38. else{
  39. if(argv.a || argv.airing || argv.aired){
  40. bShowAllReleasedUnreleased = false;
  41. bAiring = true; //airing/released
  42. }
  43. if(argv.u || argv.unreleased){
  44. bShowAllReleasedUnreleased = false;
  45. bAiring = false; //unaired/unreleased
  46. }
  47. }
  48.  
  49. if (!MAL_USER || !MAL_PASS || MAL_USER == "" || MAL_PASS =="") { bot.sendMessage(msg, "MAL login not configured by bot owner", function(erro, wMessage) { bot.deleteMessage(wMessage, {"wait": 8000}); }); return; }
  50. bot.startTyping(msg.channel);
  51. var tags = ent.encodeHTML(strSearch);
  52. var rUrl = "http://myanimelist.net/api/anime/search.xml?q=" + tags;
  53. request(rUrl, {"auth": {"user": MAL_USER, "pass": MAL_PASS, "sendImmediately": false}}, function(error, response, body) {
  54. if (error) console.log(error);
  55. else if (!error && response.statusCode == 200) {
  56. //FALZ -- MY MORE RELEVANT SEARCH ALGO, BECAUSE WHO WATCHES 1999 ANIMES (jk cowboy bebop is awesome)
  57. async.waterfall([
  58. function getSearchAPIResult(done)
  59. {
  60. xml2js.parseString(body, function(err, result){
  61. done(null, result.anime.entry);
  62. });
  63. },
  64. function filterResult(result, done)
  65. {
  66. if(!bShowAllReleasedUnreleased)
  67. {
  68. var res = [];
  69. if(bAiring){
  70. for(var i = 0; i < result.length; i++)
  71. {
  72. if(result[i].status != "Not yet aired")
  73. {
  74. res.push(result[i]);
  75. }
  76. }
  77. }else{
  78. for(var i = 0; i < result.length; i++)
  79. {
  80. if(result[i].status == "Not yet aired")
  81. {
  82. res.push(result[i]);
  83. }
  84. }
  85. }
  86. if(res.length <= 0) done(new Error("No results!"));
  87. else done(null, res);
  88. }
  89. else done(null, result);
  90. },
  91. function sortResult(result, done)
  92. {
  93. if(bRecent)
  94. {
  95. result.sort(firstBy(function(v){
  96. var vnow = moment();
  97. var vdate = moment(v.start_date);
  98. return vnow.diff(vdate, 'days');
  99. }));
  100. }
  101. else if(bPopular)
  102. {
  103. result.sort(firstBy(function(v){
  104. var vscore = parseFloat(v.score);
  105. return vscore;
  106. }, -1));
  107. }
  108. else if(bRecent && bPopular)
  109. {
  110. result.sort(
  111. firstBy(function(v){
  112. var vnow = moment();
  113. var vdate = moment(v.start_date);
  114. //console.log("RECENTPOP: v->"+vdate);
  115. return vnow.diff(vdate, 'days');
  116. }).thenBy(function(v1, v2){
  117. var vscore = parseFloat(v.score);
  118. console.log("RECENTPOP SCORE: v->"+vscore);
  119. return vscore;
  120. }, -1));
  121. }
  122. else{
  123. //do nothing :D
  124. }
  125. done(null, result);
  126. },
  127. function sendResult(result, done)
  128. {
  129. var title = result[0].title;
  130. var english = result[0].english;
  131. var ep = result[0].episodes;
  132. var score = result[0].score;
  133. var type = result[0].type;
  134. var status = result[0].status;
  135. var synopsis = result[0].synopsis.toString();
  136. var id = result[0].id;
  137. var start_date = result[0].start_date;
  138. if(start_date === "0000-00-00") start_date = "N/A";
  139. var end_date = result[0].end_date;
  140. if(end_date === "0000-00-00") end_date = "N/A";
  141. synopsis = synopsis.replace(/<br \/>/g, " "); synopsis = synopsis.replace(/\[(.{1,10})\]/g, "");
  142. synopsis = synopsis.replace(/\r?\n|\r/g, " "); synopsis = synopsis.replace(/\[(i|\/i)\]/g, "*"); synopsis = synopsis.replace(/\[(b|\/b)\]/g, "**");
  143. synopsis = ent.decodeHTML(synopsis);
  144. if (!msg.channel.isPrivate) {
  145. if (synopsis.length > 400) { synopsis = synopsis.substring(0, 400); synopsis += "..."; }
  146. }
  147. var toSend = ":tv: "
  148. + "**"
  149. + title
  150. + " / "
  151. + english
  152. + "**\n**Type:** "
  153. + type
  154. + " **| Episodes:** "
  155. + ep
  156. + " **| Status:** "
  157. + status
  158. + " **| Start:** "
  159. + start_date
  160. + " **| End:** "
  161. + end_date
  162. + " **| Score:** "
  163. + score
  164. + "\n"
  165. + synopsis
  166. + "\n**http://www.myanimelist.net/anime/"
  167. + id
  168. + "**";
  169. bot.sendMessage(msg, toSend);
  170. //bot.sendMessage(msg, ":tv: " + "**" + title + " / " + english + "**\n**Type:** " + type + " **| Episodes:** " + ep + " **| Status:** " + status + " **| Score:** " + score + "\n" + synopsis + "\n**http://www.myanimelist.net/anime/" + id + "**");
  171. done(null);
  172. }
  173. ], function(err, result){
  174. //do nothing :D
  175. if(err) bot.sendMessage(msg, "Your anime/manga was not found!\n*I blame the MAL database for not having the anime you're looking for! I-Its not my fault okay?!*");
  176. });
  177. } else bot.sendMessage(msg, "\"" + strSearch + "\" not found! \n*I blame the MAL database for not having the anime you're looking for! I-Its not my fault okay?!*", function(erro, wMessage) { bot.deleteMessage(wMessage, {"wait": 8000}); });
  178. });
  179. bot.stopTyping(msg.channel);
  180. }
  181. } else correctUsage("anime", this.usage, msg, bot);
  182. }
  183. },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement