Advertisement
Guest User

Untitled

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