Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var irc = require('irc');
- var fs = require('fs');
- //Server, Nick, Password, Channel
- var info = ['chat.freenode.net', 'GiraffeBot', 'giraffe', '#GiraffeBot'];
- var commands = ['!help', '!create', '!join', '!leave', '!done', '!end', '!rooms', '!start', '!ready', '!unready'];
- var rooms = [];
- var games = [];
- var index = 0;
- var client = new irc.Client(info[0], info[1], {
- autoConnnect: false,
- userName: info[1]
- });
- //TODO: fix this up... somehow; set the index to be what it was when the bot last shutdown.
- (function() {
- readIndex(function(data) {
- index = parseInt(data.toString());
- if(typeof index !== 'undefined') {
- console.log('Index is ' + index);
- index = parseInt(data.toString()) + 1;
- }
- else {
- console.log('Index not found, assuming it is 0');
- index = 0;
- }
- });
- })();var irc = require('irc');
- var fs = require('fs');
- //Server, Nick, Password, Channel
- var info = ['chat.freenode.net', 'GiraffeBot', 'giraffe', '#GiraffeBot'];
- var commands = ['!help', '!create', '!join', '!leave', '!done', '!end', '!rooms', '!start', '!ready', '!unready'];
- var rooms = [];
- var games = [];
- var index = 0;
- var client = new irc.Client(info[0], info[1], {
- autoConnnect: false,
- userName: info[1]
- });
- //TODO: fix this up... somehow; set the index to be what it was when the bot last shutdown.
- (function() {
- readIndex(function(data) {
- index = parseInt(data.toString());
- if(typeof index !== 'undefined') {
- console.log('Index is ' + index);
- index = parseInt(data.toString()) + 1;
- }
- else {
- console.log('Index not found, assuming it is 0');
- index = 0;
- }
- });
- })();
- client.connect(function() {
- console.log('Connected!');
- console.log('Logging in...');
- setTimeout(function() {
- client.join(info[3], function() {
- console.log('Joined main channel!');
- });
- }, 2000);
- });
- client.addListener('error', function(message) {
- console.log('error: ', message);
- });
- function saveIndex() {
- var buffer = new Buffer(index.toString());
- var path = "public/index.txt";
- fs.open(path, 'w', function(err, fd) {
- if (err) {
- throw 'error opening file: ' + err;
- }
- fs.write(fd, buffer, 0, buffer.length, null, function(err) {
- if (err) throw 'error writing file: ' + err;
- fs.close(fd, function() {
- console.log('Saved Index');
- })
- });
- });
- }
- function readIndex(callback) {
- var path = "public/index.txt";
- var content;
- fs.readFile(path, function read(err, data) {
- if (err) {
- throw err;
- return 'undefined';
- }
- content = data;
- if(typeof parseInt(content.toString()) !== 'NaN'){
- callback(data);
- } else {
- console.log('Please add a 0 to index.txt in /public/');
- }
- });
- }
- client.addListener('message', function (from, to, message) {
- var owner;;
- console.log(from + ' => ' + to + ': ' + message);
- //Create command; see if it starts with the command; see if its two words
- var splitMessage = message.split(" ");
- var messageLength = commands[1];
- if(to == info[3] && message.substring(0, messageLength.length) == messageLength && splitMessage.length == 2) {
- console.log(from + ' has created a lobby!');
- var owner = from;
- var room = { name: '#GiraffeBot-' + index,
- roomGame: game };
- var game = splitMessage[1];
- console.log('New room is titled: ' + room[game]);
- rooms.push(room);
- client.join(room.name);
- client.say(room.name, '/topic ' + owner + "'s " + game + ' lobby! Type !join to participate');
- 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');
- saveIndex();
- index++;
- //If the message isn't a different command!
- } else if(splitMessage.indexOf(message) == -1){
- client.say(info[3], owner + ': You messed up! Either you had too many arguments or used the command incorrectly. Correct usage: !create <game>');
- }
- });
- client.connect(function() {
- console.log('Connected!');
- console.log('Logging in...');
- setTimeout(function() {
- client.join(info[3], function() {
- console.log('Joined main channel!');
- });
- }, 2000);
- });
- client.addListener('error', function(message) {
- console.log('error: ', message);
- });
- function saveIndex() {
- var buffer = new Buffer(index.toString());
- var path = "public/index.txt";
- fs.open(path, 'w', function(err, fd) {
- if (err) {
- throw 'error opening file: ' + err;
- }
- fs.write(fd, buffer, 0, buffer.length, null, function(err) {
- if (err) throw 'error writing file: ' + err;
- fs.close(fd, function() {
- console.log('Saved Index');
- })
- });
- });
- }
- function readIndex(callback) {
- var path = "public/index.txt";
- var content;
- fs.readFile(path, function read(err, data) {
- if (err) {
- throw err;
- return 'undefined';
- }
- content = data;
- if(typeof parseInt(content.toString()) !== 'NaN'){
- callback(data);
- } else {
- console.log('Please add a 0 to index.txt in /public/');
- }
- });
- }
- client.addListener('message', function (from, to, message) {
- var owner;;
- console.log(from + ' => ' + to + ': ' + message);
- //Create command; see if it starts with the command; see if its two words
- var splitMessage = message.split(" ");
- var messageLength = commands[1];
- if(to == info[3] && message.substring(0, messageLength.length) == messageLength && splitMessage.length == 2) {
- console.log(from + ' has created a lobby!');
- var owner = from;
- var room = { name: '#GiraffeBot-' + index,
- roomGame: game };
- var game = splitMessage[1];
- console.log('New room is titled: ' + room[game]);
- rooms.push(room);
- client.join(room.name);
- client.say(room.name, '/topic ' + owner + "'s " + game + ' lobby! Type !join to participate');
- 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');
- saveIndex();
- index++;
- //If the message isn't a different command!
- } else if(splitMessage.indexOf(message) == -1){
- client.say(info[3], owner + ': You messed up! Either you had too many arguments or used the command incorrectly. Correct usage: !create <game>');
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement