Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- registerPlugin({
- name: 'rybufc',
- version: '0.9',
- description: 'point system',
- author: 'rybufc',
- vars: {
- api_key: {
- title: 'API KEY (https://console.developers.google.com/project)',
- type: 'string'
- },
- yt_dl_action: {
- title: 'Action with YoutubeDL',
- type: 'select',
- options: [
- 'Nothing',
- 'Donwload',
- 'Play'
- ]
- },
- yt_dl_playback: {
- title: 'Playback action',
- type: 'select',
- options: [
- 'Queue',
- 'Force play'
- ]
- },
- command_permissionsServergroups: {
- title: 'List of server groups that the bot should accept command (one per line)',
- type: 'multiline',
- placeholder: 'Leave it blank to accept everybody'
- },
- command_trigger: {
- title: 'Command trigger',
- type: 'string',
- placeholder: 'youtube'
- },
- text_format: {
- title: 'Message Format (supports bbcode) <{title}, {description}, {yt_link}, {upload_by}>',
- type: 'multiline',
- placeholder: '[B]You[/B][COLOR=#ff0000]Tube[/COLOR] - Title: {title} - Description: {description} - Link: [url={yt_link}]{yt_link}[/url] - By: {upload_by}'
- },
- catch_url: {
- title: 'Catch YouTube Links',
- type: 'select',
- options: [
- 'Yes',
- 'No'
- ]
- }
- }
- }, function (sinusbot, config) {
- setInterval(Main, 30000);
- function Main()
- {
- var oldState = parseInt(sinusbot.getVarGlobal("previousState"));
- var currentDate = new Date();
- var state = Math.floor(currentDate.getMinutes());
- if(state != oldState)
- {
- sinusbot.setVarGlobal("previousState",state)
- sinusbot.log("Entered OldState")
- //чтение списка пользователей
- var users = getUsers();
- //получение списка пользователей из комнаты и повышение их очков
- var channelInfo = sinusbot.getChannel("10197");
- //sinusbot.log(channelInfo.clients[0].uid);
- for(var i = 0; i < channelInfo.clients.length; i++)
- {
- //sinusbot.log(parseInt(channelInfo.clients[i].uid));
- var isFound = false;
- for(var j = 0; j < users.length; j++)
- {
- sinusbot.log(users[j].uid + ' ' + channelInfo.clients[i].uid)
- sinusbot.log(users[j].uid == channelInfo.clients[i].uid)
- if (users[j].uid == channelInfo.clients[i].uid)
- {
- isFound = true;
- sinusbot.log(users[j].uid)
- users[j].points += 1;
- break;
- }
- }
- if (isFound) continue;
- users[users.length] = new User(channelInfo.clients[i].uid, 30);
- }
- var toSave = "";
- for(var i = 0; i < users.length; i++)
- {
- if(i == 0)
- toSave = users[0].uid + ':' + users[0].points;
- else
- toSave += ',' + users[i].uid + ':' + users[i].points;
- }
- sinusbot.log(toSave);
- sinusbot.setVarGlobal("usersList", toSave);
- }
- }
- function User(uid, points)
- {
- this.uid = uid;
- this.points = points;
- }
- function getUsers()
- {
- var text = String(sinusbot.getVarGlobal("usersList"));
- sinusbot.log(text);
- text = text.split(',');
- //sinusbot.log(text + typeof(text));
- var users = [];
- for (var i = 0; i < text.length; i++)
- {
- var tokens = text[i].split(':');
- users[i] = new User(tokens[0], parseInt(tokens[1]));
- //sinusbot.log(users[i].uid + ' ' + users[i].points);
- }
- return users;
- }
- function getUserPoints(uid)
- {
- var users = getUsers();
- for (var i = 0; i < users.length; i++)
- {
- if(users[i].uid == uid)
- return users[i].points;
- }
- return -1;
- }
- function rewriteUser(uid, points)
- {
- var users = getUsers();
- var toSave = "";
- for(var i = 0; i < users.length; i++)
- {
- if(users[i].uid == uid)
- continue;
- if(i == 0)
- toSave = users[0].uid + ':' + users[0].points;
- else
- toSave += ',' + users[i].uid + ':' + users[i].points;
- }
- toSave += ',' + uid + ':' + points;
- sinusbot.setVarGlobal("usersList", toSave);
- }
- var api_url = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q={query_search}&key={api_key}";
- if (!String.prototype.format) {
- String.prototype.format = function() {
- var str = this.toString();
- if (!arguments.length) {
- return str;
- }
- var args = typeof arguments[0],
- args = (("string" == args || "number" == args) ? arguments : arguments[0]);
- for (arg in args) {
- str = str.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]);
- }
- return str;
- }
- }
- function send_msg(ev, msg) {
- switch (ev.mode) {
- case 1:
- sinusbot.chatPrivate(ev.clientId, msg);
- break;
- case 2:
- sinusbot.chatChannel(msg);
- break;
- default:
- sinusbot.chatServer(msg);
- break;
- }
- }
- function search_yt(str, ev) {
- sinusbot.http({
- method: 'GET',
- url: api_url.format({
- query_search: escape(str),
- api_key: config.api_key
- }),
- headers: {
- 'Content-Type': 'application/json; charset=UTF-8'
- }
- }, function(err, res) {
- if (err) {
- send_msg(ev, "API Request error");
- sinusbot.log(err);
- } else {
- if (res.statusCode == 200) {
- var q = JSON.parse(res.data),
- sq;
- if ("items" in q && q.items.length > 0 && "snippet" in q.items[0] && q.items[0].id.kind == "youtube#video") {
- sq = q.items[0].snippet;
- var default_format = "[B]You[/B][COLOR=#ff0000]Tube[/COLOR] - Title: {title} - Description: {description} - Link: [url={yt_link}]{yt_link}[/url] - By: {upload_by}",
- str_msg, str_vars;
- str_msg = (typeof config.text_format == 'undefined' || config.text_format.length == 0 ? default_format : config.text_format);
- str_vars = {
- title: sq.title,
- description: sq.description,
- yt_link: "http://www.youtube.com/watch?v={0}".format(q.items[0].id.videoId),
- upload_by: sq.channelTitle
- };
- send_msg(ev, str_msg.format(str_vars));
- if (ev.mode == 2) {
- var ytdl_action = parseInt(config.yt_dl_action),
- url = "{yt_link}".format(str_vars),
- queue = (parseInt(config.yt_dl_playback) === 0 || typeof config.yt_dl_playback == 'undefined' ? true : false);
- switch (ytdl_action) {
- case 1: // Download
- sinusbot.log("Donwload: " + url);
- sinusbot.ytdl(url, (queue ? false : true));
- if (queue) {
- sinusbot.log("Append to queue: " + url);
- sinusbot.qyt(url);
- }
- break;
- case 2: // Play
- if (queue) {
- sinusbot.log("Append to queue: " + url);
- sinusbot.qyt(url);
- } else {
- sinusbot.log("Playing: " + url);
- sinusbot.yt(url);
- }
- break;
- default: // Nothing
- break;
- }
- }
- } else {
- send_msg(ev, "Search failed (Nothing found)");
- }
- } else {
- send_msg(ev, "Search failed (Bad request)");
- sinusbot.log("(Bad request) Status Code: " + res.statusCode);
- }
- }
- });
- }
- sinusbot.on('chat', function(ev) {
- if (ev.msg == "!points")
- {
- var points = getUserPoints(ev.clientUid);
- if(points != -1)
- {
- var text = ev.clientNick + ", на вашем балансе " + points + " очков!"
- send_msg(ev, text);
- }
- else
- {
- var text = ev.clientNick + ", на вашем балансе нет очков. Оставайтесь на канале нашего бота, чтобы их заработать!";
- send_msg(ev, text);
- }
- }
- if (ev.clientNick === sinusbot.getNick()) return;
- var is_valid = -1,
- validSGgroups = (typeof config.command_permissionsServergroups == 'undefined' || config.command_permissionsServergroups.length == 0 ? [] : config.command_permissionsServergroups.split('\n'));
- if (validSGgroups.length > 0) {
- ev.clientServerGroups.forEach(function(group) {
- if (validSGgroups.indexOf(group.n) > -1) {
- is_valid++;
- }
- });
- if (is_valid != -1) return;
- }
- var cmd, text, re = /^!(\w+)\s*(.+)/;
- var youtube_rgx = /(?:http|https)\:\/\/www\.(?:youtube\.com|youtu\.be)\/watch\?v\=([\w\-]+)/,
- yt_videoId;
- var cmd_trigger = (typeof config.command_trigger == 'undefined' || config.command_trigger.length == 0 ? 'youtube' : config.command_trigger);
- if ((text = re.exec(ev.msg)) !== null)
- {
- cmd = text[1].toLowerCase();
- text = text[2];
- if (cmd === cmd_trigger)
- {
- if (text.length > 0)
- {
- var points = getUserPoints(ev.clientUid)
- if (points >= 30)
- {
- points -= 30;
- rewriteUser(ev.clientUid, points);
- search_yt(text, ev);
- }
- else
- send_msg(ev, "Извините, но на вашем балансе недостаточно средств для заказа трека!")
- }
- }
- }
- else
- {
- if (parseInt(config.catch_url) !== 1 && (yt_videoId = youtube_rgx.exec(ev.msg)) !== null)
- {
- var points = getUserPoints(ev.clientUid)
- if (points >= 30)
- {
- points -= 30;
- rewriteUser(ev.clientUid, points);
- search_yt(yt_videoId, ev);
- }
- else
- send_msg(ev, "Извините, но на вашем балансе недостаточно средств для заказа трека!")
- }
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement