Advertisement
Guest User

lastfmCommand.js

a guest
Dec 7th, 2019
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     (function() {
  2.         const db_title = "lastfm";
  3.         const default_last_fm_api_key = "293111fdaf9f475bf1f5463a063ffa66";
  4.         const api_url_basics = "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&format=json&limit=1";
  5.         // variable
  6.         var last_fm_api_key = default_last_fm_api_key;
  7.         var last_fm_username = "";
  8.         var artist = "";
  9.         var name = "";
  10.         var sender;
  11.        
  12.         function initVars() {
  13.             last_fm_username = $.inidb.get(db_title, 'user_name');
  14.             last_fm_api_key = $.inidb.get(db_title, "api_key");
  15.         }
  16.        
  17.         function getTrackFromLastFm(index, repeater) {
  18.             try {
  19.                 var jsonObj = JSON.parse($.customAPI.get(api_url_basics + "&page=" + index + "&user=" + last_fm_username + "&api_key=" + last_fm_api_key).content);
  20.                 var current_track = jsonObj.recenttracks.track[0];
  21.                 if (index > 1) {
  22.                     track = jsonObj.recenttracks.track[1];
  23.                     $.say($.whisperPrefix(sender) + " 🎶 " + track["name"] + " - " +  track["artist"]["#text"] + ", " + track["album"]["#text"]);
  24.                 } else if (index == 1) {
  25.                     var new_track = ((artist != current_track["artist"]["#text"]) || (name != current_track["name"]));
  26.                     artist = current_track["artist"]["#text"];
  27.                     name = current_track["name"];
  28.                     if (!repeater || new_track) {
  29.                         $.say((repeater ? "" : $.whisperPrefix(sender)) + " 🎶 " + current_track["name"] + " - " +  current_track["artist"]["#text"] + ", " + current_track["album"]["#text"]);
  30.                     }
  31.                 }
  32.                 return true;
  33.             } catch (ex) {
  34.                 $.consoleLn(ex);
  35.                 $.consoleLn(ex.stack);             
  36.             }
  37.             return false;
  38.         }
  39.  
  40.         $.bind('command', function(event) {
  41.  
  42.             sender = event.getSender(); // Grabs the person who used the command
  43.             var command = event.getCommand(), // Grabs the command being used
  44.                 args = event.getArgs(), // arguments used for your commands like maybe
  45.                 action = args[0], // the array of args received
  46.                 subAction = args[1];
  47.  
  48.             if (command.equalsIgnoreCase('song')) {
  49.                 initVars();
  50.                 if (action === undefined) {
  51.                     getTrackFromLastFm(1, false);
  52.                 } else if (action.equalsIgnoreCase("previous")) {
  53.                     getTrackFromLastFm(2, false);
  54.                 } else if (action.equalsIgnoreCase("index")) {
  55.                     getTrackFromLastFm(parseInt(subAction));
  56.                 } else if (action.equalsIgnoreCase("help")) {
  57.                     $.say($.whisperPrefix(sender) + " !song - get the current played song \\ !song on - enable \\ !song off - disable \\ !song previous - last played song \\ !song index * - last played song numerical 1 (newest) to max-oldest \\  !song key * - set the lastfm api key \\ !song user * - set the lastfm user name", $.whisperPrefix(sender));
  58.                 } else if (action.equalsIgnoreCase("user")) {
  59.                     if (!$.isBot(sender)) {
  60.                         return;
  61.                     }
  62.                     last_fm_username = subAction;
  63.                     if (getTrackFromLastFm(1, false)) {
  64.                         $.inidb.set(db_title, 'user_name', subAction);
  65.                         $.inidb.SaveAll(true);
  66.                         $.say($.whisperPrefix(sender) + " user name set to " + last_fm_username);
  67.                     } else {
  68.                         initVars();
  69.                         $.say($.whisperPrefix(sender) + " user name ignored keeping old username " + last_fm_username);
  70.                     }
  71.                 } else if (action.equalsIgnoreCase('key')) {
  72.                     if (!$.isBot(sender)) {
  73.                         return;
  74.                     }
  75.                     last_fm_api_key = subAction;
  76.                     if (getTrackFromLastFm(1, false)) {
  77.                         $.inidb.set(db_title, "api_key", subAction);
  78.                         $.inidb.SaveAll(true);
  79.                         $.say($.whisperPrefix(sender) + " api key set to" + last_fm_api_key);
  80.                     } else {
  81.                         initVars();
  82.                         $.say($.whisperPrefix(sender) + " api key ignored keeping old api key ");
  83.                     }
  84.                 } else if (action.equalsIgnoreCase('on')) {
  85.                     if (!$.isBot(sender)) {
  86.                         return;
  87.                     }
  88.                     $.inidb.set(db_title, 'enabled', true);
  89.                 } else if (action.equalsIgnoreCase('off')) {
  90.                     if (!$.isBot(sender)) {
  91.                         return;
  92.                     }
  93.                     $.inidb.set(db_title, 'enabled', false);
  94.                 }
  95.                 $.consoleLn("command completed");
  96.             }
  97.         });
  98.  
  99.         $.bind('initReady', function() {
  100.  
  101.             if (!$.inidb.exists(db_title, 'initial_push')) {
  102.                 setTimeout(function() {
  103.                     $.inidb.set(db_title, 'initial_push', true);
  104.                     $.inidb.set(db_title, 'enabled', true);
  105.                     $.inidb.set(db_title, 'user_name', 'rj');
  106.                     $.inidb.set(db_title, 'api_key', default_last_fm_api_key);
  107.                     $.inidb.SaveAll(true);
  108.                 }, 5 * 1000);
  109.             };
  110.             if ($.bot.isModuleEnabled('./commands/lastfmCommand.js')) {
  111.                 initVars();
  112.                 $.registerChatCommand('./commands/lastfmCommand.js', 'song');
  113.                 setInterval(function() {
  114.                     if ($.inidb.get(db_title, 'enabled') == 'true') {
  115.                         if (!getTrackFromLastFm(1, true)) {
  116.                             $.consoleLn("failed to get last.fm track info");
  117.                         }
  118.                     }
  119.                 }, 2.5 * 1000);
  120.             }
  121.         });
  122.  
  123.     })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement