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.

79 lines
2.7 KiB

11 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. // display based on viewer auth
  47. if (rows[i].auth === 1) {
  48. __self.irc.emit('message',{message:rows[i].text, options:{caller: __self.irc.caller(data[0]), auth: 1}});
  49. break;
  50. } else if (rows[i].auth === 0) {
  51. __self.irc.emit('message',{message:rows[i].text, options:null});
  52. break;
  53. }
  54. }
  55. }
  56. });
  57. } else if (command_check === '!' && command === __self.config.bot_name.toLowerCase()) {
  58. var commands = '> Commands: !' + __self.config.currency.toLowerCase() + ', ';
  59. for (var i = 0; i < __self.command_list.length; i++) {
  60. if (i !== __self.command_list.length - 1) {
  61. commands += '!' + __self.command_list[i] + ', ';
  62. } else {
  63. commands += '!' + __self.command_list[i];
  64. }
  65. }
  66. __self.irc.emit('message',{message:commands, options:{caller: __self.irc.caller(data[0]), auth: 1}});
  67. }
  68. };
  69. module.exports = function(irc, db, options) {
  70. return new Commands(irc, db, options);
  71. };