-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangbot.py
329 lines (292 loc) · 11.1 KB
/
Hangbot.py
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import logging
import os
import string
import time
import tweepy
import random
import _pickle as cPickle
import atexit
from config import create_api
class Hangman:
def __init__(self):
self.wordbank = ['Aruba', 'Afghanistan', 'Angola', 'Anguilla', 'Aland Islands', 'Albania', 'Algeria', 'Andorra',
'Argentina', 'Armenia', 'Samoa', 'Antarctica', 'Antigua and Barbuda', 'Australia', 'Austria',
'Azerbaijan',
'Burundi', 'Belgium', 'Bonaire', 'Burkina Faso', 'Bangladesh', 'Bulgaria', 'Bahrain',
'Bahamas', 'Bosnia and Herzegovina', 'Belarus', 'Belize', 'Bermuda', 'Bolivia', 'Brazil',
'Barbados', 'Botswana',
'Canada', 'Cocos Islands', 'Chile', 'China', 'Cameroon', 'Congo', 'Cook Islands', 'Colombia',
'Comoros', 'Cabo Verde', 'Costa Rica', 'Cuba', 'Cayman Islands', 'Cyprus', 'Czechia',
'Croatia', 'Cambodia', 'Chad',
'Djibouti', 'Dominica', 'Denmark',
'Ecuador', 'Egypt', 'Eritrea', 'Estonia', 'Ethiopia',
'Spain', 'Sri Lanka', 'Saint Lucia', 'Switzerland',
'Finland', 'Fiji', 'Falkland Islands ', 'France',
'Gabon', 'Germany', 'Georgia', 'Ghana', 'Gibraltar', 'Guinea', 'Guadeloupe', 'Gambia',
'Greece', 'Grenada', 'Greenland', 'Guatemala',
'United Kingdom', 'United Arab Emirates',
'Hong Kong', 'Honduras', 'Haiti', 'Hungary',
'Indonesia', 'Isle of Man', 'India', 'Ireland', 'Iran', 'Iraq', 'Iceland', 'Italy',
'Jamaica', 'Jersey', 'Jordan', 'Japan',
'Kazakhstan', 'Kenya', 'Kyrgyzstan', 'Kuwait',
'Lebanon', 'Liberia', 'Libya',
'Lesotho', 'Lithuania', 'Luxembourg', 'Latvia',
'Macao', 'Morocco', 'Monaco', 'Madagascar', 'Maldives', 'Mexico', 'Mali', 'Malta', 'Myanmar',
'Mongolia',
'Mozambique', 'Mauritania', 'Mauritius', 'Malawi', 'Malaysia',
'Namibia', 'Niger', 'Nigeria', 'Netherlands', 'Norway', 'Nepal', 'Nauru', 'New Zealand',
'Oman',
'Pakistan', 'Panama', 'Peru', 'Philippines', 'Papua New Guinea', 'Poland', 'Puerto Rico',
'Portugal', 'Paraguay', 'Palestine',
'Qatar',
'Romania', 'Russia', 'Rwanda',
'Saudi Arabia', 'Sudan', 'Senegal', 'Singapore', 'Solomon Islands', 'Sierra Leone',
'El Salvador',
'San Marino', 'Somalia', 'Serbia', 'Slovakia', 'Slovenia', 'Sweden', 'Seychelles', 'Syrian',
'South Africa', 'Samoa',
'Togo', 'Thailand', 'Tonga', 'Trinidad and Tobago', 'Tunisia', 'Turkey', 'Taiwan', 'Tanzania',
'Uganda', 'Ukraine', 'Uruguay', 'United States', 'Uzbekistan', 'Vatican City', 'Venezuela',
'Vietnam', 'Yemen',
'Zambia', 'Zimbabwe']
self.puzzle = ""
self.setPuzzle()
self.blank = ""
self.setBlank()
self.answer = ""
self.strikes = 0
self.won = False
self.lost = False
self.used = []
self.hangmanart = [
'''
+ - - - - +
| |
|
|
|
|
=========
''',
'''
+ - - - - +
| |
O |
|
|
|
=========
''', '''
+ - - - - +
| |
O |
| |
|
|
=========
''', '''
+ - - - - +
| |
O |
/| |
|
|
=========
''', '''
+ - - - - +
| |
O |
/|\ |
|
|
=========
''', '''
+ - - - - +
| |
O |
/|\ |
/ |
|
=========
''', '''
+ - - - - +
| |
O |
/|\ |
/ \ |
|
=========
''']
def hasLost(self):
if self.strikes > 7:
self.lost = True
def setPuzzle(self):
rand = random.randint(0, len(self.wordbank) - 1)
self.puzzle = self.wordbank[rand].lower().replace(" ", "\n")
def setBlank(self):
blank = ""
for l in self.puzzle:
if l != "\n":
blank = blank + "_ "
else:
blank = blank + "\n"
self.blank = blank.lstrip().rstrip()
def getBlank(self):
return self.blank
def isDone(self):
if self.answer == self.puzzle:
return True
else:
return False
def Check(self, letter):
if letter not in self.used:
if letter in self.puzzle:
puzLis = list(self.puzzle)
blanLis = list(self.blank.replace(" ", ""))
for x in range(0, len(puzLis)):
if puzLis[x] == letter:
blanLis[x] = letter
b = ""
for x in blanLis:
if x != " ":
b += x
else:
b += " "
self.blank = b.rstrip().lstrip()
self.answer = self.blank.replace("_", "")
if self.isDone():
self.won = True
return "You Win!" + "\n" + "Final Answer: " + self.puzzle.upper().replace("\n", " ")
if self.strikes > 0:
return self.hangmanart[self.strikes - 1] + "\n" + "-".join(self.used) + '\n\n' + self.blank.upper()
else:
return self.blank.upper()
else:
self.used.append(letter.upper())
if self.strikes < 7:
self.strikes += 1
if self.strikes == 7:
self.lost = True
return self.hangmanart[
self.strikes - 1] + "\n" + "You Lose!" + "\n" + "Final Answer: " + self.puzzle.upper().replace(
"\n", " ")
if not self.lost:
return self.hangmanart[self.strikes - 1] + "-".join(self.used) + '\n\n' + self.blank.upper()
class Games:
def __init__(self, t_id):
self.Game = Hangman()
self.tweet_id = t_id
self.thread = [t_id]
self.user = t_id
def setTweet(self, tweet):
self.tweet_id = tweet
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
repliedMessages = set()
rGamesMessages = {}
repliedMentions = set()
rGamesMentions = {}
def storeData(i, saveFile):
dbFile = open(saveFile, 'wb')
cPickle.dump(i, dbFile)
dbFile.close()
def loadData(loadFile):
dbFile = open(loadFile, 'rb')
db = cPickle.load(dbFile)
dbFile.close()
return db
def check_mentions(api, keywords, since_id):
global repliedMentions, rGamesMentions
logger.info("Retrieving mentions")
new_since_id = since_id
mentions = list(tweepy.Cursor(api.mentions_timeline, since_id=since_id).items())
for tweet in mentions:
new_since_id = max(tweet.id, new_since_id)
if tweet.in_reply_to_status_id is not None:
if any(keyword in tweet.text.lower() for keyword in keywords):
logger.info(f"Answering to {tweet.user.name}")
g = rGamesMentions.get(tweet.user.id)
if g is not None:
g.setTweet(tweet.id)
thisGame = g.Game
t = tweet.text.split(" ")
txt = t[-1]
if not thisGame.lost and not thisGame.won:
chk = thisGame.Check(txt.lower())
api.update_status(
status=chk,
in_reply_to_status_id=g.tweet_id,
auto_populate_reply_metadata=True)
else:
rGamesMentions.pop(tweet.user.id)
del g
else:
repliedMentions.add(tweet.id)
game = Games(tweet.id)
rGamesMentions[tweet.user.id] = game
api.update_status(
status=game.Game.blank,
in_reply_to_status_id=game.tweet_id,
auto_populate_reply_metadata=True
)
repliedMentions.clear()
repliedMentions.add(tweet.id)
return new_since_id
def check_DMs(api, keywords):
global repliedMessages, rGamesMessages
me = int(api.me().id)
logger.info("Retrieving DMs")
messages = api.list_direct_messages()[::-1]
recMessages = set([mes.id for mes in messages if int(mes.message_create['target']['recipient_id']) == me])
newMessages = recMessages - repliedMessages
repliedMessages = recMessages.intersection(repliedMessages)
for i in newMessages:
m = api.get_direct_message(i)
txt = m.message_create['message_data']['text'].lower()
user = int(m.message_create['sender_id'])
name = api.get_user(user).name
logger.info(f"Messaging to {name}")
if txt == "New Game".lower():
game = Games(user)
rGamesMessages[user] = game
api.send_direct_message(user, game.Game.blank)
if txt in keywords:
g = rGamesMessages.get(user)
if g is not None:
thisGame = g.Game
if not thisGame.lost and not thisGame.won:
chk = thisGame.Check(txt)
api.send_direct_message(user, chk)
else:
rGamesMessages.pop(user)
del g
repliedMessages.add(m.id)
def main():
global rGamesMentions, repliedMentions, rGamesMessages, repliedMessages
api = create_api()
if os.path.getsize('repliedMentions') > 0:
repliedMentions = set(loadData('repliedMentions'))
if os.path.getsize('rGamesMentions') > 0:
rGamesMentions = loadData('rGamesMentions')
if os.path.getsize('repliedMessages') > 0:
repliedMessages = set(loadData('repliedMessages'))
if os.path.getsize('rGamesMessages') > 0:
rGamesMessages = loadData('rGamesMessages')
since_id = max(repliedMentions)
while True:
check_DMs(api, list(string.ascii_lowercase))
for x in range(5):
since_id = check_mentions(api, list(string.ascii_lowercase), since_id)
time.sleep(12)
def exit_handler():
logger.info("SAVING DATA")
if len(repliedMentions) != 0:
storeData(repliedMentions, 'repliedMentions')
if len(rGamesMentions) != 0:
storeData(rGamesMentions, 'rGamesMentions')
if len(repliedMessages) != 0:
storeData(repliedMessages, 'repliedMessages')
if len(rGamesMessages) != 0:
storeData(rGamesMessages, 'rGamesMessages')
if __name__ == "__main__":
atexit.register(exit_handler)
main()