-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.js
160 lines (141 loc) · 4.61 KB
/
robot.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var Botkit = require('botkit');
var mixin = require('mixin-object');
var store = require('./lib/models');
//missions
var StandardMission = require('./lib/missions/standard');
var MatchTheGiphyMission = require('./lib/missions/giphy_match');
//environment
var BOT_MASTER = process.env.BOT_MASTER;
var SLACK_TOKEN = process.env.SLACK_TOKEN;
var BOT_DEBUG = (process.env.BOT_DEBUG==='true');
var GLOBAL_THRESHOLD = process.env.GLOBAL_THRESHOLD;
if( !SLACK_TOKEN ){
console.log('Error: Specify SLACK_TOKEN in environment');
process.exit(1);
}
var controller = Botkit.slackbot({
debug: BOT_DEBUG
});
// connect the bot to a stream of messages
controller.spawn({token: SLACK_TOKEN,}).startRTM(
function (err, bot) {
if (err) {
throw new Error(err);
}
}
);
// Settings
var SettingsController = require('./lib/controllers/settings');
controller.hears('^\s*settings( game [0-9]+)?(.*=.*)*$',['direct_message', 'direct_mention'],secure(SettingsController));
// Leaderboard
var LeaderboardController = require('./lib/controllers/leaderboard');
controller.hears('^\s*leaderboard\s*$',['direct_mention'],LeaderboardController);
// Track latest giphy and record stats
controller.hears('\/giphy (.*)',['message_received', 'direct_message', 'mention', 'ambient'],onGiphy);
// Current challenge
controller.hears('^\s*show challenge\s*$',['direct_mention'],onShowChallenge);
controller.hears('^\s*reset challenge\s*$',['direct_mention'],onResetChallenge);
// End current game
var EndController = require('./lib/controllers/end');
controller.hears('^\s*end game\s*$',['direct_mention'],secure(EndController));
function secure(handler) {
return function(bot,message){
if( message.user === BOT_MASTER ){
handler.call(this, bot, message);
}
else{
bot.reply(message, 'You are not my master. I do what I want!');
}
}
}
//handlers
function onGiphy(bot,message) {
console.log("Got a giphy...", message);
store.Game.forChannel(message.channel).then(
function(game){
//start or get current game
if(game.isNew()){
bot.reply(message,"Game is on baby!");
console.log("Starting game...", game);
game.start();
game.save();
//start missions
StandardMission.for(game).start(bot.reply.bind(bot, message));
MatchTheGiphyMission.for(game).start(bot.reply.bind(bot, message));
}
console.log("Playing game " + game.get('id'));
return game;
}
).then(
function(game){
return store.Player.getOrCreate(message.user).then(
function(player){
//get and add player to game if necessary
return player.inGame(game).then(
function(inGame){
if(!inGame){
game.addPlayer(player);
bot.reply(message,`Welcome to the game <@${player.messaging_id}>!`);
player.save();
}
return [game, player];
}
);
}
)
}
).then(
function([game, player]){
return store.GiphyPost.new(game, player, message).then(
function(post){
return [game, player, post]
}
);
}
).then(
function([game, player, post]){
//process missions
StandardMission.for(game).onPost(post, bot.reply.bind(bot, message));
MatchTheGiphyMission.for(game).onPost(post, bot.reply.bind(bot, message));
}
).catch(function(err){
console.error("Not able to process giphy,", err, message);
});
}
function onShowChallenge(bot,message) {
console.log("Showing challenge", message);
store.Game.forChannel(message.channel, true).then(
function(game){
if(!game){
bot.reply(message,"No active game. Start one by posting a giphy!");
return;
}
MatchTheGiphyMission.for(game).show(bot.reply.bind(bot, message));
}
);
}
function onResetChallenge(bot,message) {
console.log("Reset challenge", message);
store.Game.forChannel(message.channel, true).then(
function(game){
if(!game){
bot.reply(message,"No active game. Start one by posting a giphy!");
return;
}
MatchTheGiphyMission.for(game).reset(bot.reply.bind(bot, message));
}
);
}
//messages
function toChannelLevelUpMessage(aChanStats){
return `*Level Up!*
This channel is now at level *${aChanStats.level}*
Currently producing giphys at *${Math.round(aChanStats.rate() * 100) / 100} gpm*
`
}
function toUserLevelUpMessage(user, aUserStats){
return `*Level Up!*
<@${user.id}> is now at level *${aUserStats.level}*
Currently producing giphys at *${Math.round(aUserStats.rate() * 100) / 100} gpm*
`
}