-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
185 lines (156 loc) · 5.32 KB
/
index.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var Botkit = require('botkit');
var os = require('os');
var controller = Botkit.slackbot();
var forcaWordTemp = null
var forcaWord = null
var forcaWordHidden = null
var forcaErrorCount = 0
var correctLetters = []
var wrongLetters = []
var ideas = []
String.prototype.replaceAt=function(index, character) {
return this.substr(0, index) + character + this.substr(index+character.length);
}
var bot = controller.spawn({
token: process.env.slackBotKey
})
bot.startRTM(function(err,bot,payload) {
if (err) {
throw new Error('Could not connect to Slack');
}
});
controller.hears(['store (.*) = (.*)'], ['direct_message,direct_mention,mention', 'ambient'], function(bot, message) {
var matches = message.text.match(/store (.*) = (.*)/i)
controller.hears(matches[1], ['direct_message,direct_mention,mention','ambient'], function(bot, message) {
console.log("ouviu")
bot.reply(message,matches[2])
});
});
controller.hears(['hangman (.*)'], ['direct_message'], function(bot, message) {
var matches = message.text.match(/hangman (.*)/i);
forcaWordTemp = matches[1]
bot.reply(message, "Now write `@" + bot.identity.name + ": start hangman` in a channel")
});
controller.hears(['rules hangman'], ['direct_message,direct_mention,mention','ambient'], function(bot, message) {
bot.reply(message, "Commands: ")
bot.reply(message, "Select a world (only by private message): `hangman *word*`")
bot.reply(message, "Start hangman: `start hangman`")
bot.reply(message, "End hangman: `end hangman`")
bot.reply(message, "Try a word (only one try!): `word *word*`")
bot.reply(message, "Try a letter: `letter *letter*`")
})
controller.hears(['start hangman'], ['direct_message,direct_mention,mention','ambient'], function(bot, message) {
if (forcaWordTemp === null) {
return
}
forcaWord = forcaWordTemp
forcaWordTemp = null
forcaWordHidden = '-'.repeat(forcaWord.length)
bot.reply(message, "The hangman word is: " + forcafy(forcaWordHidden))
bot.reply(message, "Read the rules typing `rules hangman`")
forcaErrorCount = 0
correctLetters = []
wrongLetters = []
});
controller.hears(['end hangman'], ['direct_message,direct_mention,mention','ambient'], function(bot, message) {
if(forcaWord != null || forcaWordTemp != null) {
bot.reply(message, "Ok!")
}
forcaWord = null
forcaWordTemp = null
});
function forcafy (word) {
return word.split('').join(' ')
}
function wrongLetterMessageForca () {
var messages = ["Head", "Body", "Right arm", "Left arm",
"Right leg", "Left leg", "You died :(. Better luck next time."]
return messages[forcaErrorCount - 1]
}
controller.hears(['word (.*)'], ['direct_message,direct_mention,mention','ambient'], function(bot, message) {
if (forcaWord === null) {
return
}
var word = message.text.match(/word (.*)/i)[1];
if (forcaWord == word) {
bot.reply(message, "You won! The word is " + forcaWord)
} else {
bot.reply(message, "You died :(. Better luck next time.")
bot.reply(message, "The word was " + forcaWord)
}
forcaWord = null
return
})
controller.hears(['letter (.)'], ['direct_message,direct_mention,mention','ambient'], function(bot, message) {
if (forcaWord === null) {
return
}
var letter = message.text.match(/letter (.)/i)[1];
if (correctLetters.indexOf(letter) != -1 || wrongLetters.indexOf(letter) != -1) {
bot.reply(message, "Letter already used, try another")
return
}
var gotItRight = false
for (var i = 0; i < forcaWord.length; i++) {
if (letter === forcaWord[i]) {
gotItRight = true
forcaWordHidden = forcaWordHidden.replaceAt(i, letter)
}
}
if (forcaWordHidden == forcaWord) {
bot.reply(message, "You won! The word is " + forcaWord)
forcaWord = null
return
} else {
if (gotItRight) {
correctLetters.push(letter)
bot.reply(message, forcafy(forcaWordHidden))
} else {
forcaErrorCount++
wrongLetters.push(letter)
bot.reply(message, wrongLetterMessageForca())
if (forcaErrorCount == 7) {
bot.reply(message, "The word was " + forcaWord)
forcaWord = null
return
}
}
}
bot.reply(message, "Correct letters: " + correctLetters)
bot.reply(message, "Incorrect letters: " + wrongLetters)
});
controller.hears(['Save text'], ['direct_message,direct_mention'], function(bot, message) {
bot.startConversation(message, function(err,convo) {
var a = new Object()
convo.ask('What is the text title?', function(response,convo) {
a.ideaTitle = response.text
convo.next();
});
convo.ask("Great! Now what's the text?", function(response,convo) {
a.ideaText = response.text
convo.next();
});
convo.say('Cool! Text saved')
ideas.push(a)
})
})
controller.hears(['List texts'], ['direct_message,direct_mention'], function(bot, message) {
bot.startConversation(message, function(err,convo) {
convo.say('The texts are:')
for (var i = 0; i < ideas.length; i++) {
convo.say(i + 1 + ' - ' + ideas[i].ideaTitle)
}
var num = 0
convo.ask("Type the number of the text you want to see", function(response,convo) {
num = parseInt(response.text)
num--;
if (num >= ideas.length || num < 0) {
convo.say('Invalid number')
return
}
convo.say('The text is:')
convo.say(ideas[num].ideaText)
convo.next();
});
})
})