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.

138 lines
4.5 KiB

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