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.

139 lines
4.6 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
  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. command_check = data[3].slice(1).charAt(0),
  36. command = data[3].slice(2);
  37. // check if potential command was called and match it with stored commands
  38. // if the bots name is used as a command, then display all of the available commands
  39. if(command_check === '!' && __self.command_list.indexOf(command) >= 0) {
  40. var sql = 'SELECT * FROM commands WHERE command = \'' + command + '\'';
  41. // get command info from database
  42. __self.db.execute(sql, function(rows) {
  43. // filter through command results
  44. for (var i = 0; i < rows.length; i++) {
  45. // match db command with called command
  46. if (rows[i].command = command) {
  47. /*
  48. // display based on viewer auth
  49. if (rows[i].auth === 1) {
  50. __self.irc.emit('message',{message:'> '+rows[i].text, options:{caller: __self.irc.caller(data[0]), auth: 1}});
  51. break;
  52. } else if (rows[i].auth === 0) {
  53. __self.irc.emit('message',{message:'> '+rows[i].text, options:null});
  54. break;
  55. }
  56. */
  57. __self.irc.emit('message',{message:'> '+rows[i].text, options:null});
  58. }
  59. }
  60. });
  61. } else if (command_check === '!' && command === __self.config.bot_name.toLowerCase()) {
  62. var commands = '> Commands: !' + __self.config.currency.toLowerCase() + ', !top, !rank, ';
  63. if(Object.keys(__self.config.exchange).length != 0) commands += '!exchange, ';
  64. for (var i = 0; i < __self.command_list.length; i++) {
  65. if (i !== __self.command_list.length - 1) {
  66. commands += '!' + __self.command_list[i] + ', ';
  67. } else {
  68. commands += '!' + __self.command_list[i];
  69. }
  70. }
  71. __self.irc.emit('message',{message:commands, options:null});
  72. }
  73. };
  74. Commands.prototype.add = function(command, text) {
  75. var __self = this;
  76. // escape string for sql sanity
  77. var txt = text.replace(/[\0\x08\x09\x1a\n\r"'\\\%]/g, function(char) {
  78. switch (char) {
  79. case "\0":
  80. return "\\0";
  81. case "\x08":
  82. return "\\b";
  83. case "\x09":
  84. return "\\t";
  85. case "\x1a":
  86. return "\\z";
  87. case "\n":
  88. return "\\n";
  89. case "\r":
  90. return "\\r";
  91. case "\"":
  92. case "'":
  93. case "\\":
  94. case "%":
  95. return "\\"+char;
  96. }
  97. });
  98. var sql = 'INSERT INTO commands (command, text) VALUES (\''+command+'\', \''+txt+'\');';
  99. // add the command to the database
  100. __self.db.execute(sql, function(data) {
  101. var message = '> Add Command: !' + command + ' has been added.';
  102. __self.command_list.push(command);
  103. __self.irc.emit('message',{message:message, options:null});
  104. });
  105. };
  106. Commands.prototype.remove = function(command) {
  107. var __self = this,
  108. index = __self.command_list.indexOf(command);
  109. if( index >= 0) {
  110. var sql = 'DELETE FROM commands WHERE command=\''+command+'\';';
  111. // remove the command from the database
  112. __self.db.execute(sql, function(data) {
  113. var message = '> Delete Command: !' + command + ' has been removed.';
  114. __self.command_list.splice(index, 1);
  115. __self.irc.emit('message',{message:message, options:null});
  116. });
  117. }
  118. };
  119. module.exports = function(irc, db, options) {
  120. return new Commands(irc, db, options);
  121. };