Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 49.30 KB | None | 0 0
  1. registerPlugin(
  2. {
  3. name: 'UseTS-AdminBOT',
  4. version: '0.4.2',
  5. description: 'Many administration functions in one plugin.',
  6. author: 'sync667',
  7. vars: {
  8. helpChannelsId: { title: 'Support Channels - IDs (comma separated)', type: 'string' },
  9. helpGroupsId: { title: 'Support Members - GroupsID (comma separated)', type: 'string' },
  10. helpIgnoredGroupsId: { title: 'Group ignored on support channels - GroupsID (comma separated)', type: 'string' },
  11. helpCloseNoStaff: {title: 'Should support channels should be closed when no staff online?', type: 'select',
  12. options: ['Yes', 'No']},
  13. helpAwayMode: {title: 'Should staff with away status be excluded from online support?', type: 'select',
  14. options: ['Yes', 'No']},
  15. helpClosePrefix: {title: 'Prefix added to channel name when closed (must be short, because of chars limit)', type: 'string',
  16. placeholder: '(Closed)'},
  17. helpUserNotification: {title: 'Message to user waiting support - s0 for Username, s1 for Staff Online', type: 'string',
  18. placeholder: 'Hi [b]s0[/b]! Staff member will help you soon! [b]Online staff:[/b] s1'},
  19. helpUserNotification2: {title: 'Message to user when no staff online - s0 for Username', type: 'string',
  20. placeholder: 'Hi [b]s0[/b]! Sorry, but all our staff is offline come back later.'},
  21. helpStaffNotification: {title: 'Message to staff - s0 for Username, s1 for Staff Online', type: 'string',
  22. placeholder: 'User [b]s0[/b] is waiting in support zone! [b]Online staff:[/b] s1'},
  23. helpStaffAway: {title: 'Message to user after Staff Away Time passed about staff currently busy', type: 'string',
  24. placeholder: 'Sorry! Our staff is currently busy. They will contact you as soon as possible.'},
  25. helpStaffAwayTime: {title: 'Timer after with user will get notification about staff is busy right now in seconds (0 to disable)', type: 'number',
  26. placeholder: 'Default 60 seconds'},
  27. helpStaffJoined: {title: 'Message to staff when someone of them join to help user - s0 for Username of helper, s1 for Username of user', type: 'string',
  28. placeholder: 'Staff member [b]s0[/b] joined to help [b]s1[/b].'},
  29. staffJoinSupport: {title: 'Send message to whole staff when one of them help start to help waiting user?', type: 'select',
  30. options: ['Yes','No']},
  31. helpStaffNotificationType: {title: 'Support (staff users) notification type', type: 'select',
  32. options: ['Chat','Poke']},
  33. helpUsersNotificationType: {title: 'Support (normal users) notification type', type: 'select',
  34. options: ['Chat','Poke']},
  35. autoTempChannelsId: {title: 'Auto temporary channels (comma separated)', type: 'string' },
  36. autoTempChannelsParentId: {title: 'Auto temporary channels parent', type: 'channel' },
  37. autoTempChannelsRestrictedGroups: {title: 'Groups that are not allowed to get temporary channels - IDs (comma separated)', type: 'string'},
  38. autoTempChannelsName: {title: 'Automatic temporary channel name - s0 is channel number', type: 'string',
  39. placeholder: 'Temporary Channel s0'},
  40. autoTempChannelsMessage: {title: 'Message to user about auto temporary chanel get created', type: 'string',
  41. placeholder: 'I created temporary channel as you wish!'},
  42. autoTempChannelsPassMessage: {title: 'Message to user about auto temporary chanel password - s0 is password', type: 'string',
  43. placeholder: 'Your channel password: [b]s0[/b]'},
  44. autoTempChannelsPass: {title: 'Automatic temporary channels with password?', type: 'select',
  45. options: ['Yes', 'No']},
  46. autoTempChannelsPassLength: {title: 'Automatic temporary channel password length', type: 'number',
  47. placeholder: '4'},
  48. autoTempChannelsRestricted: {title: 'Message when users is not permitted do get temporary channel', type: 'string',
  49. placeholder: 'Sorry! You are not allowed to get temporary channel.'},
  50. autoTempChannelsErrorMessage: {title: 'Message to user about auto temp channel creation error', type: 'string',
  51. placeholder: "I can't created temporary channel for you right now, try later."},
  52. botChecksEvery: {title: 'Every x seconds bot will do checks (optimal is 60)', type: 'number'},
  53. botAIPrivateChat: {title: 'Should bot use his AI to replay for private chat messages to user?', type: 'select',
  54. options: ['Yes', 'No']},
  55. botAIPoke: {title: 'Should bot use his AI to replay for poke messages to user?', type: 'select',
  56. options: ['Yes', 'No']},
  57. onlineUsersChannelId: {title: 'Channel with name update for online users information', type: 'channel'},
  58. onlineUsersChannel: {title: 'Name of auto online channel - s0 is online number', type: 'string',
  59. placeholder: "Users Online: s0"},
  60. maxOnlineUsersChannelId: {title: 'Channel with name update for max online users information', type: 'channel'},
  61. maxOnlineUsersChannel: {title: 'Name of auto max online channel - s0 is for max online number', type: 'string',
  62. placeholder: "Max Users Online: s0"},
  63. maxOnlineUsersChannelDesc: {title: 'Desc of max channel - s0 is for record date', type: 'string',
  64. placeholder: "Last record date: s0"},
  65. staffOnlineChannelId: {title: 'Channel with name update for staff online information', type: 'channel'},
  66. staffOnlineChannelName: {title: 'Name of staff online channel - s0 is online number', type: 'string',
  67. placeholder: "Staff Online: s0"},
  68. staffOnlineChannelSublist: {title: 'Turn on or off staff list by subchannels', type: 'select',
  69. options: ['On', 'Off']},
  70. staffOnlineChannelZero: {title: 'When none admins are online text - can be 0', type: 'string',
  71. placeholder: "none"},
  72. staffOnlineChannelDesc: {title: 'Desc of staff online channel - s0 is nicname, s1 is group', type: 'string',
  73. placeholder: "s0 - s1 is online"},
  74. registerGroupId: {title: 'Registration Group Id', type: 'number'},
  75. registerWelcomeMessage: {title: 'Welcome message for user without register group', type: 'multiline'},
  76. registerMessageType: {title: 'Type of message poke or chat', type: 'select',
  77. options: ['Poke', 'Chat']},
  78. registerMessageOn: {title: 'Turn on or off welcome message poke or chat', type: 'select',
  79. options: ['On', 'Off']},
  80. autoMusicCodecBot: {title: 'Change codec of channel to music when bot joins?', type: 'select',
  81. options: ['On', 'Off']},
  82. autoMusicCodecNew: {title: 'Codec and quality to set on channel with bot joins - syntax: "codecId#quality", example for opus music "5#10"', type: 'string',
  83. placeholder: '5#10'},
  84. clockChannel: {title: 'Channel for clock display', type: 'channel'},
  85. clockFormat: {title: 'Format of clock - s0 day, s1 month, s2 year, s3 hour, s4 minute', type: 'string',
  86. placeholder: "[cspacer*] s3:s4 | s0.s1.s2"},
  87. clockTimezone: {title: 'Select clock timezone offset from UTC (type number like 2 or -2 etc.)', type: 'string',
  88. placeholder: "0"}
  89. }
  90. },
  91. function(sinusbot, config, info) {
  92. sinusbot.log('Loading ' + info.name + ' version: ' + info.version + ' - powered by ' + info.author);
  93. sinusbot.log('Do not forget to check all settings!');
  94.  
  95. sinusbot.setVar('staff', []);
  96. sinusbot.setVar('onChannelCreate', []);
  97. sinusbot.setVar('onlineClients', 1);
  98. sinusbot.setVar('usersOnHelp', []);
  99. sinusbot.setVar('channelCodec', []);
  100. sinusbot.setVar('botMove' + self, {});
  101. sinusbot.setVar("helpClosed", 0);
  102.  
  103. var self = sinusbot.getBotId();
  104. var staff = sinusbot.getVar('staff');
  105.  
  106. //Defaults values handling
  107. if (isUndefined(config.helpChannelsId))
  108. config.helpChannelsId = -1;
  109. if (isUndefined(config.helpGroupsId))
  110. config.helpGroupsId = -1;
  111. if (isUndefined(config.helpIgnoredGroupsId))
  112. config.helpIgnoredGroupsId = -1;
  113. if (isUndefined(config.helpStaffAwayTime) || config.helpStaffAwayTime < 0)
  114. config.helpStaffAwayTime = 60;
  115. if (isUndefined(config.autoTempChannelsId))
  116. config.autoTempChannelsId = -1;
  117. if (isUndefined(config.autoTempChannelsParentId) || parseInt(config.autoTempChannelsParentId) < 0)
  118. config.autoTempChannelsParentId = 0;
  119. if (isUndefined(config.autoTempChannelsRestrictedGroups))
  120. config.autoTempChannelsRestrictedGroups = -1;
  121. if (isUndefined(config.autoTempChannelsPass))
  122. config.autoTempChannelsPass = 1;
  123. if (isUndefined(config.autoTempChannelsPassLength) || config.autoTempChannelsPassLength < 0)
  124. config.autoTempChannelsPassLength = 4;
  125. if (isUndefined(config.botChecksEvery) || config.botChecksEvery < 0)
  126. config.botChecksEvery = 60;
  127. if (isUndefined(config.helpCloseNoStaff))
  128. config.helpCloseNoStaff = 1;
  129. if (isUndefined(config.helpAwayMode))
  130. config.helpAwayMode = 1;
  131. if (isUndefined(config.staffJoinSupport))
  132. config.staffJoinSupport = 1;
  133. if (isUndefined(config.staffOnlineChannelSublist))
  134. config.staffOnlineChannelSublist = 1;
  135. if (isUndefined(config.helpStaffNotificationType))
  136. config.helpStaffNotificationType = 0;
  137. if (isUndefined(config.helpUsersNotificationType))
  138. config.helpUsersNotificationType = 0;
  139. if (isUndefined(config.botAIPrivateChat))
  140. config.botAIPrivateChat = 1;
  141. if (isUndefined(config.botAIPoke))
  142. config.botAIPoke = 1;
  143. if (isUndefined(config.registerMessageType))
  144. config.registerMessageType = 1;
  145. if (isUndefined(config.registerMessageOn))
  146. config.registerMessageOn = 1;
  147. if (isUndefined(config.autoMusicCodecBot))
  148. config.autoMusicCodecBot = 1;
  149. if (isUndefined(config.clockChannel))
  150. config.clockChannel = -1;
  151. if (isUndefined(config.clockTimezone))
  152. config.clockTimezone = 0;
  153. if (isUndefined(sinusbot.getVar("maxOnlineRecord")))
  154. sinusbot.setVar("maxOnlineRecord", 0);
  155. if (isUndefined(sinusbot.getVar("tempChannels")))
  156. sinusbot.setVar("tempChannels", []);
  157. if (isUndefined(config.helpClosePrefix))
  158. config.helpClosePrefix = "(Closed)";
  159. if (isUndefined(config.helpUserNotification))
  160. config.helpUserNotification = "Hi [b]s0[/b]! Staff member will help you soon! [b]Online staff:[/b] s1";
  161. if (isUndefined(config.helpUserNotification2))
  162. config.helpUserNotification2 = "Hi [b]s0[/b]! Sorry, but all our staff is offline come back later.";
  163. if (isUndefined(config.helpStaffNotification))
  164. config.helpStaffNotification = "User [b]s0[/b] is waiting in support zone! [b]Online staff:[/b] s1";
  165. if (isUndefined(config.helpStaffAway))
  166. config.helpStaffAway = "Sorry! Our staff is currently busy. They will contact you as soon as possible.";
  167. if (isUndefined(config.helpStaffJoined))
  168. config.helpStaffJoined = "Staff member [b]s0[/b] joined to help [b]s1[/b].";
  169. if (isUndefined(config.autoTempChannelsName))
  170. config.autoTempChannelsName = "Temporary Channel s0";
  171. if (isUndefined(config.autoTempChannelsMessage))
  172. config.autoTempChannelsMessage = "I created temporary channel as you wish!";
  173. if (isUndefined(config.autoTempChannelsPassMessage))
  174. config.autoTempChannelsPassMessage = "Your channel password: [b]s0[/b]";
  175. if (isUndefined(config.autoTempChannelsRestricted))
  176. config.autoTempChannelsRestricted = "Sorry! You are not allowed to get temporary channel.";
  177. if (isUndefined(config.autoTempChannelsErrorMessage))
  178. config.autoTempChannelsErrorMessage = "I can't created temporary channel for you right now, try later.";
  179. if (isUndefined(config.onlineUsersChannel))
  180. config.onlineUsersChannel = "Users Online: s0";
  181. if (isUndefined(config.maxOnlineUsersChannel))
  182. config.maxOnlineUsersChannel = "Max Users Online: s0";
  183. if (isUndefined(config.maxOnlineUsersChannelDesc))
  184. config.maxOnlineUsersChannelDesc = "Last record date: s0";
  185. if (isUndefined(config.staffOnlineChannelName))
  186. config.staffOnlineChannelName = "Staff Online: s0";
  187. if (isUndefined(config.staffOnlineChannelZero))
  188. config.staffOnlineChannelZero = "none";
  189. if (isUndefined(config.staffOnlineChannelDesc))
  190. config.staffOnlineChannelDesc = "s0 - s1 is online";
  191. if (isUndefined(config.autoMusicCodecNew))
  192. config.autoMusicCodecNew = '5#10';
  193. if (isUndefined(config.clockFormat))
  194. config.clockFormat = "[cspacer*] s3:s4 | s0.s1.s2";
  195.  
  196. try {
  197. var helpChannelsId = config.helpChannelsId.toString().split(',');
  198. var helpGroupsId = config.helpGroupsId.toString().split(',');
  199. var helpIgnoredGroupsId = config.helpIgnoredGroupsId.toString().split(',');
  200. var autoTempChannelsId = config.autoTempChannelsId.toString().split(',');
  201. var autoTempChannelsRestrictedGroups = config.autoTempChannelsRestrictedGroups.toString().split(',');
  202. var autoMusicCodecNew = config.autoMusicCodecNew.toString().split('#');
  203. } catch (e) {
  204. sinusbot.log('Your configuration is wrong! Check if you typed correct values.');
  205. sinusbot.log('START - Error code: ' + e);
  206. sinusbot.log('Line: ' + e.lineNumber + ' Column: ' + e.columnNumber);
  207. sinusbot.log('Setting1: ' + config.helpChannelsId);
  208. sinusbot.log('Setting2: ' + config.helpGroupsId);
  209. sinusbot.log('Setting3: ' + config.helpIgnoredGroupsId);
  210. sinusbot.log('Setting4: ' + config.autoTempChannelsId);
  211. sinusbot.log('Setting5: ' + config.autoTempChannelsRestrictedGroups);
  212. sinusbot.log('Setting6: ' + config.autoMusicCodecNew);
  213. sinusbot.log('END ---------------------- END');
  214.  
  215. //Temp again defaults
  216. helpChannelsId = -1;
  217. helpGroupsId = -1;
  218. helpIgnoredGroupsId = -1;
  219. autoTempChannelsId = -1;
  220. autoTempChannelsRestrictedGroups = -1;
  221. autoMusicCodecNew = [5,10];
  222. }
  223.  
  224. /*
  225. Support Channels auto poke function event section
  226. */
  227. sinusbot.on('clientMove', function (e) {
  228. var isStaff = haveGroupFromArray(e, helpGroupsId);
  229. var isHelpClosed = sinusbot.getVar('helpClosed');
  230. var isIgnored = haveGroupFromArray(e, helpIgnoredGroupsId);
  231. var staff = sinusbot.getVar('staff');
  232. var usersOnHelp = sinusbot.getVar('usersOnHelp');
  233.  
  234. if(config.helpAwayMode == 0 && e.client.away == 1)
  235. isStaff = false;
  236.  
  237. var objectId = getObjectIdArray(staff, e.clientId);
  238.  
  239. if (isStaff != false) {
  240.  
  241. for (var i = 0; i < staff.length; i++) {
  242. if (getUserChannelId(staff[i].id) == 0) {
  243. if(config.staffOnlineChannelSublist == 0) {
  244. var channelId = getChannelId(staff[i].nick + ' | ' + staff[i].groups.n, config.staffOnlineChannelId);
  245. }
  246. staff.splice(i, 1);
  247. if(!isUndefined(channelId))
  248. {
  249. channelDelete(channelId, true);
  250. }
  251. }
  252. }
  253.  
  254. if (objectId == -1) {
  255. var group = haveGroupFromArray(e, helpGroupsId);
  256. staff.push({id: e.clientId, uuid: e.clientUid, nick: e.clientNick, groups: group});
  257. if(config.staffOnlineChannelSublist == 0) {
  258. var channel = {
  259. name: e.clientNick + ' | ' + group.n,
  260. parent: config.staffOnlineChannelId,
  261. topic: e.clientNick + ' | ' + group.n,
  262. description: '[URL=client://0/' + e.clientUid + ']' + e.clientNick + '[/URL] | ' + group.n +
  263. ' - [COLOR=#00aa00]online![/COLOR]',
  264. enc: 1,
  265. perm: 0,
  266. sperm: 1,
  267. maxClients: 0,
  268. default: 0
  269. };
  270. channelCreate(channel);
  271. }
  272. }
  273. } else if (objectId >= 0) {
  274. if(config.staffOnlineChannelSublist == 0) {
  275. var channelId = getChannelId(staff[objectId].nick + ' | ' + staff[objectId].groups.n, config.staffOnlineChannelId);
  276. }
  277. staff.splice(objectId, 1);
  278. if(!isUndefined(channelId)) {
  279. channelDelete(channelId, true);
  280. }
  281. }
  282.  
  283. if(config.helpCloseNoStaff == 0) {
  284. if (staff.length == 0 && isHelpClosed == 0) {
  285. for (var i = 0; i < helpChannelsId.length; i++) {
  286. var channel = getChannelParams(parseInt(helpChannelsId[i]));
  287. if (channel == false)
  288. continue;
  289. var newName = config.helpClosePrefix + channel.name;
  290. channelUpdate(channel.id, {name: newName, maxClients: 0});
  291. }
  292. sinusbot.setVar('helpClosed', 1);
  293. } else if (staff.length > 0 && isHelpClosed == 1) {
  294. for (var i = 0; i < helpChannelsId.length; i++) {
  295. var channel = getChannelParams(parseInt(helpChannelsId[i]));
  296. if (channel == false)
  297. continue;
  298. var newName = channel.name.substring(config.helpClosePrefix.length);
  299. channelUpdate(channel.id, {name: newName, maxClients: -1});
  300. }
  301. sinusbot.setVar('helpClosed', 0);
  302. }
  303. }
  304.  
  305. sinusbot.setVar('staff', staff);
  306.  
  307. if (isChannelInArray(e.newChannel, helpChannelsId)) {
  308. if (isIgnored == false) {
  309. if (isUndefined(staff) || staff.length <= 0) {
  310. sendUserNotification(config.helpUsersNotificationType, e.clientId, replaceSNVariables(config.helpUserNotification2, e.clientNick));
  311. usersOnHelp.push({
  312. clientId: e.clientId,
  313. clientUid: e.clientUid,
  314. clientNick: e.clientNick,
  315. newChannel: e.newChannel,
  316. oldChannel: e.oldChannel,
  317. action: 1,
  318. staffReaction: false
  319. });
  320. } else {
  321. for (var i = 0; i < staff.length; i++) {
  322. var message = replaceSNVariables(config.helpStaffNotification, e.clientNick, staff.length);
  323. if (config.helpStaffNotificationType == 0) {
  324. message = replaceSNVariables(
  325. config.helpStaffNotification,
  326. '[URL=client://0/' + e.clientUid + ']' + e.clientNick + '[/URL]',
  327. staff.length
  328. );
  329. }
  330. sendUserNotification(config.helpStaffNotificationType, staff[i].id, message);
  331. }
  332. sendUserNotification(config.helpUsersNotificationType, e.clientId, replaceSNVariables(config.helpUserNotification, e.clientNick, staff.length));
  333. usersOnHelp.push({
  334. clientId: e.clientId,
  335. clientUid: e.clientUid,
  336. clientNick: e.clientNick,
  337. newChannel: e.newChannel,
  338. oldChannel: e.oldChannel,
  339. action: 2,
  340. staffReaction: false
  341. });
  342. if (config.helpStaffAwayTime > 0) {
  343. setTimeout(function () {
  344. var userOnHelpF = sinusbot.getVar('usersOnHelp');
  345. for (var i = 0; i < userOnHelpF.length; i++) {
  346. var user = userOnHelpF[i];
  347. if (user.clientId == e.clientId && user.staffReaction != true) {
  348. sendUserNotification(config.helpUsersNotificationType, e.clientId, config.helpStaffAway);
  349. }
  350. }
  351. }, 1000 * config.helpStaffAwayTime);
  352. }
  353. }
  354. sinusbot.setVar('usersOnHelp', usersOnHelp);
  355.  
  356. } else {
  357. for (var i = 0; i < usersOnHelp.length; i++) {
  358. var user = usersOnHelp[i];
  359. if (user.newChannel == e.newChannel && user.staffReaction != true) {
  360. if(config.staffJoinSupport == 0) {
  361. for (var i = 0; i < staff.length; i++) {
  362. if (staff[i].id != e.clientId) {
  363. var message = replaceSNVariables(config.helpStaffJoined, e.clientNick, user.clientNick);
  364. if (config.helpStaffNotificationType == 0) {
  365. message = replaceSNVariables(
  366. config.helpStaffJoined,
  367. '[URL=client://0/' + e.clientUid + ']' + e.clientNick + '[/URL]',
  368. '[URL=client://0/' + user.clientUid + ']' + user.clientNick + '[/URL]'
  369. );
  370. }
  371. sendUserNotification(config.helpStaffNotificationType, staff[i].id, message);
  372. }
  373. }
  374. }
  375. user.staffReaction = true;
  376. usersOnHelp[i] = user;
  377. }
  378. }
  379. sinusbot.setVar('usersOnHelp', usersOnHelp);
  380. }
  381. } else if (isChannelInArray(e.oldChannel, helpChannelsId)) {
  382. if (isIgnored == false) {
  383. for (var i = 0; i < usersOnHelp.length; i++) {
  384. var user = usersOnHelp[i];
  385. if (user.clientId == e.clientId && user.oldChannel == e.oldChannel) {
  386. usersOnHelp.splice(i, 1);
  387. }
  388. }
  389. sinusbot.setVar('usersOnHelp', usersOnHelp);
  390. }
  391. }
  392.  
  393. });
  394. /*
  395. //END Support Channels auto poke function section //END
  396. */
  397.  
  398. /*
  399. Automatic temporary channel events section
  400. */
  401. sinusbot.on('clientMove', function (e) {
  402. if (isChannelInArray(e.newChannel, autoTempChannelsId)) {
  403. if (haveGroupFromArray(e, autoTempChannelsRestrictedGroups) == false) {
  404. var autoTempChannelParentId = parseInt(config.autoTempChannelsParentId);
  405. var channelName = config.autoTempChannelsName;
  406. var channelPassword = randomString(config.autoTempChannelsPassLength, '#');
  407.  
  408. for (var i = 1; i <= 1000; i++) {
  409. var tempChannelName = replaceSNVariables(channelName, i);
  410.  
  411. if (getChannelId(tempChannelName, autoTempChannelParentId) == false) {
  412. channelName = tempChannelName;
  413. break;
  414. }
  415. }
  416.  
  417. var channel = {
  418. name: channelName,
  419. parent: autoTempChannelParentId,
  420. password: channelPassword,
  421. topic: e.clientNick,
  422. enc: 1,
  423. perm: 0,
  424. sperm: 1,
  425. default: 0
  426. };
  427.  
  428. var channelCreated = {
  429. channelName: channelName,
  430. channelParentId: autoTempChannelParentId,
  431. creator: e,
  432. password: channelPassword
  433. };
  434.  
  435. if(config.autoTempChannelsPass == 1) {
  436. channel.password = '';
  437. channelCreated.password = '';
  438. }
  439.  
  440. channelCreate(channel);
  441.  
  442. var onChannelCreate = sinusbot.getVar("onChannelCreate");
  443.  
  444. if (!isUndefined(onChannelCreate) && onChannelCreate.indexOf(channelCreated) == -1) {
  445. onChannelCreate.push(channelCreated);
  446. sinusbot.setVar("onChannelCreate", onChannelCreate);
  447. } else {
  448. sinusbot.log('Something goes wrong when adding you new channel to event list checks!');
  449. }
  450. } else {
  451. chatPrivate(e.clientId, config.autoTempChannelsRestricted);
  452. }
  453. }
  454. });
  455.  
  456. sinusbot.on('channelCreate', function (channel) {
  457. var onChannelCreate = sinusbot.getVar("onChannelCreate");
  458. if(!isUndefined(onChannelCreate) && !isEmpty(onChannelCreate)) {
  459. for (var i = 0; i < onChannelCreate.length; i++) {
  460. if (onChannelCreate[i].channelName == channel.name && onChannelCreate[i].channelParentId == channel.parent) {
  461. if (channel.id > 0) {
  462. var tempChannels = sinusbot.getVar("tempChannels");
  463.  
  464. sinusbot.log('Temp channel ' + channel.name + ' ID: ' + channel.id + ' was created by ' + onChannelCreate[i].creator.clientId + "@" + onChannelCreate[i].creator.clientNick);
  465. tempChannels.push(channel.id);
  466. sinusbot.setVar("tempChannels", tempChannels);
  467. move(onChannelCreate[i].creator.clientId, channel.id);
  468. chatPrivate(onChannelCreate[i].creator.clientId, config.autoTempChannelsMessage);
  469. if (config.autoTempChannelsPass == 0) {
  470. chatPrivate(onChannelCreate[i].creator.clientId, replaceSNVariables(config.autoTempChannelsPassMessage, onChannelCreate[i].password));
  471. }
  472. } else {
  473. sinusbot.log('Something goes wrong when creating auto temp channel! ' + channel.id);
  474. chatPrivate(onChannelCreate[i].creator.clientId, config.autoTempChannelsErrorMessage);
  475. }
  476.  
  477. onChannelCreate.splice(i, 1);
  478. sinusbot.setVar("onChannelCreate", onChannelCreate);
  479. break;
  480. }
  481. }
  482. }
  483. });
  484.  
  485. /*
  486. Timer checks events
  487. */
  488. setInterval(function () {
  489. //Temporary channels inactive deleting
  490. var tempChannels = sinusbot.getVar("tempChannels");
  491. if(!isUndefined(tempChannels) && !isEmpty(tempChannels)) {
  492. for (var i = 0; i < tempChannels.length; i++) {
  493. if (getClientsNumber(tempChannels[i]) == 0) {
  494. channelDelete(tempChannels[i], true);
  495. sinusbot.log('Temp channel ' + tempChannels[i] + ' was deleted by check timer.');
  496. tempChannels.splice(i, 1);
  497. }
  498. }
  499. sinusbot.setVar("tempChannels", tempChannels);
  500. }
  501. //Users on help check
  502. var userOnHelp = sinusbot.getVar('usersOnHelp');
  503. if(!isUndefined(userOnHelp) && !isEmpty(userOnHelp)) {
  504. for (var i = 0; i < userOnHelp.length; i++) {
  505. var userChannel = getUserChannelId(userOnHelp[i].clientId);
  506. var out = true;
  507. for (var i2 = 0; i < helpChannelsId.length; i2++) {
  508. if (userChannel == helpChannelsId[i2]) {
  509. out = false;
  510. }
  511. }
  512. if (out) {
  513. userOnHelp.splice(i, 1);
  514. }
  515. }
  516.  
  517. sinusbot.setVar('usersOnHelp', userOnHelp);
  518. }
  519. }, 1000 * config.botChecksEvery);
  520.  
  521. setInterval(function () {
  522. if(config.clockChannel != -1)
  523. {
  524. var clockChannel = getChannelParams(config.clockChannel);
  525. if (!isUndefined(clockChannel)) {
  526. var date_local = new Date();
  527. var date = new Date(date_local.getUTCFullYear(), date_local.getUTCMonth(), date_local.getDate(),
  528. date_local.getUTCHours(), date_local.getUTCMinutes(), date_local.getUTCSeconds());
  529.  
  530. var timezone = parseInt(config.clockTimezone);
  531. var hours = date.getHours();
  532. if (timezone < 9) {
  533. hours += timezone;
  534. } else if (timezone > 8) {
  535. hours -= (timezone - 8);
  536. }
  537. var minutes = date.getMinutes();
  538. if (minutes < 10) {
  539. minutes = '0' + minutes;
  540. }
  541.  
  542. clockChannel.name = replaceSNVariables(config.clockFormat, date.getDate(), date.getMonth() + 1, date.getFullYear(),
  543. hours, minutes);
  544.  
  545. channelUpdate(clockChannel.id, [clockChannel.name]);
  546. }
  547. }
  548. }, 1000 * config.botChecksEvery);
  549.  
  550. /*
  551. Welcome poke information event
  552. */
  553. sinusbot.on('clientMove', function (e) {
  554. if (e.oldChannel == 0 && config.registerMessageOn == 0) {
  555. var isNew = !haveGroupOnServer(e, parseInt(config.registerGroupId));
  556.  
  557. if (isNew && haveGroupFromArray(e, helpGroupsId) == false) {
  558. var message = config.registerWelcomeMessage;
  559. if (config.registerMessageType == 0) {
  560. poke(e.clientId, message);
  561. } else {
  562. chatPrivate(e.clientId, message);
  563. }
  564. }
  565. }
  566. });
  567.  
  568. /*
  569. Information event for online users channel updates and max online record
  570. */
  571. sinusbot.on('clientMove', function (e) {
  572. if (e.oldChannel == 0 || e.newChannel == 0) {
  573. var clients = getClientsNumber();
  574. onlineClientsChannel(clients);
  575. sinusbot.setVar("onlineClients", clients);
  576.  
  577. if (sinusbot.getVar("maxOnlineRecord") < clients) {
  578. maxOnlineClientsChannel(clients);
  579. sinusbot.setVar("maxOnlineRecord", clients);
  580. }
  581.  
  582. var maxOnlineChannel = getChannelParams(config.maxOnlineUsersChannel);
  583. if(!isUndefined(maxOnlineChannel.name)) {
  584. var wasUpdated = maxOnlineChannel.name.indexOf(sinusbot.getVar('maxOnlineRecord')) == -1;
  585. if (wasUpdated) {
  586. maxOnlineClientsChannel(sinusbot.getVar("maxOnlineRecord"));
  587. }
  588. }
  589. }
  590.  
  591. //Staff Groups bypass
  592. if (haveGroupFromArray(e, helpGroupsId) != false) {
  593. staffOnlineChannel(sinusbot.getVar('staff').length, sinusbot.getVar('staff'));
  594. }
  595. });
  596.  
  597. /*
  598. Logging event to track channels users activity
  599. */
  600. sinusbot.on('clientMove', function (e) {
  601. var timeNow = new Date().getTime();
  602. var channelLogNewChannel = [];
  603. var channelLogOldChannel = [];
  604. var entryTypeOld = 0;
  605. var entryTypeNew = 0;
  606.  
  607. if (isUndefined(sinusbot.getVar(e.newChannel))) {
  608. channelLogNewChannel = [];
  609. entryTypeNew = (e.newChannel == 0) ? 3 : 1;
  610. channelLogNewChannel.push([timeNow, e.clientUid, e.clientNick, e.oldChannel, e.newChannel, entryTypeNew]);
  611. sinusbot.setVar(e.newChannel, channelLogNewChannel);
  612. } else {
  613. channelLogNewChannel = sinusbot.getVar(e.newChannel);
  614. entryTypeNew = (e.newChannel == 0) ? 3 : 1;
  615. channelLogNewChannel.push([timeNow, e.clientUid, e.clientNick, e.oldChannel, e.newChannel, entryTypeNew]);
  616. sinusbot.setVar(e.newChannel, channelLogNewChannel);
  617. }
  618.  
  619. if (isUndefined(sinusbot.getVar(e.oldChannel))) {
  620. channelLogOldChannel = [];
  621. entryTypeOld = (e.oldChannel == 0) ? 4 : 2;
  622. channelLogOldChannel.push([timeNow, e.clientUid, e.clientNick, e.oldChannel, e.newChannel, entryTypeOld]);
  623. sinusbot.setVar(e.oldChannel, channelLogOldChannel);
  624. } else {
  625. channelLogOldChannel = sinusbot.getVar(e.oldChannel);
  626. entryTypeOld = (e.oldChannel == 0) ? 4 : 2;
  627. channelLogOldChannel.push([timeNow, e.clientUid, e.clientNick, e.oldChannel, e.newChannel, entryTypeOld]);
  628. sinusbot.setVar(e.oldChannel, channelLogOldChannel);
  629. }
  630. });
  631.  
  632. /*
  633. Logging event to clear channel log activity on channel delete
  634. */
  635. sinusbot.on('channelDelete', function (channel) {
  636. sinusbot.unsetVar(channel.id.toString());
  637. });
  638.  
  639. /*
  640. Command event for getting channel activity log
  641. */
  642. sinusbot.on('chat', function (chat) {
  643. if (getNick() != chat.clientNick && chat.mode == 1 && haveGroupFromArray(chat, helpGroupsId) != false) {
  644. if (chat.msg == 'info' || chat.msg == 'info server' || chat.msg == 'info all' || new RegExp('info [0-9]+').test(chat.msg)) {
  645. if (chat.msg == 'info' || chat.msg == 'info all') {
  646. var channelLog = sinusbot.getVar(chat.channel);
  647. } else if (new RegExp('info [0-9]+').test(chat.msg)) {
  648. var channelLog = sinusbot.getVar(parseInt(chat.msg.split(' ')[1]));
  649. } else if (chat.msg == 'info server') {
  650. var channelLog = sinusbot.getVar(0);
  651. }
  652.  
  653. if (isUndefined(channelLog) || channelLog.length <= 0) {
  654. chatPrivate(chat.clientId, 'No entry logs for this channel!');
  655. } else {
  656. if (channelLog.length < 50) {
  657. var history = 0;
  658. } else {
  659. if(chat.msg == 'info all') {
  660. var history = 0;
  661. } else {
  662. var history = channelLog.length - 50;
  663. }
  664. }
  665. for (var i = history; i < channelLog.length; i++) {
  666. var logEntry = channelLog[i];
  667. var entryType = 'error';
  668.  
  669. switch (logEntry[5]) {
  670. case 1:
  671. entryType = 'entered';
  672. break;
  673. case 2:
  674. entryType = 'leaved';
  675. break;
  676. case 3:
  677. entryType = 'disconnected from server';
  678. break;
  679. case 4:
  680. entryType = 'joined to server';
  681. break;
  682. }
  683.  
  684. chatPrivate(chat.clientId, 'Date: ' + new Date(logEntry[0]).toISOString() + '/ClientId: '
  685. + logEntry[1] + '/ClientName: ' + logEntry[2] + '/OldChannel: '
  686. + logEntry[3] + '/NewChannel: ' + logEntry[4] + '/' + entryType);
  687. }
  688. chatPrivate(chat.clientId, 'All records - ' + channelLog.length);
  689. }
  690. }
  691. }
  692. });
  693.  
  694. /*
  695. Web Data API Event
  696. */
  697. sinusbot.on('api:web', function (data) {
  698. return sinusbot.getVar('staff').length;
  699. });
  700.  
  701. /*
  702. Event will handle bot joining to channel and change codec automatically if autoMusicCodecBot is On
  703. */
  704. sinusbot.on('botMove', function (info) {
  705. if (config.autoMusicCodecBot == 0) {
  706. var channelCodec = sinusbot.getVar('channelCodec');
  707. var botMove = sinusbot.getVar('botMove' + self);
  708. if (info.newChannel != 0) {
  709. if (isEmpty(botMove)) {
  710. botMove = {newChannel: info.newChannel, oldChannel: 0};
  711. } else {
  712. botMove = {newChannel: info.newChannel, oldChannel: botMove.newChannel};
  713. }
  714.  
  715. var channel = getChannelParams(info.newChannel);
  716. channelCodec.push({id: info.newChannel, codec: channel.codec, quality: channel.quality});
  717. channelUpdate(info.newChannel, {
  718. codec: parseInt(autoMusicCodecNew[0]),
  719. quality: parseInt(autoMusicCodecNew[1])
  720. });
  721. }
  722. if (botMove.oldChannel != 0) {
  723. var codec = 4;
  724. var quality = 6;
  725. for (var i = 0; i < channelCodec.length; i++) {
  726. if (channelCodec[i].id == botMove.oldChannel) {
  727. codec = channelCodec[i].codec;
  728. quality = channelCodec[i].quality;
  729. break;
  730. }
  731. }
  732. channelUpdate(botMove.oldChannel, {codec: codec, quality: quality});
  733. channelCodec = removeFromArray(channelCodec, {
  734. id: botMove.oldChannel,
  735. codec: codec,
  736. quality: quality
  737. });
  738. }
  739.  
  740.  
  741. sinusbot.setVar('botMove' + self, botMove);
  742. sinusbot.setVar('channelCodec', channelCodec);
  743. }
  744. });
  745.  
  746. /*
  747. Update clients online information channel
  748. */
  749. function onlineClientsChannel(online) {
  750. if (!isUndefined(config.onlineUsersChannel) || !isUndefined(config.onlineUsersChannelId)) {
  751. var channel = getChannelParams(parseInt(config.onlineUsersChannelId));
  752. if (channel != false) {
  753. channel.name = replaceSNVariables(config.onlineUsersChannel, online);
  754. channelUpdate(channel.id, {name: channel.name});
  755. }
  756. }
  757. }
  758.  
  759. /*
  760. Update max clients online information channel
  761. */
  762. function maxOnlineClientsChannel(maxOnline) {
  763. if (!isUndefined(config.maxOnlineUsersChannel) || !isUndefined(config.maxOnlineUsersChannelId)) {
  764. var channel = getChannelParams(parseInt(config.maxOnlineUsersChannelId));
  765. if (channel != false) {
  766. var date = new Date().toISOString();
  767. channel.name = replaceSNVariables(config.maxOnlineUsersChannel, maxOnline);
  768. channel.description = replaceSNVariables(config.maxOnlineUsersChannelDesc, date);
  769. channelUpdate(channel.id, {name: channel.name, description: channel.description});
  770. }
  771. }
  772. }
  773.  
  774. /*
  775. Update staff online information channel
  776. */
  777. function staffOnlineChannel(staffOnline, staffList) {
  778. if (!isUndefined(config.staffOnlineChannelName) || !isUndefined(config.staffOnlineChannelId)) {
  779. var channel = getChannelParams(parseInt(config.staffOnlineChannelId));
  780. if (channel != false) {
  781. if(channel.name.indexOf(staffOnline) == -1) {
  782. channel.name = replaceSNVariables(config.staffOnlineChannelName,
  783. (staffOnline != 0 ? staffOnline.toString() : config.staffOnlineChannelZero));
  784. var parsedList = '';
  785. for (var i = 0; i < staffList.length; i++) {
  786. if(staffList.length != 0) {
  787. var member = staffList[i];
  788. var text = "\n" + replaceSNVariables(config.staffOnlineChannelDesc,
  789. '[URL=client://0/' + member.uuid + ']' + member.nick + '[/URL]', member.groups.n);
  790. parsedList += text;
  791. }
  792.  
  793. }
  794. channelUpdate(channel.id, {name: channel.name, description: parsedList.toString()});
  795. }
  796. }
  797. }
  798. }
  799.  
  800. /*
  801. Function with will send notification poke or chat message
  802. */
  803. function sendUserNotification(notificationType, user, message) {
  804. if (notificationType == 0)
  805. chatPrivate(user, message);
  806. else if (notificationType == 1)
  807. poke(user, message);
  808. }
  809.  
  810. /*
  811. If user have specific group in client server groups array
  812. */
  813. function haveGroupOnServer(e, group) {
  814. if (typeof(group) != 'number') {
  815. group = parseInt(group);
  816. }
  817.  
  818. var result = false;
  819. for (var i = 0; i < e.clientServerGroups.length; i++) {
  820. if (e.clientServerGroups[i].i == group) {
  821. return result = e.clientServerGroups[i];
  822. }
  823. }
  824. return result;
  825. }
  826.  
  827. /*
  828. If user have specific group of clientServerGroups from input array
  829. */
  830. function haveGroupFromArray(e, arrayGroups) {
  831. var result = false;
  832.  
  833. for (var i = 0; i < arrayGroups.length; i++) {
  834. var group = arrayGroups[i];
  835. if (typeof(group) != 'number') {
  836. group = parseInt(group);
  837. }
  838. var groupOnServer = haveGroupOnServer(e, group);
  839. if (groupOnServer != false) {
  840. return result = groupOnServer;
  841. }
  842. }
  843. return result;
  844. }
  845.  
  846. /*
  847. Generate random hash with specific length and chars selected
  848. */
  849. function randomString(length, chars) {
  850. var mask = '';
  851. if (chars.indexOf('a') > -1) mask += 'abcdefghijklmnopqrstuvwxyz';
  852. if (chars.indexOf('A') > -1) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  853. if (chars.indexOf('#') > -1) mask += '0123456789';
  854. var result = '';
  855. for (var i = length; i > 0; --i) result += mask[Math.round(Math.random() * (mask.length - 1))];
  856.  
  857. return result;
  858. }
  859.  
  860. /*
  861. Function to get the channelId based on the channelName and the parentId
  862. */
  863. function getChannelId(channelName, parentId) {
  864. var channels = getChannels();
  865. for (var i = 0; i < channels.length; i++) {
  866. if (channels[i].name == channelName && channels[i].parent == parentId) {
  867. return channels[i].id;
  868. }
  869. }
  870. return false;
  871. }
  872.  
  873. /*
  874. Function to get the channelParams based on the channelName and the parentId or channelId
  875. */
  876. function getChannelParams(channelId, channelName, parentId) {
  877. var channels = getChannels();
  878. for (var i = 0; i < channels.length; i++) {
  879. var channel = channels[i];
  880. if (channel.id == channelId || (channel.name == channelName && channel.parent == parentId)) {
  881. return channel;
  882. }
  883. }
  884. return false;
  885. }
  886.  
  887. /*
  888. Function return status of new user channel if it is one from array
  889. */
  890. function isChannelInArray(channel, array) {
  891. var result = false;
  892.  
  893. for (var i = 0; i < array.length; i++) {
  894. var id = array[i];
  895. if (typeof(id) != 'number') {
  896. id = parseInt(id);
  897. }
  898.  
  899. if (channel == id) {
  900. result = true;
  901. break;
  902. }
  903. }
  904. return result;
  905. }
  906.  
  907. /*
  908. Function return number of users online total or channel based
  909. */
  910. function getClientsNumber(channelId) {
  911. var channel, channels = [];
  912. var clients = 0;
  913.  
  914. channels = getChannels();
  915. if (typeof channelId == 'undefined') {
  916. for (var i = 0; i < channels.length; i++) {
  917. channel = channels[i];
  918. clients += channel.clients.length;
  919. }
  920. return clients;
  921. } else {
  922. channel = getChannel(channelId);
  923. if (isEmpty(channel.clients)) {
  924. return 0;
  925. } else {
  926. return channel.clients.length;
  927. }
  928. }
  929. return 0;
  930. }
  931.  
  932. /*
  933. Function get id of channel where specific user is
  934. */
  935. function getUserChannelId(clientId) {
  936. var channel, channels = [];
  937.  
  938. channels = getChannels();
  939.  
  940. for (var i = 0; i < channels.length; i++) {
  941. channel = channels[i];
  942. if (!isEmpty(channel.clients)) {
  943. for (var i2 = 0; i2 < channel.clients.length; i2++) {
  944. if (channel.clients[i2].id == clientId) {
  945. return channel.id;
  946. }
  947. }
  948. }
  949. }
  950.  
  951. return 0;
  952. }
  953.  
  954. /*
  955. Check if object is empty
  956. */
  957. function isEmpty(myObject) {
  958. for (var key in myObject) {
  959. if (myObject.hasOwnProperty(key)) {
  960. return false;
  961. }
  962. }
  963. return true;
  964. }
  965.  
  966. /*
  967. Function find and replace specific char in string
  968. */
  969. function replaceChar(str, find, replace) {
  970. return str.replace(find, replace);
  971. }
  972.  
  973. /*
  974. Function replace as many 'sN' variables in string as needed
  975. */
  976. function replaceSNVariables(string) {
  977. for (var i = 1; i < arguments.length; i++) {
  978. var snI = i - 1;
  979. var sn = 's' + snI;
  980. string = replaceChar(string, sn, arguments[i].toString());
  981. }
  982. return string;
  983. }
  984.  
  985. /*
  986. Function remove specific element from array
  987. */
  988. function removeFromArray(array, item) {
  989. if(isUndefined(array) || isUndefined(item))
  990. return false;
  991. var index = array.indexOf(item);
  992. if (index > -1) {
  993. array.splice(index, 1);
  994. }
  995. return array;
  996. }
  997.  
  998. /*
  999. Filter object in array
  1000. */
  1001. function getObjectIdArray(array, id) {
  1002. for (var i = 0, len = array.length; i < len; i++) {
  1003. if (array[i].id === id) {
  1004. return i;
  1005. }
  1006. }
  1007. return -1;
  1008. }
  1009.  
  1010. // -> isUndefined(variable)
  1011. function isUndefined(variable) {
  1012. return typeof variable == "undefined";
  1013. }
  1014.  
  1015. }
  1016. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement