Advertisement
DryRoastedLemon

Caster Command Module for KoalaBot

Feb 10th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Caster Command - By DryRoastedLemon
  3.   Used to call out a streamer and show what game he/she last played.
  4.   Can be configured via the panel. You can change the following:
  5.     - Whether mod status is needed or not
  6.     - The timeout duration
  7.     - The message that's sent
  8.  */
  9.  
  10.  var modCaster = (function() {
  11.      
  12.     // Saving the settings
  13.     var _saveSettings = function() {
  14.         apiWriteFile( _settingsFileName, JSON.stringify(_casterSettings) );
  15.     };
  16.      
  17.      // Private variables
  18.      var _myTab = apiAddTab( "Caster Command" );
  19.      var _casterSettings = {
  20.          requiresMod: true,
  21.          timeOut: 10 * 1000,
  22.          message: `Check out [caster]'s stream over at [link] and drop them a follow! [caster] last played [game]`
  23.      };
  24.      var _settingsFileName = "modCasterSettings.ini";
  25.      var _timedOut = false;
  26.      
  27.      // Load settings
  28.      var _settingsFile = apiOpenFile( _settingsFileName );
  29.      if ( !_settingsFile) { // If the file doesn't exist save settings
  30.          _saveSettings();
  31.      } else {
  32.          _casterSettings = $.parseJSON( _settingsFile ); // If the file exists load the settings
  33.      }
  34.    
  35.     // Add the !caster command
  36.     if (_casterSettings.requiresMod) {
  37.         _addCommand( "caster", "modCaster.casterCmd", "mod");
  38.     } else {
  39.         _addCommand( "caster", "modCaster.casterCmd", "all");
  40.     }
  41.    
  42.      // Module tab
  43.     $(_myTab).html(`
  44.         <div class="row-fluid">
  45.             <div class="col-sm-12">
  46.                 <div class="panel panel-default">
  47.                     <div class="panel-heading">
  48.                         <h2 class="panel-title">Caster Command options</h2>
  49.                     </div>
  50.                     <ul class="list-group">
  51.                         <li class="list-group-item">
  52.                             <strong>Requires moderator status:</strong>
  53.                             <button id="modCasterEnableMod" class="btn btn-xs">Yes</button>
  54.                             <button id="modCasterDisableMod" class="btn btn-xs">No</button>
  55.                         </li>
  56.                         <li class="list-group-item">
  57.                             <strong>Time-out Duration:</strong>
  58.                             <input id="modCasterTimeoutInput" type="text" size="4"> seconds.
  59.                         </li>
  60.                         <li class="list-group-item">
  61.                             <p><strong>Message:</strong>
  62.                             <input id="modCasterMessageInput" type="text" size="80">
  63.                             <p>You can use the following tags in your message:</p>
  64.                             <ul>
  65.                                 <li>[caster] - The caster's username.</li>
  66.                                 <li>[link] - The link to the caster's Twitch channel.</li>
  67.                                 <li>[game] - The caster's last played game.</li>
  68.                             </ul>
  69.                         </li>
  70.                         <li class="list-group-item">
  71.                             <p><strong>Directions</strong></p>
  72.                             <p>Usage: !caster [twitch username]</p>
  73.                         </li>
  74.                     </ul>
  75.                 </div>
  76.             </div>
  77.         </div>`);
  78.    
  79.     // Initial panel settings
  80.     // Mod button colors
  81.     if (_casterSettings.requiresMod) {
  82.         $("#modCasterEnableMod")
  83.             .addClass("btn-info");
  84.         $("#modCasterDisableMod")
  85.             .addClass("btn-danger");
  86.     } else {
  87.         $("#modCasterEnableMod")
  88.             .addClass("btn-danger");
  89.         $("#modCasterDisableMod")
  90.             .addClass("btn-info");
  91.     }
  92.    
  93.     // Time-out duration
  94.     $( "#modCasterTimeoutInput" ).val(_casterSettings.timeOut / 1000);
  95.     $( "#modCasterMessageInput" ).val(_casterSettings.message);
  96.    
  97.     // Message
  98.     $( "#modCasterMessageInput" ).val(_casterSettings.message);
  99.    
  100.     // Settings are automatically saved as you change any settings
  101.     // Mod requirement settings
  102.     $( "#modCasterEnableMod" ).click(function() {
  103.         $( "#modCasterEnableMod" ).removeClass( "btn-danger" ).addClass( "btn-info" );
  104.         $( "#modCasterDisableMod" ).removeClass( "btn-info" ).addClass("btn-danger");
  105.         _casterSettings.requiresMod = true;
  106.         _addCommand( "caster", "modCaster.casterCmd", "mod");
  107.         _saveSettings();
  108.     });
  109.     $( "#modCasterDisableMod" ).click(function() {
  110.         $( "#modCasterEnableMod" ).removeClass( "btn-info" ).addClass( "btn-danger" );
  111.         $( "#modCasterDisableMod" ).removeClass( "btn-danger" ).addClass("btn-info");
  112.         _casterSettings.requiresMod = false;
  113.         _addCommand( "caster", "modCaster.casterCmd", "all");
  114.         _saveSettings();
  115.     });
  116.    
  117.     // Time-out duration
  118.     $( "#modCasterTimeoutInput" ).change(function() {
  119.             var temp = $( "#modCasterTimeoutInput" ).val();
  120.             if (Math.floor(temp) == temp && $.isNumeric(temp) && temp >= 0) {
  121.                 _casterSettings.timeOut = temp * 1000;
  122.                 _saveSettings();
  123.             } else {
  124.                 $( "#modCasterTimeoutInput" ).val(_casterSettings.timeOut);
  125.                 alert("Please enter a positive integer (e.g.: 1, 2, 3).");
  126.             }
  127.     });
  128.    
  129.     // Message
  130.     $( "#modCasterMessageInput" ).change(function() {
  131.             _casterSettings.message = $( "#modCasterMessageInput" ).val();
  132.             _saveSettings();
  133.     });
  134.    
  135.     // Engaging the time-out
  136.     var _timeOut = function(duration) {
  137.         _timedOut = true;
  138.         setTimeout( function() { _timedOut = false; }, duration );
  139.     };
  140.    
  141.     // The !caster Command
  142.     var _casterCmd = function(params, from) {
  143.         // Check for mod requirement and mod status
  144.         //if (_casterSettings.requiresMod && (!mod || from.toLowerCase() != apiGetChannelName())) return;
  145.        
  146.         if (!params[0]) {
  147.             return apiSay( "Usage: !caster [castername]" );
  148.         } else {
  149.             if( _timedOut ) return;
  150.            
  151.             var caster = params[0];
  152.             $.get(
  153.             "https://api.rtainc.co/twitch/game",
  154.             { "channel": caster },
  155.             function( data ){
  156.                 if ( data.includes( "<br />" )) return apiSay( "Couldn't find any results!" ); // I don't know how to properly check for errors, but this works for the time being.
  157.                 else {
  158.                     // Replace the [caster], [link] and [game] tags with actual data.
  159.                     // The code's ugly, but I had to escape the brackets.
  160.                     var message = _casterSettings.message;
  161.                     message = message.replace( /\[caster\]/g, caster );
  162.                     message = message.replace( /\[link\]/g, "http://twitch.tv/" + caster.toLowerCase() );
  163.                     message = message.replace( /\[game\]/g, data );
  164.                    
  165.                     apiSay( message );
  166.                     _timeOut(_casterSettings.timeOut);
  167.                 }
  168.             });
  169.         }
  170.     };
  171.    
  172.     return {
  173.         casterCmd: _casterCmd
  174.     };
  175.    
  176.     // -------------------- Updating the command --------------------
  177.     // Declared in this way so it's created at parse-time.
  178.    
  179.     function _addCommand(cmd, functionName, accesslevel) {
  180.         var description = "Gives a shoutout to other streamers.";
  181.        
  182.         // Remove the command if it exists
  183.         for (var i = 0; i < cmdList.length; i++) {
  184.             if (cmdList[i].cmd == cmd) {
  185.                 cmdList.splice(i, 1);
  186.             }
  187.         }
  188.        
  189.         // Add the command with the correct accesslevel
  190.         apiAddCmd(cmd, functionName, accesslevel, description);
  191.     }
  192.    
  193.  })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement