Advertisement
Guest User

Untitled

a guest
Dec 20th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. //Require libraries
  2. var tmi = require('tmi.js');
  3. var mysql = require('mysql');
  4.  
  5. //Configure chat connection
  6. var options = {
  7. options: {
  8. debug: true
  9. },
  10. connection: {
  11. cluster: "aws",
  12. reconnect: true
  13. },
  14. identity: {
  15. username: "ShadowoBot27",
  16. password: "oauth:3qanta6l9vu80wl0x502ytgcxljg9x"
  17. },
  18. channels: ["ShadowMario27"]
  19. };
  20.  
  21. //Configure MySql connection
  22. var con = mysql.createConnection({
  23. host: "localhost",
  24. user: "shadowobot27",
  25. password: "454pildl380z",
  26. database: "shadowobot"
  27. });
  28.  
  29. //Connect to chat
  30. var client = new tmi.client(options);
  31. client.connect();
  32.  
  33. //Connect to MySql
  34. con.connect(function(err) {
  35. if (err) throw err;
  36. console.log("Connected to MySql");
  37. });
  38.  
  39.  
  40. //Handle chat event
  41. client.on('chat', function(channel, userstate, message, self) {
  42.  
  43. //Don't give a shit about own messages
  44. if (self) {
  45. return;
  46. }
  47.  
  48. //Set user info variables
  49. var user = userstate["username"];
  50. var isMod = userstate["mod"];
  51. var userDisplayName = userstate["display-name"];
  52.  
  53. //The most important command
  54. if (message.startsWith("gl")) {
  55. client.say("ShadowMario27", "thankS");
  56. return;
  57. }
  58.  
  59. //Lock Samuel from editing !sam
  60. if (message.startsWith("!editcom sam") && user == "samgodro43") {
  61. client.say("ShadowMario27", "Sorry, Samuel is unable to edit his own command. TPFufun");
  62. return;
  63. }
  64.  
  65.  
  66. //Handle command adding
  67. if (message.startsWith("!addcom") && (isMod || user == "shadowmario27" || user == "challenger_egirl" )) {
  68.  
  69. var errorResponse = "@" + userDisplayName + ", that is an invalid format. Please add command in the format: \"!addcom name text\".";
  70.  
  71. //Get string index of the command name
  72. var a_commandIndex = message.indexOf(" ") + 1;
  73. //Respond with error if there is no command name
  74. if (a_commandIndex == 0) {
  75. client.say("ShadowMario27", errorResponse);
  76. return;
  77. }
  78.  
  79. //Get string index of the command text
  80. var a_textIndex = message.substring(a_commandIndex).indexOf(" ") + a_commandIndex + 1;
  81. //Respond with error if there is no command text
  82. if (a_textIndex == a_commandIndex) {
  83. client.say("ShadowMario27", errorResponse);
  84. return;
  85. }
  86.  
  87. //Get command name and command text
  88. var a_commandName = message.substring(a_commandIndex, a_textIndex - 1);
  89. var a_commandText = message.substring(a_textIndex);
  90.  
  91. //Escape single quote character so MySql query works
  92. a_commandText = a_commandText.replace("'", "''");
  93.  
  94.  
  95. //Attempt to insert new command using stored procedure
  96. con.query("call AddCommand('" + a_commandName + "', '" + a_commandText + "')", function (err, result, fields) {
  97. if (err) throw err;
  98.  
  99. if (result[0][0].response == "a_success") {
  100. //Procedure was able to add command successfully
  101. client.say("ShadowMario27", "@" + userDisplayName + ", command \"!" + a_commandName + "\" has been added.");
  102. }
  103. else {
  104. //Command already existed
  105. client.say("ShadowMario27", "@" + userDisplayName + ", command \"!" + a_commandName + "\" already exists.");
  106. }
  107. });
  108.  
  109. }
  110.  
  111.  
  112.  
  113.  
  114. //Handle general text commands
  115. if (message.charAt(0) == "!") {
  116.  
  117. var g_commandName = "";
  118.  
  119. //If there is space after the command, only grab the command part of the message
  120. if (message.indexOf(" ") > -1) {
  121. g_commandName = message.substring(1, message.indexOf(" "));
  122. }
  123. else {
  124. g_commandName = message.substring(1);
  125. }
  126.  
  127. //Query database for command, respond if it is found
  128. con.query("select command_text from command where command_name = '" + g_commandName + "'", function (err, result, fields) {
  129. if (err) throw err;
  130.  
  131. if (!(result[0] == null)) {
  132. client.say("ShadowMario27", result[0].command_text);
  133. }
  134. else {
  135. return;
  136. }
  137. });
  138.  
  139. }
  140.  
  141. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement