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.

128 lines
4.0 KiB

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. __self.irc.emit('message',{message:'> '+rows[i].text, options:null});
  47. }
  48. }
  49. });
  50. } else if (command_check === '!' && command === __self.config.bot_name.toLowerCase()) {
  51. var commands = '> Commands: !' + __self.config.currency.toLowerCase() + ', !top, !rank, ';
  52. for (var i = 0; i < __self.command_list.length; i++) {
  53. if (i !== __self.command_list.length - 1) {
  54. commands += '!' + __self.command_list[i] + ', ';
  55. } else {
  56. commands += '!' + __self.command_list[i];
  57. }
  58. }
  59. __self.irc.emit('message',{message:commands, options:null});
  60. }
  61. };
  62. Commands.prototype.add = function(command, text) {
  63. var __self = this;
  64. // escape string for sql sanity
  65. var txt = text.replace(/[\0\x08\x09\x1a\n\r"'\\\%]/g, function(char) {
  66. switch (char) {
  67. case "\0":
  68. return "\\0";
  69. case "\x08":
  70. return "\\b";
  71. case "\x09":
  72. return "\\t";
  73. case "\x1a":
  74. return "\\z";
  75. case "\n":
  76. return "\\n";
  77. case "\r":
  78. return "\\r";
  79. case "\"":
  80. case "'":
  81. case "\\":
  82. case "%":
  83. case ";":
  84. return "\\"+char;
  85. }
  86. });
  87. var sql = 'INSERT INTO commands (command, text) VALUES (\''+command+'\', \''+txt+'\');';
  88. // add the command to the database
  89. __self.db.execute(sql, function(data) {
  90. var message = '> Add Command: !' + command + ' has been added.';
  91. __self.command_list.push(command);
  92. __self.irc.emit('message',{message:message, options:null});
  93. });
  94. };
  95. Commands.prototype.remove = function(command) {
  96. var __self = this,
  97. index = __self.command_list.indexOf(command);
  98. if( index >= 0) {
  99. var sql = 'DELETE FROM commands WHERE command=\''+command+'\';';
  100. // remove the command from the database
  101. __self.db.execute(sql, function(data) {
  102. var message = '> Delete Command: !' + command + ' has been removed.';
  103. __self.command_list.splice(index, 1);
  104. __self.irc.emit('message',{message:message, options:null});
  105. });
  106. }
  107. };
  108. module.exports = function(irc, db, options) {
  109. return new Commands(irc, db, options);
  110. };