Guest User

Untitled

a guest
Jul 23rd, 2017
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. /*
  2. * Copyright (C) 2016 Luigi Martinelli <xdefconhd@gmail.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * @author Luigi Martinelli <xdefconhd@gmail.com>
  18. *
  19. */
  20.  
  21.  
  22. registerPlugin({
  23. name: 'Online Users Record',
  24. version: '1.0',
  25. description: 'This script will monitor and save in a channel the highest number of concurrent online users',
  26. author: 'Luigi M. - xDefcon (xdefconhd@gmail.com)',
  27. vars: {
  28. channel: {
  29. title: 'Channel to be updated', type: 'channel'
  30. }, delayTime: {
  31. title: 'Run a check every [seconds]',
  32. type: 'number',
  33. placeholder: 'Check online users every... (Default: 20sec).'
  34. }, customString: {
  35. title: 'Channel name (use %u to indicate the users record)',
  36. type: 'string',
  37. placeholder: 'Change the name of the channel (Example: Max online users: %u)'
  38. }, debugSwitch: {
  39. title: 'Enable debug messages?', type: 'select', options: ['no', 'yes']
  40. }
  41. }
  42. }, function (sinusbot, config) {
  43. if (typeof config.debugSwitch === 'undefined') {
  44. config.debugSwitch = 0;
  45. }
  46. if (typeof config.delayTime === 'undefined') {
  47. config.delayTime = 20;
  48. debug("Check delay set to 20 seconds.");
  49. } else if (config.delayTime < 2) {
  50. debug("To avoid lag/crashes, the delay MUST be greater than 2 seconds."); config.delayTime = 3;
  51. debug("Delay set to 3 seconds.");
  52. } else {
  53. debug("Check delay set to " + config.delayTime + " seconds.");
  54. }
  55. if (typeof config.channel === 'undefined') {
  56. debug("No channel selected, SCRIPT STOPPED.");
  57. return;
  58. }
  59.  
  60. setInterval(onlineCheck, config.delayTime * 1000);
  61.  
  62. function debug(msg) {
  63. if (config.debugSwitch == 1) {
  64. sinusbot.log("[DEBUG] " + msg);
  65. }
  66. }
  67.  
  68. function onlineCheck() {
  69. var nowOnline = getClients();
  70. var chanOnline = 0;
  71. var channel = sinusbot.getChannel(config.channel);
  72. var name = channel.name;
  73. var confClearName = config.customString.replace("%u", "");
  74. var chanName;
  75.  
  76. var firstOccur = name.indexOf(confClearName);
  77.  
  78. if (firstOccur === -1) {
  79. debug("Channel not updated, updating now with " + nowOnline + " users online.");
  80. chanName = config.customString.replace("%u", nowOnline);
  81. sinusbot.channelUpdate(config.channel, {name: chanName});
  82. } else {
  83. if (firstOccur === 0) {
  84. chanOnline = name.substring(confClearName.length);
  85. } else {
  86. chanOnline = name.substring(0, firstOccur);
  87. chanOnline += name.substring(firstOccur + confClearName.length);
  88. }
  89. if (parseInt(chanOnline, 10) < nowOnline) {
  90. chanName = config.customString.replace("%u", nowOnline);
  91. sinusbot.channelUpdate(config.channel, {name: chanName});
  92. } else {
  93. debug("There are " + nowOnline + " users connected, the record is " + chanOnline);
  94. debug("Channel not updated.");
  95. }
  96. }
  97. }
  98.  
  99. function getClients() {
  100. var channel, channels, x = 0;
  101. channels = sinusbot.getChannels();
  102. for (var i = 0; i < channels.length; i++) {
  103. channel = channels[i];
  104. x += channel.clients.length;
  105. }
  106. return x;
  107. }
  108. });
Add Comment
Please, Sign In to add comment