Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "anime": {
- desc: "Gets details on an anime from MAL. Do ``" + config.command_prefix + "anime --help`` for more info",
- usage: "<anime name> [--help] [--recent | --popular | --airing | --unreleased]",
- deleteCommand: true,
- cooldown: 6,
- process: function(bot, msg, suffix) {
- if (suffix) {
- //add function for recent and popular, if both aren't set then do default recent
- var argv = yargs.parse(suffix);
- var bRecent = false;
- var bPopular = false;
- var bAiring = false;
- var bShowAllReleasedUnreleased = true;
- var strSearch = argv._.join(' ');
- if(argv.h || argv.help)
- {
- var helpMsg = [];
- helpMsg.push(":information_source: **Anime Search Help\n**");
- helpMsg.push("**Usage**: ``" + config.command_prefix + "anime <search_term> [ --recent | --popular | --airing | --unreleased]``");
- helpMsg.push("\n**Optional Flags:**\n Tags to include to search/filter.");
- helpMsg.push(":black_small_square:``--help / -h`` | Display this help");
- 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");
- helpMsg.push(":black_small_square:``--popular / -p`` | Sort by score and return first entry");
- helpMsg.push(":black_small_square:``--airing / --aired / -a`` | Displays only animes currently airing/already aired");
- helpMsg.push(":black_small_square:``--unreleased / -u`` | Displays only animes that aren't aired yet\n");
- 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.")
- bot.sendMessage(msg.author, helpMsg);
- return;
- }
- else
- {
- if(argv.r || argv.recent) bRecent = !bRecent; //recent, sortby start_date (airing date start)
- if(argv.p || argv.popular) bPopular = !bPopular; //popular, sortby score
- if((argv.a || argv.airing) && (argv.u || argv.unreleased)){
- bShowAllReleasedUnreleased = true;
- }
- else{
- if(argv.a || argv.airing || argv.aired){
- bShowAllReleasedUnreleased = false;
- bAiring = true; //airing/released
- }
- if(argv.u || argv.unreleased){
- bShowAllReleasedUnreleased = false;
- bAiring = false; //unaired/unreleased
- }
- }
- 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; }
- bot.startTyping(msg.channel);
- var tags = ent.encodeHTML(strSearch);
- var rUrl = "http://myanimelist.net/api/anime/search.xml?q=" + tags;
- request(rUrl, {"auth": {"user": MAL_USER, "pass": MAL_PASS, "sendImmediately": false}}, function(error, response, body) {
- if (error) console.log(error);
- else if (!error && response.statusCode == 200) {
- //FALZ -- MY MORE RELEVANT SEARCH ALGO, BECAUSE WHO WATCHES 1999 ANIMES (jk cowboy bebop is awesome)
- async.waterfall([
- function getSearchAPIResult(done)
- {
- xml2js.parseString(body, function(err, result){
- done(null, result.anime.entry);
- });
- },
- function filterResult(result, done)
- {
- if(!bShowAllReleasedUnreleased)
- {
- var res = [];
- if(bAiring){
- for(var i = 0; i < result.length; i++)
- {
- if(result[i].status != "Not yet aired")
- {
- res.push(result[i]);
- }
- }
- }else{
- for(var i = 0; i < result.length; i++)
- {
- if(result[i].status == "Not yet aired")
- {
- res.push(result[i]);
- }
- }
- }
- if(res.length <= 0) done(new Error("No results!"));
- else done(null, res);
- }
- else done(null, result);
- },
- function sortResult(result, done)
- {
- if(bRecent)
- {
- result.sort(firstBy(function(v){
- var vnow = moment();
- var vdate = moment(v.start_date);
- return vnow.diff(vdate, 'days');
- }));
- }
- else if(bPopular)
- {
- result.sort(firstBy(function(v){
- var vscore = parseFloat(v.score);
- return vscore;
- }, -1));
- }
- else if(bRecent && bPopular)
- {
- result.sort(
- firstBy(function(v){
- var vnow = moment();
- var vdate = moment(v.start_date);
- //console.log("RECENTPOP: v->"+vdate);
- return vnow.diff(vdate, 'days');
- }).thenBy(function(v1, v2){
- var vscore = parseFloat(v.score);
- console.log("RECENTPOP SCORE: v->"+vscore);
- return vscore;
- }, -1));
- }
- else{
- //do nothing :D
- }
- done(null, result);
- },
- function sendResult(result, done)
- {
- var title = result[0].title;
- var english = result[0].english;
- var ep = result[0].episodes;
- var score = result[0].score;
- var type = result[0].type;
- var status = result[0].status;
- var synopsis = result[0].synopsis.toString();
- var id = result[0].id;
- var start_date = result[0].start_date;
- if(start_date === "0000-00-00") start_date = "N/A";
- var end_date = result[0].end_date;
- if(end_date === "0000-00-00") end_date = "N/A";
- synopsis = synopsis.replace(/<br \/>/g, " "); synopsis = synopsis.replace(/\[(.{1,10})\]/g, "");
- synopsis = synopsis.replace(/\r?\n|\r/g, " "); synopsis = synopsis.replace(/\[(i|\/i)\]/g, "*"); synopsis = synopsis.replace(/\[(b|\/b)\]/g, "**");
- synopsis = ent.decodeHTML(synopsis);
- if (!msg.channel.isPrivate) {
- if (synopsis.length > 400) { synopsis = synopsis.substring(0, 400); synopsis += "..."; }
- }
- var toSend = ":tv: "
- + "**"
- + title
- + " / "
- + english
- + "**\n**Type:** "
- + type
- + " **| Episodes:** "
- + ep
- + " **| Status:** "
- + status
- + " **| Start:** "
- + start_date
- + " **| End:** "
- + end_date
- + " **| Score:** "
- + score
- + "\n"
- + synopsis
- + "\n**http://www.myanimelist.net/anime/"
- + id
- + "**";
- bot.sendMessage(msg, toSend);
- //bot.sendMessage(msg, ":tv: " + "**" + title + " / " + english + "**\n**Type:** " + type + " **| Episodes:** " + ep + " **| Status:** " + status + " **| Score:** " + score + "\n" + synopsis + "\n**http://www.myanimelist.net/anime/" + id + "**");
- done(null);
- }
- ], function(err, result){
- //do nothing :D
- 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?!*");
- });
- } 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}); });
- });
- bot.stopTyping(msg.channel);
- }
- } else correctUsage("anime", this.usage, msg, bot);
- }
- },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement