forked from julianwachholz/liarsdice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
99 lines (84 loc) · 2.5 KB
/
bot.js
File metadata and controls
99 lines (84 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env node
/*
* Bot.js loads the game module and starts the IRC bot
*/
var irc = require('irc'),
config = require('./config'),
lang = require('./lang')[config.lang],
game = require('./liarsdice'),
mongo = require('mongodb'),
bot;
// Initializes the bot
bot = new irc.Client(config.server, config.nick, {
port: config.port || 6667,
channels: [config.channel],
userName: config.user || config.nick,
realName: config.real || config.nick,
autoConnect: false
});
// Set callback functions
game.set_announce(function(str) {
bot.say(config.channel, str);
});
game.set_tell(function(nick, str) {
bot.notice(nick, str);
});
// Player has a new nick
bot.addListener('nick', function(oldnick, newnick) {
game.player_rename(oldnick, newnick);
});
// Player quits
bot.addListener('part' + config.channel, function(nick) {
game.player_quit(nick);
});
bot.addListener('quit', function(nick) {
game.player_quit(nick);
});
bot.addListener('kick', function(chan, nick) {
if (chan === config.channel) {
game.player_quit(nick);
}
});
// Identify with NickServ
bot.addListener('notice', function(nick, to, text) {
if (nick === 'NickServ' && text.match(/^This nickname is registered\./)) {
console.info('Authenticating with NickServ');
bot.say('NickServ', 'IDENTIFY ' + require('./password').password);
}
});
// Main message listener
bot.addListener('message' + config.channel, function(nick, message) {
var command, match;
// log activity
game.activity();
for (command in lang.command) {
if (lang.command.hasOwnProperty(command)) {
match = message.match(lang.command[command]);
if (!!match) {
game.player_command(nick, command, match.splice(1));
}
}
}
});
// Connect to MongoDB
if (!!config.mongodb) {
new mongo.Db(
config.mongodb.dbname,
new mongo.Server(config.mongodb.server, config.mongodb.port || mongo.Connection.DEFAULT_PORT),
config.mongodb.options)
.open(function(err, db) {
if (!err) {
db.collection('players', function(err, collection) {
console.info('Connected to MongoDB, starting IRC bot');
game.set_stats_collection(collection, function() {
bot.connect();
});
});
} else {
throw err;
}
});
} else {
console.info('Starting IRC bot without statistics');
bot.connect();
}