Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. var irc = require('irc');
  2. var fs = require('fs');
  3.  
  4. //Server, Nick, Password, Channel
  5. var info = ['chat.freenode.net', 'GiraffeBot', 'giraffe', '#GiraffeBot'];
  6. var commands = ['!help', '!create', '!join', '!leave', '!done', '!end', '!rooms', '!start', '!ready', '!unready'];
  7. var rooms = [];
  8. var games = [];
  9. var index = 0;
  10.  
  11. var client = new irc.Client(info[0], info[1], {
  12. autoConnnect: false,
  13. userName: info[1]
  14. });
  15.  
  16. //TODO: fix this up... somehow; set the index to be what it was when the bot last shutdown.
  17. (function() {
  18. readIndex(function(data) {
  19. index = parseInt(data.toString());
  20. if(typeof index !== 'undefined') {
  21. console.log('Index is ' + index);
  22. index = parseInt(data.toString()) + 1;
  23. }
  24. else {
  25. console.log('Index not found, assuming it is 0');
  26. index = 0;
  27. }
  28.  
  29. });
  30. })();
  31.  
  32. client.connect(function() {
  33. console.log('Connected!');
  34. console.log('Logging in...');
  35. setTimeout(function() {
  36. client.say('NickServ', 'IDENTIFY ' + info[2]);
  37. client.join(info[3], function() {
  38. console.log('Joined main channel!');
  39. });
  40. }, 2000);
  41. });
  42.  
  43. client.addListener('error', function(message) {
  44. console.log('error: ', message);
  45. });
  46.  
  47. function saveIndex() {
  48. var buffer = new Buffer(index.toString());
  49. var path = "public/index.txt";
  50.  
  51. fs.open(path, 'w', function(err, fd) {
  52. if (err) {
  53. throw 'error opening file: ' + err;
  54. }
  55. fs.write(fd, buffer, 0, buffer.length, null, function(err) {
  56. if (err) throw 'error writing file: ' + err;
  57. fs.close(fd, function() {
  58. console.lvar irc = require('irc');
  59. var fs = require('fs');
  60.  
  61. //Server, Nick, Password, Channel
  62. var info = ['chat.freenode.net', 'GiraffeBot', 'giraffe', '#GiraffeBot'];
  63. var commands = ['!help', '!create', '!join', '!leave', '!done', '!end', '!rooms', '!start', '!ready', '!unready'];
  64. var rooms = [];
  65. var games = [];
  66. var index = 0;
  67.  
  68. var client = new irc.Client(info[0], info[1], {
  69. autoConnnect: false,
  70. userName: info[1]
  71. });
  72.  
  73. //TODO: fix this up... somehow; set the index to be what it was when the bot last shutdown.
  74. (function() {
  75. readIndex(function(data) {
  76. index = parseInt(data.toString());
  77. if(typeof index !== 'undefined') {
  78. console.log('Index is ' + index);
  79. index = parseInt(data.toString()) + 1;
  80. }
  81. else {
  82. console.log('Index not found, assuming it is 0');
  83. index = 0;
  84. }
  85.  
  86. });
  87. })();
  88.  
  89. client.connect(function() {
  90. console.log('Connected!');
  91. console.log('Logging in...');
  92. setTimeout(function() {
  93. client.say('NickServ', 'IDENTIFY ' + info[2]);
  94. client.join(info[3], function() {
  95. console.log('Joined main channel!');
  96. });
  97. }, 2000);
  98. });
  99.  
  100. client.addListener('error', function(message) {
  101. console.log('error: ', message);
  102. });
  103.  
  104. function saveIndex() {
  105. var buffer = new Buffer(index.toString());
  106. var path = "public/index.txt";
  107.  
  108. fs.open(path, 'w', function(err, fd) {
  109. if (err) {
  110. throw 'error opening file: ' + err;
  111. }
  112. fs.write(fd, buffer, 0, buffer.length, null, function(err) {
  113. if (err) throw 'error writing file: ' + err;
  114. fs.close(fd, function() {
  115. console.log('Saved Index');
  116. })
  117. });
  118. });
  119. }
  120.  
  121. function readIndex(callback) {
  122. var path = "public/index.txt";
  123. var content;
  124. fs.readFile(path, function read(err, data) {
  125. if (err) {
  126. throw err;
  127. return 'undefined';
  128. }
  129. content = data;
  130. if(typeof parseInt(content.toString()) !== 'NaN'){
  131. callback(data);
  132. } else {
  133. console.log('Please add a 0 to index.txt in /public/');
  134. }
  135. });
  136. }
  137.  
  138. client.addListener('message', function (from, to, message) {
  139. var owner = '';
  140. console.log(from + ' => ' + to + ': ' + message);
  141. //Create command; see if it starts with the command; see if its two words
  142. var splitMessage = message.split(" ");
  143. var messageLength = commands[1];
  144. if(to == info[3] && message.substring(0, messageLength.length) == messageLength && splitMessage.length == 2) {
  145. console.log(from + ' has created a lobby!');
  146. var owner = from;
  147. var room = { name: '#GiraffeBot-' + index,
  148. roomGame: game };
  149. var game = splitMessage[1];
  150. console.log('New room is titled: ' + room[game]);
  151. rooms.push(room);
  152. client.join(room.name);
  153. client.say(room.name, '/topic ' + owner + "'s " + game + ' lobby! Type !join to participate');
  154. client.say(info[3], owner + ' has created a lobby for ' + game + '! To join the lobby, please go to this room: ' + room.name + ' and type !join');
  155. saveIndex();
  156. index++;
  157. //If the message isn't a different command!
  158. } else if(splitMessage.indexOf(message) == -1){
  159. client.say(info[3], owner + ': You messed up! Either you had too many arguments or used the command incorrectly. Correct usage: !create <game>');
  160. }
  161.  
  162.  
  163. });
  164.  
  165. og('Saved Index');
  166. })
  167. });
  168. });
  169. }
  170.  
  171. function readIndex(callback) {
  172. var path = "public/index.txt";
  173. var content;
  174. fs.readFile(path, function read(err, data) {
  175. if (err) {
  176. throw err;
  177. return 'undefined';
  178. }
  179. content = data;
  180. if(typeof parseInt(content.toString()) !== 'NaN'){
  181. callback(data);
  182. } else {
  183. console.log('Please add a 0 to index.txt in /public/');
  184. }
  185. });
  186. }
  187.  
  188. client.addListener('message', function (from, to, message) {
  189. var owner = '';
  190. console.log(from + ' => ' + to + ': ' + message);
  191. //Create command; see if it starts with the command; see if its two words
  192. var splitMessage = message.split(" ");
  193. var messageLength = commands[1];
  194. if(to == info[3] && message.substring(0, messageLength.length) == messageLength && splitMessage.length == 2) {
  195. console.log(from + ' has created a lobby!');
  196. var owner = from;
  197. var room = { name: '#GiraffeBot-' + index,
  198. roomGame: game };
  199. var game = splitMessage[1];
  200. console.log('New room is titled: ' + room[game]);
  201. rooms.push(room);
  202. client.join(room.name);
  203. client.say(room.name, '/topic ' + owner + "'s " + game + ' lobby! Type !join to participate');
  204. client.say(info[3], owner + ' has created a lobby for ' + game + '! To join the lobby, please go to this room: ' + room.name + ' and type !join');
  205. saveIndex();
  206. index++;
  207. //If the message isn't a different command!
  208. } else if(splitMessage.indexOf(message) == -1){
  209. client.say(info[3], owner + ': You messed up! Either you had too many arguments or used the command incorrectly. Correct usage: !create <game>');
  210. }
  211.  
  212.  
  213. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement