Advertisement
rybufc

rank system for musicBot

Oct 12th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. registerPlugin({
  2.  name: 'rybufc',
  3.  version: '0.9',
  4.  description: 'point system',
  5.  author: 'rybufc',
  6.  vars: {
  7.  api_key: {
  8.  title: 'API KEY (https://console.developers.google.com/project)',
  9.  type: 'string'
  10.  },
  11.  yt_dl_action: {
  12.  title: 'Action with YoutubeDL',
  13.  type: 'select',
  14.  options: [
  15.  'Nothing',
  16.  'Donwload',
  17.  'Play'
  18.  ]
  19.  },
  20.  yt_dl_playback: {
  21.  title: 'Playback action',
  22.  type: 'select',
  23.  options: [
  24.  'Queue',
  25.  'Force play'
  26.  ]
  27.  },
  28.  command_permissionsServergroups: {
  29.  title: 'List of server groups that the bot should accept command (one per line)',
  30.  type: 'multiline',
  31.  placeholder: 'Leave it blank to accept everybody'
  32.  },
  33.  command_trigger: {
  34.  title: 'Command trigger',
  35.  type: 'string',
  36.  placeholder: 'youtube'
  37.  },
  38.  text_format: {
  39.  title: 'Message Format (supports bbcode) <{title}, {description}, {yt_link}, {upload_by}>',
  40.             type: 'multiline',
  41.             placeholder: '[B]You[/B][COLOR=#ff0000]Tube[/COLOR] - Title: {title} - Description: {description} - Link: [url={yt_link}]{yt_link}[/url] - By: {upload_by}'
  42.         },
  43.         catch_url: {
  44.             title: 'Catch YouTube Links',
  45.             type: 'select',
  46.             options: [
  47.                 'Yes',
  48.                 'No'
  49.             ]
  50.         }
  51.     }
  52.  }, function (sinusbot, config) {
  53.     setInterval(Main, 30000);
  54.  
  55.     function Main()
  56.     {
  57.         var oldState = parseInt(sinusbot.getVarGlobal("previousState"));
  58.         var currentDate = new Date();
  59.         var state = Math.floor(currentDate.getMinutes());
  60.  
  61.         if(state != oldState)
  62.         {
  63.             sinusbot.setVarGlobal("previousState",state)
  64.             sinusbot.log("Entered OldState")
  65.             //чтение списка пользователей
  66.            
  67.             var users = getUsers();
  68.      
  69.             //получение списка пользователей из комнаты и повышение их очков
  70.             var channelInfo = sinusbot.getChannel("10197");
  71.             //sinusbot.log(channelInfo.clients[0].uid);
  72.  
  73.             for(var i = 0; i < channelInfo.clients.length; i++)
  74.             {
  75.                 //sinusbot.log(parseInt(channelInfo.clients[i].uid));
  76.                
  77.                 var isFound = false;
  78.      
  79.                 for(var j = 0; j < users.length; j++)
  80.                 {
  81.                     sinusbot.log(users[j].uid + ' ' + channelInfo.clients[i].uid)
  82.                     sinusbot.log(users[j].uid == channelInfo.clients[i].uid)
  83.                     if (users[j].uid == channelInfo.clients[i].uid)
  84.                     {
  85.                         isFound = true;
  86.                         sinusbot.log(users[j].uid)
  87.                         users[j].points += 1;
  88.                         break;
  89.                     }
  90.                 }
  91.  
  92.                
  93.                 if (isFound) continue;
  94.                 users[users.length] = new User(channelInfo.clients[i].uid, 30);
  95.             }
  96.  
  97.             var toSave = "";
  98.             for(var i = 0; i < users.length; i++)
  99.             {
  100.                 if(i == 0)
  101.                     toSave = users[0].uid + ':' + users[0].points;
  102.                 else
  103.                     toSave += ',' + users[i].uid + ':' + users[i].points;
  104.             }
  105.             sinusbot.log(toSave);
  106.             sinusbot.setVarGlobal("usersList", toSave);
  107.         }
  108.      
  109.        
  110.  
  111.     }
  112.  
  113.     function User(uid, points)
  114.     {
  115.         this.uid = uid;
  116.         this.points = points;
  117.     }
  118.  
  119.     function getUsers()
  120.     {
  121.         var text = String(sinusbot.getVarGlobal("usersList"));
  122.         sinusbot.log(text);
  123.         text = text.split(',');
  124.         //sinusbot.log(text + typeof(text));
  125.  
  126.         var users = [];
  127.         for (var i = 0; i < text.length; i++)
  128.         {
  129.             var tokens = text[i].split(':');
  130.             users[i] = new User(tokens[0], parseInt(tokens[1]));
  131.             //sinusbot.log(users[i].uid + ' ' + users[i].points);
  132.         }
  133.         return users;
  134.     }
  135.  
  136.     function getUserPoints(uid)
  137.     {
  138.         var users = getUsers();
  139.         for (var i = 0; i < users.length; i++)
  140.         {
  141.             if(users[i].uid == uid)
  142.                 return users[i].points;
  143.         }
  144.         return -1;
  145.     }
  146.  
  147.     function rewriteUser(uid, points)
  148.     {
  149.         var users = getUsers();
  150.         var toSave = "";
  151.         for(var i = 0; i < users.length; i++)
  152.         {
  153.             if(users[i].uid == uid)
  154.                 continue;
  155.             if(i == 0)
  156.                 toSave = users[0].uid + ':' + users[0].points;
  157.             else
  158.                 toSave += ',' + users[i].uid + ':' + users[i].points;
  159.         }
  160.         toSave += ',' + uid + ':' + points;
  161.         sinusbot.setVarGlobal("usersList", toSave);
  162.     }
  163.  
  164.     var api_url = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q={query_search}&key={api_key}";
  165.  
  166.     if (!String.prototype.format) {
  167.         String.prototype.format = function() {
  168.             var str = this.toString();
  169.             if (!arguments.length) {
  170.                 return str;
  171.             }
  172.             var args = typeof arguments[0],
  173.                 args = (("string" == args || "number" == args) ? arguments : arguments[0]);
  174.             for (arg in args) {
  175.                 str = str.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]);
  176.             }
  177.             return str;
  178.         }
  179.     }
  180.  
  181.     function send_msg(ev, msg) {
  182.         switch (ev.mode) {
  183.             case 1:
  184.                 sinusbot.chatPrivate(ev.clientId, msg);
  185.                 break;
  186.             case 2:
  187.                 sinusbot.chatChannel(msg);
  188.                 break;
  189.             default:
  190.                 sinusbot.chatServer(msg);
  191.                 break;
  192.         }
  193.     }
  194.  
  195.     function search_yt(str, ev) {
  196.         sinusbot.http({
  197.             method: 'GET',
  198.             url: api_url.format({
  199.                 query_search: escape(str),
  200.                 api_key: config.api_key
  201.             }),
  202.             headers: {
  203.                 'Content-Type': 'application/json; charset=UTF-8'
  204.             }
  205.         }, function(err, res) {
  206.             if (err) {
  207.                 send_msg(ev, "API Request error");
  208.                 sinusbot.log(err);
  209.             } else {
  210.                 if (res.statusCode == 200) {
  211.                     var q = JSON.parse(res.data),
  212.                         sq;
  213.                     if ("items" in q && q.items.length > 0 && "snippet" in q.items[0] && q.items[0].id.kind == "youtube#video") {
  214.                         sq = q.items[0].snippet;
  215.                         var default_format = "[B]You[/B][COLOR=#ff0000]Tube[/COLOR] - Title: {title} - Description: {description} - Link: [url={yt_link}]{yt_link}[/url] - By: {upload_by}",
  216.                             str_msg, str_vars;
  217.                         str_msg = (typeof config.text_format == 'undefined' || config.text_format.length == 0 ? default_format : config.text_format);
  218.                         str_vars = {
  219.                             title: sq.title,
  220.                             description: sq.description,
  221.                             yt_link: "http://www.youtube.com/watch?v={0}".format(q.items[0].id.videoId),
  222.                             upload_by: sq.channelTitle
  223.                         };
  224.                         send_msg(ev, str_msg.format(str_vars));
  225.                         if (ev.mode == 2) {
  226.                             var ytdl_action = parseInt(config.yt_dl_action),
  227.                                 url = "{yt_link}".format(str_vars),
  228.                                 queue = (parseInt(config.yt_dl_playback) === 0 || typeof config.yt_dl_playback == 'undefined' ? true : false);
  229.                             switch (ytdl_action) {
  230.                                 case 1: // Download
  231.                                     sinusbot.log("Donwload: " + url);
  232.                                     sinusbot.ytdl(url, (queue ? false : true));
  233.                                     if (queue) {
  234.                                         sinusbot.log("Append to queue: " + url);
  235.                                         sinusbot.qyt(url);
  236.                                     }
  237.                                     break;
  238.                                 case 2: // Play
  239.                                     if (queue) {
  240.                                         sinusbot.log("Append to queue: " + url);
  241.                                         sinusbot.qyt(url);
  242.                                     } else {
  243.                                         sinusbot.log("Playing: " + url);
  244.                                         sinusbot.yt(url);
  245.                                     }
  246.                                     break;
  247.                                 default: // Nothing
  248.                                     break;
  249.                             }
  250.                         }
  251.                     } else {
  252.                         send_msg(ev, "Search failed (Nothing found)");
  253.                     }
  254.                 } else {
  255.                     send_msg(ev, "Search failed (Bad request)");
  256.                     sinusbot.log("(Bad request) Status Code: " + res.statusCode);
  257.                 }
  258.             }
  259.         });
  260.     }
  261.  
  262.     sinusbot.on('chat', function(ev) {
  263.         if (ev.msg == "!points")
  264.         {
  265.             var points = getUserPoints(ev.clientUid);
  266.             if(points != -1)
  267.             {  
  268.                 var text = ev.clientNick + ", на вашем балансе " + points + " очков!"
  269.                 send_msg(ev, text);
  270.             }
  271.             else
  272.             {
  273.                 var text = ev.clientNick + ", на вашем балансе нет очков. Оставайтесь на канале нашего бота, чтобы их заработать!";
  274.                 send_msg(ev, text);
  275.             }
  276.         }
  277.         if (ev.clientNick === sinusbot.getNick()) return;
  278.         var is_valid = -1,
  279.             validSGgroups = (typeof config.command_permissionsServergroups == 'undefined' || config.command_permissionsServergroups.length == 0 ? [] : config.command_permissionsServergroups.split('\n'));
  280.         if (validSGgroups.length > 0) {
  281.             ev.clientServerGroups.forEach(function(group) {
  282.                 if (validSGgroups.indexOf(group.n) > -1) {
  283.                     is_valid++;
  284.                 }
  285.             });
  286.             if (is_valid != -1) return;
  287.         }
  288.  
  289.         var cmd, text, re = /^!(\w+)\s*(.+)/;
  290.         var youtube_rgx = /(?:http|https)\:\/\/www\.(?:youtube\.com|youtu\.be)\/watch\?v\=([\w\-]+)/,
  291.             yt_videoId;
  292.         var cmd_trigger = (typeof config.command_trigger == 'undefined' || config.command_trigger.length == 0 ? 'youtube' : config.command_trigger);
  293.         if ((text = re.exec(ev.msg)) !== null)
  294.         {
  295.             cmd = text[1].toLowerCase();
  296.             text = text[2];
  297.             if (cmd === cmd_trigger)
  298.             {
  299.                 if (text.length > 0)
  300.                 {
  301.                     var points = getUserPoints(ev.clientUid)
  302.                     if (points >= 30)
  303.                     {
  304.                         points -= 30;
  305.                         rewriteUser(ev.clientUid, points);
  306.                         search_yt(text, ev);
  307.                     }
  308.                     else
  309.                         send_msg(ev, "Извините, но на вашем балансе недостаточно средств для заказа трека!")
  310.                 }
  311.             }
  312.         }
  313.         else
  314.         {
  315.             if (parseInt(config.catch_url) !== 1 && (yt_videoId = youtube_rgx.exec(ev.msg)) !== null)
  316.             {
  317.                 var points = getUserPoints(ev.clientUid)
  318.                 if (points >= 30)
  319.                 {
  320.                     points -= 30;
  321.                     rewriteUser(ev.clientUid, points);
  322.                     search_yt(yt_videoId, ev);
  323.                 }
  324.                 else
  325.                     send_msg(ev, "Извините, но на вашем балансе недостаточно средств для заказа трека!")
  326.             }
  327.         }
  328.     });
  329. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement