A Twitch.tv viewer reward and games system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.8 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. /**
  2. * api:
  3. * Commands(irc object, database object);
  4. *
  5. * example:
  6. * commands = require('./mysql/commands.js')(irc, db, {
  7. * bot_name : 'bot name',
  8. * currency: 'currency name'
  9. * });
  10. */
  11. //-------- Construct ---------
  12. function Commands(irc, db, options) {
  13. var __self = this;
  14. __self.irc = irc;
  15. __self.db = db;
  16. // config
  17. __self.config = options || {};
  18. __self.config.bot_name = options.bot_name || '';
  19. __self.config.currency = options.currency || 'coins';
  20. __self.config.exchange = options.exchange || {};
  21. __self.command_list = [];
  22. }
  23. //-------- Methods --------
  24. Commands.prototype.start = function() {
  25. var __self = this,
  26. sql = 'SELECT * FROM commands';
  27. __self.db.execute(sql, function(rows) {
  28. for (var i = 0; i < rows.length; i++) {
  29. __self.command_list.push(rows[i].command);
  30. }
  31. });
  32. };
  33. Commands.prototype.commands = function(data) {
  34. var __self = this;
  35. if (typeof data[3] === 'undefined') return;
  36. var command_check = data[3].slice(1).charAt(0),
  37. command = data[3].slice(2);
  38. // check if potential command was called and match it with stored commands
  39. // if the bots name is used as a command, then display all of the available commands
  40. if(command_check === '!' && __self.command_list.indexOf(command) >= 0) {
  41. var sql = 'SELECT * FROM commands WHERE command = \'' + command + '\'';
  42. // get command info from database
  43. __self.db.execute(sql, function(rows) {
  44. // filter through command results
  45. for (var i = 0; i < rows.length; i++) {
  46. // match db command with called command
  47. if (rows[i].command = command) {
  48. /*
  49. // display based on viewer auth
  50. if (rows[i].auth === 1) {
  51. __self.irc.emit('message',{message:'> '+rows[i].text, options:{caller: __self.irc.caller(data[0]), auth: 1}});
  52. break;
  53. } else if (rows[i].auth === 0) {
  54. __self.irc.emit('message',{message:'> '+rows[i].text, options:null});
  55. break;
  56. }
  57. */
  58. __self.irc.emit('message',{message:'> '+rows[i].text, options:null});
  59. }
  60. }
  61. });
  62. } else if (command_check === '!' && command === __self.config.bot_name.toLowerCase()) {
  63. moderator_initiated = __self.irc.mods.indexOf(__self.irc.caller(data[0])) >= 0;
  64. console.log(__self.irc.mods)
  65. var commands = '> Commands: !' + __self.config.currency.toLowerCase() + ', !top, !rank, ';
  66. if(Object.keys(__self.config.exchange).length != 0) commands += '!exchange, ';
  67. if(moderator_initiated) {
  68. for (var i = 0; i < __self.command_list.length; i++) {
  69. if (i !== __self.command_list.length - 1) {
  70. commands += '!' + __self.command_list[i] + ', ';
  71. } else {
  72. commands += '!' + __self.command_list[i];
  73. }
  74. }
  75. __self.irc.emit('message',{message:commands, options:null});
  76. }
  77. }
  78. };
  79. Commands.prototype.add = function(command, text) {
  80. var __self = this;
  81. // escape string for sql sanity
  82. var txt = text.replace(/[\0\x08\x09\x1a\n\r"'\\\%]/g, function(char) {
  83. switch (char) {
  84. case "\0":
  85. return "\\0";
  86. case "\x08":
  87. return "\\b";
  88. case "\x09":
  89. return "\\t";
  90. case "\x1a":
  91. return "\\z";
  92. case "\n":
  93. return "\\n";
  94. case "\r":
  95. return "\\r";
  96. case "\"":
  97. case "'":
  98. case "\\":
  99. case "%":
  100. return "\\"+char;
  101. }
  102. });
  103. var sql = 'INSERT INTO commands (command, text) VALUES (\''+command+'\', \''+txt+'\');';
  104. // add the command to the database
  105. __self.db.execute(sql, function(data) {
  106. var message = '> Add Command: !' + command + ' has been added.';
  107. __self.command_list.push(command);
  108. __self.irc.emit('message',{message:message, options:null});
  109. });
  110. };
  111. Commands.prototype.remove = function(command) {
  112. var __self = this,
  113. index = __self.command_list.indexOf(command);
  114. if( index >= 0) {
  115. var sql = 'DELETE FROM commands WHERE command=\''+command+'\';';
  116. // remove the command from the database
  117. __self.db.execute(sql, function(data) {
  118. var message = '> Delete Command: !' + command + ' has been removed.';
  119. __self.command_list.splice(index, 1);
  120. __self.irc.emit('message',{message:message, options:null});
  121. });
  122. }
  123. };
  124. module.exports = function(irc, db, options) {
  125. return new Commands(irc, db, options);
  126. };