81ddab81 by Barry

Finished refactor and added threading for all calls.

1 parent 4264acb8
No preview for this file type
...@@ -8,6 +8,7 @@ import time ...@@ -8,6 +8,7 @@ import time
8 import re 8 import re
9 import pickle 9 import pickle
10 import logging 10 import logging
11 import thread
11 12
12 import traceback 13 import traceback
13 import sys 14 import sys
...@@ -27,7 +28,7 @@ import sqlite3 ...@@ -27,7 +28,7 @@ import sqlite3
27 from blackjack import Blackjack 28 from blackjack import Blackjack
28 import data 29 import data
29 30
30 VERSION = 2.0 31 VERSION = 2.2
31 32
32 conn = sqlite3.connect('db.sqlite3') 33 conn = sqlite3.connect('db.sqlite3')
33 34
...@@ -37,7 +38,7 @@ muted_until = datetime.datetime.now() ...@@ -37,7 +38,7 @@ muted_until = datetime.datetime.now()
37 38
38 client = discord.Client() 39 client = discord.Client()
39 wolf = {} 40 wolf = {}
40 logging.basicConfig(filename='hellsbot.log',level=logging.WARNING) 41 logging.basicConfig(filename='hellsbot.log', level=logging.WARNING)
41 42
42 registered_commands = {'!help': 'do_help', '!commands': 'do_help', 43 registered_commands = {'!help': 'do_help', '!commands': 'do_help',
43 '!shutup': 'do_shutup', 44 '!shutup': 'do_shutup',
...@@ -70,18 +71,24 @@ registered_commands = {'!help': 'do_help', '!commands': 'do_help', ...@@ -70,18 +71,24 @@ registered_commands = {'!help': 'do_help', '!commands': 'do_help',
70 '!question': 'do_question', 71 '!question': 'do_question',
71 '!addjoke': 'do_addjoke', 72 '!addjoke': 'do_addjoke',
72 '!joke': 'do_joke', 73 '!joke': 'do_joke',
74 '!secret': 'do_secret',
75 '!bemyirlwaifu': 'do_waifu',
76 'HILLARY 2016': 'do_hillary',
77 '!squid': 'do_squid',
78 '!stars': 'do_stars',
73 } 79 }
74 80
81
75 ##################### 82 #####################
76 ## Utility Functions 83 # Utility Functions
77 ##################### 84 #####################
78
79 def log(message): 85 def log(message):
80 try: 86 try:
81 logging.warning("{} - {}".format(datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S'), message)) 87 logging.warning("{} - {}".format(datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S'), message))
82 except: 88 except:
83 pass 89 pass
84 90
91
85 def format_exception(e): 92 def format_exception(e):
86 exception_list = traceback.format_stack() 93 exception_list = traceback.format_stack()
87 exception_list = exception_list[:-2] 94 exception_list = exception_list[:-2]
...@@ -95,12 +102,14 @@ def format_exception(e): ...@@ -95,12 +102,14 @@ def format_exception(e):
95 102
96 return exception_str 103 return exception_str
97 104
105
98 def leaders(xs, top=20): 106 def leaders(xs, top=20):
99 counts = defaultdict(int) 107 counts = defaultdict(int)
100 for x in xs: 108 for x in xs:
101 counts[x] += 1 109 counts[x] += 1
102 return sorted(counts.items(), reverse=True, key=lambda tup: tup[1])[:top] 110 return sorted(counts.items(), reverse=True, key=lambda tup: tup[1])[:top]
103 111
112
104 def byteify(input): 113 def byteify(input):
105 if isinstance(input, dict): 114 if isinstance(input, dict):
106 return {byteify(key): byteify(value) for key, value in input.iteritems()} 115 return {byteify(key): byteify(value) for key, value in input.iteritems()}
...@@ -111,18 +120,20 @@ def byteify(input): ...@@ -111,18 +120,20 @@ def byteify(input):
111 else: 120 else:
112 return input 121 return input
113 122
123
114 def search_youtube(query): 124 def search_youtube(query):
115 query_string = {"search_query" : query} 125 query_string = {"search_query": query}
116 r = requests.get("http://www.youtube.com/results", params=query_string) 126 r = requests.get("http://www.youtube.com/results", params=query_string)
117 search_results = re.findall(r'href=\"\/watch\?v=(.{11})', r.content) 127 search_results = re.findall(r'href=\"\/watch\?v=(.{11})', r.content)
118 log("http://www.youtube.com/watch?v=" + search_results[0]) 128 log("http://www.youtube.com/watch?v=" + search_results[0])
119 return "http://www.youtube.com/watch?v=" + search_results[0] 129 return "http://www.youtube.com/watch?v=" + search_results[0]
120 130
131
121 def search_google_images(query, animated=False): 132 def search_google_images(query, animated=False):
122 headers = {'User-Agent': "Mozilla/5.0 (X11; FreeBSD amd64; rv:12.0) Gecko/20100101 Firefox/12.0"} 133 headers = {'User-Agent': "Mozilla/5.0 (X11; FreeBSD amd64; rv:12.0) Gecko/20100101 Firefox/12.0"}
123 query_string = {"safe": "off", "tbm": "isch", "q" : query} 134 query_string = {"safe": "off", "tbm": "isch", "q": query}
124 if animated: 135 if animated:
125 query_string = {"safe": "off", "tbm": "isch", "q" : query, 'tbs': 'itp:animated'} 136 query_string = {"safe": "off", "tbm": "isch", "q": query, 'tbs': 'itp:animated'}
126 137
127 r = requests.get("http://www.google.com/search", params=query_string, headers=headers) 138 r = requests.get("http://www.google.com/search", params=query_string, headers=headers)
128 139
...@@ -139,24 +150,23 @@ def search_google_images(query, animated=False): ...@@ -139,24 +150,23 @@ def search_google_images(query, animated=False):
139 150
140 151
141 ################# 152 #################
142 ## Client Events 153 # Client Events
143 ################# 154 #################
144
145 @client.event 155 @client.event
146 def on_socket_raw_send(payload, binary=False): 156 def on_socket_raw_send(payload, binary=False):
147 check_msg_queue() 157 check_msg_queue()
148 158
159
149 @client.event 160 @client.event
150 def on_status(member): 161 def on_status(member):
151 for member in client.get_all_members(): 162 for member in client.get_all_members():
152 try: 163 try:
153 db_member = data.db_get_member(member.id) 164 db_member = data.db_get_member(member.id)
154 #log(db_member) 165
155 if not db_member: 166 if not db_member:
156 log("Creating new member: {}".format(member) ) 167 log("Creating new member: {}".format(member))
157 data.db_create_member(member) 168 data.db_create_member(member)
158 else: 169 else:
159 #log("Updating member: {}".format(member) )
160 data.db_update_member(member, db_member) 170 data.db_update_member(member, db_member)
161 171
162 check_msg_queue() 172 check_msg_queue()
...@@ -164,8 +174,8 @@ def on_status(member): ...@@ -164,8 +174,8 @@ def on_status(member):
164 log("Exception: {}".format(format_exception(e))) 174 log("Exception: {}".format(format_exception(e)))
165 pass 175 pass
166 176
177
167 def check_msg_queue(): 178 def check_msg_queue():
168 #log("checking messages")
169 messages = data.db_get_messages() 179 messages = data.db_get_messages()
170 if messages: 180 if messages:
171 for message in messages: 181 for message in messages:
...@@ -181,6 +191,7 @@ def check_msg_queue(): ...@@ -181,6 +191,7 @@ def check_msg_queue():
181 log("{}\nFailed to send message: {}".format(format_exception(e), message['message_id'],)) 191 log("{}\nFailed to send message: {}".format(format_exception(e), message['message_id'],))
182 return 192 return
183 193
194
184 def do_roll(client, message_parts, message): 195 def do_roll(client, message_parts, message):
185 request = message_parts[0] 196 request = message_parts[0]
186 count = 1 197 count = 1
...@@ -201,15 +212,15 @@ def do_roll(client, message_parts, message): ...@@ -201,15 +212,15 @@ def do_roll(client, message_parts, message):
201 return 212 return
202 roll_results = [] 213 roll_results = []
203 for i in range(count): 214 for i in range(count):
204 roll_results.append(random.randint(1,dice)) 215 roll_results.append(random.randint(1, dice))
205 out_string = '{} your roll {}d{}: {} = {}'.format(message.author.mention(), count, dice, '+'.join(str(r) for r in roll_results), sum(roll_results)) 216 out_string = '{} your roll {}d{}: {} = {}'.format(message.author.mention(), count, dice, '+'.join(str(r) for r in roll_results), sum(roll_results))
206 client.send_message(message.channel, out_string) 217 client.send_message(message.channel, out_string)
207 return 218 return
208 219
220
209 def do_lastseen(client, message_parts, message): 221 def do_lastseen(client, message_parts, message):
210 username = ' '.join(message_parts).replace('@', '').lower() 222 username = ' '.join(message_parts).replace('@', '').lower()
211 member = data.db_get_member(username=username) 223 member = data.db_get_member(username=username)
212 #log(member)
213 if member: 224 if member:
214 out_string = '' 225 out_string = ''
215 if member['is_afk'] == 1: 226 if member['is_afk'] == 1:
...@@ -228,9 +239,9 @@ def do_lastseen(client, message_parts, message): ...@@ -228,9 +239,9 @@ def do_lastseen(client, message_parts, message):
228 client.send_message(message.channel, 'I don\'t have any data on {} yet {}'.format(byteify(username), message.author.mention())) 239 client.send_message(message.channel, 'I don\'t have any data on {} yet {}'.format(byteify(username), message.author.mention()))
229 return 240 return
230 241
242
231 def do_help(client, message_parts, message): 243 def do_help(client, message_parts, message):
232 client.send_message(message.channel, 244 client.send_message(message.channel, """{} Available Commands:
233 """{} Available Commands:
234 You can ask compound or questions and I will choose. Example: HellsBot Rui is a Faggot or Rui is a faggot? 245 You can ask compound or questions and I will choose. Example: HellsBot Rui is a Faggot or Rui is a faggot?
235 246
236 User Info: 247 User Info:
...@@ -272,29 +283,35 @@ Stuff: ...@@ -272,29 +283,35 @@ Stuff:
272 !shutup - disables all image / gif / youtube spam for 5 minutes 283 !shutup - disables all image / gif / youtube spam for 5 minutes
273 !bemyirlwaifu""".format(message.author.mention())) 284 !bemyirlwaifu""".format(message.author.mention()))
274 285
286
275 def do_shutup(client, message_parts, message): 287 def do_shutup(client, message_parts, message):
288 global muted_until
276 muted_until = datetime.datetime.now() + datetime.timedelta(minutes=5) 289 muted_until = datetime.datetime.now() + datetime.timedelta(minutes=5)
277 client.send_message(message.channel, 'All image / gif / youtube spam disabled for 5 minutes') 290 client.send_message(message.channel, 'All image / gif / youtube spam disabled for 5 minutes')
278 return 291 return
279 292
293
280 def do_youtube(client, message_parts, message): 294 def do_youtube(client, message_parts, message):
281 if datetime.datetime.now() < muted_until: 295 if datetime.datetime.now() < muted_until:
282 return 296 return
283 client.send_message(message.channel, search_youtube(' '.join(message_parts))) 297 client.send_message(message.channel, search_youtube(' '.join(message_parts)))
284 return 298 return
285 299
300
286 def do_image(client, message_parts, message): 301 def do_image(client, message_parts, message):
287 if datetime.datetime.now() < muted_until: 302 if datetime.datetime.now() < muted_until:
288 return 303 return
289 client.send_message(message.channel, search_google_images(' '.join(message_parts))) 304 client.send_message(message.channel, search_google_images(' '.join(message_parts)))
290 return 305 return
291 306
307
292 def do_gif(client, message_parts, message): 308 def do_gif(client, message_parts, message):
293 if datetime.datetime.now() < muted_until: 309 if datetime.datetime.now() < muted_until:
294 return 310 return
295 client.send_message(message.channel, search_google_images(' '.join(message_parts), True)) 311 client.send_message(message.channel, search_google_images(' '.join(message_parts), True))
296 return 312 return
297 313
314
298 def do_gameslist(client, message_parts, message): 315 def do_gameslist(client, message_parts, message):
299 limit = 20 316 limit = 20
300 if len(message_parts) > 0 and message_parts[0].isdigit(): 317 if len(message_parts) > 0 and message_parts[0].isdigit():
...@@ -310,6 +327,7 @@ def do_gameslist(client, message_parts, message): ...@@ -310,6 +327,7 @@ def do_gameslist(client, message_parts, message):
310 out_string = out_string[1900:] 327 out_string = out_string[1900:]
311 return 328 return
312 329
330
313 def do_alias(client, message_parts, message): 331 def do_alias(client, message_parts, message):
314 if len(message_parts) > 0: 332 if len(message_parts) > 0:
315 username = ' '.join(message_parts).replace('@', '').lower() 333 username = ' '.join(message_parts).replace('@', '').lower()
...@@ -327,6 +345,7 @@ def do_alias(client, message_parts, message): ...@@ -327,6 +345,7 @@ def do_alias(client, message_parts, message):
327 client.send_message(message.channel, 'I don\'t know who you are speaking of {}!'.format(message.author.mention())) 345 client.send_message(message.channel, 'I don\'t know who you are speaking of {}!'.format(message.author.mention()))
328 return 346 return
329 347
348
330 def do_addalias(client, message_parts, message): 349 def do_addalias(client, message_parts, message):
331 alias = ' '.join(message_parts) 350 alias = ' '.join(message_parts)
332 username = message.author.name.lower() 351 username = message.author.name.lower()
...@@ -338,6 +357,7 @@ def do_addalias(client, message_parts, message): ...@@ -338,6 +357,7 @@ def do_addalias(client, message_parts, message):
338 client.send_message(message.channel, 'Something horrible happened and it is all your fault, try logging out / on again or play a game. (or fuck off i dunno i\'m just an error message. Who am I to tell you how to run your life...)') 357 client.send_message(message.channel, 'Something horrible happened and it is all your fault, try logging out / on again or play a game. (or fuck off i dunno i\'m just an error message. Who am I to tell you how to run your life...)')
339 return 358 return
340 359
360
341 def do_games(client, message_parts, message): 361 def do_games(client, message_parts, message):
342 if len(message_parts) > 0: 362 if len(message_parts) > 0:
343 username = ' '.join(message_parts).replace('@', '').lower() 363 username = ' '.join(message_parts).replace('@', '').lower()
...@@ -352,12 +372,14 @@ def do_games(client, message_parts, message): ...@@ -352,12 +372,14 @@ def do_games(client, message_parts, message):
352 else: 372 else:
353 client.send_message(message.channel, 'I don\'t have any data on {} yet {}'.format(byteify(username), message.author.mention())) 373 client.send_message(message.channel, 'I don\'t have any data on {} yet {}'.format(byteify(username), message.author.mention()))
354 374
375
355 def do_reload(client, message_parts, message): 376 def do_reload(client, message_parts, message):
356 if message.author.id != '78767557628133376': 377 if message.author.id != '78767557628133376':
357 client.send_message(message.channel, "You shouldn't be calling this. You are clearly looking to piss Hellsbreath off.") 378 client.send_message(message.channel, "You shouldn't be calling this. You are clearly looking to piss Hellsbreath off.")
358 return 379 return
359 call(["service", "hellsbot", "restart"]) 380 call(["service", "hellsbot", "restart"])
360 381
382
361 def do_whoplayed(client, message_parts, message): 383 def do_whoplayed(client, message_parts, message):
362 game_name = ' '.join(message_parts) 384 game_name = ' '.join(message_parts)
363 member_list = data.db_get_whoplayed(game_name) 385 member_list = data.db_get_whoplayed(game_name)
...@@ -373,6 +395,7 @@ def do_whoplayed(client, message_parts, message): ...@@ -373,6 +395,7 @@ def do_whoplayed(client, message_parts, message):
373 out_string = out_string[1900:] 395 out_string = out_string[1900:]
374 return 396 return
375 397
398
376 def do_gimmecredits(client, message_parts, message): 399 def do_gimmecredits(client, message_parts, message):
377 member = data.db_get_member(message.author.id) 400 member = data.db_get_member(message.author.id)
378 if not member: 401 if not member:
...@@ -387,6 +410,7 @@ def do_gimmecredits(client, message_parts, message): ...@@ -387,6 +410,7 @@ def do_gimmecredits(client, message_parts, message):
387 client.send_message(message.author, "You already have credits. Stop begging.") 410 client.send_message(message.author, "You already have credits. Stop begging.")
388 return 411 return
389 412
413
390 def do_grantcredits(clients, message_parts, message): 414 def do_grantcredits(clients, message_parts, message):
391 if message.author.id != '78767557628133376': 415 if message.author.id != '78767557628133376':
392 client.send_message(message.channel, "You are not Hellsbreath. Use !gimmecredits to get a few extra if you run out.") 416 client.send_message(message.channel, "You are not Hellsbreath. Use !gimmecredits to get a few extra if you run out.")
...@@ -402,6 +426,7 @@ def do_grantcredits(clients, message_parts, message): ...@@ -402,6 +426,7 @@ def do_grantcredits(clients, message_parts, message):
402 client.send_message(message.channel, "{} has been given {} credits.".format(member['member_name'], 100)) 426 client.send_message(message.channel, "{} has been given {} credits.".format(member['member_name'], 100))
403 return 427 return
404 428
429
405 def do_ticketrank(clients, message_parts, message): 430 def do_ticketrank(clients, message_parts, message):
406 members = data.db_get_all_members() 431 members = data.db_get_all_members()
407 if len(members) < 0: 432 if len(members) < 0:
...@@ -410,13 +435,11 @@ def do_ticketrank(clients, message_parts, message): ...@@ -410,13 +435,11 @@ def do_ticketrank(clients, message_parts, message):
410 ticket_count = 0 435 ticket_count = 0
411 for member in members: 436 for member in members:
412 if member['discord_id'] != '78767557628133376': 437 if member['discord_id'] != '78767557628133376':
413 #log(member)
414 ticket_count += member['tickets'] 438 ticket_count += member['tickets']
415 if ticket_count == 0: 439 if ticket_count == 0:
416 client.send_message(message.channel, "No Tickets have been sold for this raffle.") 440 client.send_message(message.channel, "No Tickets have been sold for this raffle.")
417 return 441 return
418 442
419 #log("Ticket Count: {}".format(ticket_count))
420 out_string = "" 443 out_string = ""
421 for member in members: 444 for member in members:
422 if member['tickets'] > 0 and member['discord_id'] != '78767557628133376': 445 if member['tickets'] > 0 and member['discord_id'] != '78767557628133376':
...@@ -426,6 +449,7 @@ def do_ticketrank(clients, message_parts, message): ...@@ -426,6 +449,7 @@ def do_ticketrank(clients, message_parts, message):
426 client.send_message(message.channel, out_string) 449 client.send_message(message.channel, out_string)
427 return 450 return
428 451
452
429 def do_startraffle(client, message_parts, message): 453 def do_startraffle(client, message_parts, message):
430 if message.author.id != '78767557628133376': 454 if message.author.id != '78767557628133376':
431 client.send_message(message.channel, "You are not Hellsbreath. Go die in an especially hot fire.") 455 client.send_message(message.channel, "You are not Hellsbreath. Go die in an especially hot fire.")
...@@ -437,13 +461,11 @@ def do_startraffle(client, message_parts, message): ...@@ -437,13 +461,11 @@ def do_startraffle(client, message_parts, message):
437 ticket_count = 0 461 ticket_count = 0
438 for member in members: 462 for member in members:
439 if member['discord_id'] != '78767557628133376': 463 if member['discord_id'] != '78767557628133376':
440 #log(member)
441 ticket_count += member['tickets'] 464 ticket_count += member['tickets']
442 if ticket_count == 0: 465 if ticket_count == 0:
443 client.send_message(message.channel, "No Tickets have been sold for this raffle.") 466 client.send_message(message.channel, "No Tickets have been sold for this raffle.")
444 return 467 return
445 468
446 #log("Ticket Count: {}".format(ticket_count))
447 out_string = "The final standings are as follows: \n\n" 469 out_string = "The final standings are as follows: \n\n"
448 ticket_reel = [] 470 ticket_reel = []
449 for member in members: 471 for member in members:
...@@ -461,13 +483,14 @@ def do_startraffle(client, message_parts, message): ...@@ -461,13 +483,14 @@ def do_startraffle(client, message_parts, message):
461 while second in ticket_reel: 483 while second in ticket_reel:
462 ticket_reel.remove(second) 484 ticket_reel.remove(second)
463 time.sleep(0.5) 485 time.sleep(0.5)
464 client.send_message(message.channel, "\n\n*2nd Place:* {}".format(byteify(winner))) 486 client.send_message(message.channel, "\n\n*2nd Place:* {}".format(byteify(second)))
465 if len(ticket_reel) > 0: 487 if len(ticket_reel) > 0:
466 third = random.choice(ticket_reel) 488 third = random.choice(ticket_reel)
467 time.sleep(0.5) 489 time.sleep(0.5)
468 client.send_message(message.channel, "\n*3rd Place:* {}".format(byteify(winner))) 490 client.send_message(message.channel, "\n*3rd Place:* {}".format(byteify(third)))
469 return 491 return
470 492
493
471 def do_raffle(client, message_parts, message): 494 def do_raffle(client, message_parts, message):
472 client.send_message(message.channel, """Current Raffle Item: 495 client.send_message(message.channel, """Current Raffle Item:
473 496
...@@ -496,6 +519,7 @@ You can get extra credits by playing !slots <amount> and !bet <amount> on BlackJ ...@@ -496,6 +519,7 @@ You can get extra credits by playing !slots <amount> and !bet <amount> on BlackJ
496 """) 519 """)
497 return 520 return
498 521
522
499 def do_buyticket(client, message_parts, message): 523 def do_buyticket(client, message_parts, message):
500 member = data.db_get_member(message.author.id) 524 member = data.db_get_member(message.author.id)
501 if not member: 525 if not member:
...@@ -510,6 +534,7 @@ def do_buyticket(client, message_parts, message): ...@@ -510,6 +534,7 @@ def do_buyticket(client, message_parts, message):
510 client.send_message(message.author, "Raffle ticket purchased. Tickets: {} Credits: {}".format(response, credits)) 534 client.send_message(message.author, "Raffle ticket purchased. Tickets: {} Credits: {}".format(response, credits))
511 return 535 return
512 536
537
513 def do_balance(client, message_parts, message): 538 def do_balance(client, message_parts, message):
514 member = data.db_get_member(message.author.id) 539 member = data.db_get_member(message.author.id)
515 if not member: 540 if not member:
...@@ -518,6 +543,7 @@ def do_balance(client, message_parts, message): ...@@ -518,6 +543,7 @@ def do_balance(client, message_parts, message):
518 client.send_message(message.author, "Credits: {}\nTickets: {}".format(member['credits'], member['tickets'])) 543 client.send_message(message.author, "Credits: {}\nTickets: {}".format(member['credits'], member['tickets']))
519 return 544 return
520 545
546
521 def do_slotsrules(client, message_parts, message): 547 def do_slotsrules(client, message_parts, message):
522 client.send_message(message.channel, """Paying Combinations: 548 client.send_message(message.channel, """Paying Combinations:
523 549
...@@ -538,6 +564,7 @@ Max Bet: 10 credits ...@@ -538,6 +564,7 @@ Max Bet: 10 credits
538 To Play: !slots <bet>""") 564 To Play: !slots <bet>""")
539 return 565 return
540 566
567
541 def do_slots(client, message_parts, message): 568 def do_slots(client, message_parts, message):
542 member = data.db_get_member(message.author.id) 569 member = data.db_get_member(message.author.id)
543 if not member: 570 if not member:
...@@ -571,6 +598,7 @@ def do_slots(client, message_parts, message): ...@@ -571,6 +598,7 @@ def do_slots(client, message_parts, message):
571 diamonds = reels.count(":diamonds:") 598 diamonds = reels.count(":diamonds:")
572 bells = reels.count(":bell:") 599 bells = reels.count(":bell:")
573 moneybags = reels.count(":moneybag:") 600 moneybags = reels.count(":moneybag:")
601 out_string = ""
574 if moneybags == 3: 602 if moneybags == 3:
575 out_string += "JACKPOT!!" 603 out_string += "JACKPOT!!"
576 winnings = 250 604 winnings = 250
...@@ -606,6 +634,7 @@ def do_slots(client, message_parts, message): ...@@ -606,6 +634,7 @@ def do_slots(client, message_parts, message):
606 client.send_message(message.author, out_string) 634 client.send_message(message.author, out_string)
607 return 635 return
608 636
637
609 def do_bj_hit(client, message_parts, message): 638 def do_bj_hit(client, message_parts, message):
610 member = data.db_get_member(message.author.id) 639 member = data.db_get_member(message.author.id)
611 if not member: 640 if not member:
...@@ -643,6 +672,7 @@ def do_bj_hit(client, message_parts, message): ...@@ -643,6 +672,7 @@ def do_bj_hit(client, message_parts, message):
643 client.send_message(message.author, "You must start a game with !bet before you can ask for a new card.") 672 client.send_message(message.author, "You must start a game with !bet before you can ask for a new card.")
644 return 673 return
645 674
675
646 def do_bj_stay(client, message_parts, message): 676 def do_bj_stay(client, message_parts, message):
647 member = data.db_get_member(message.author.id) 677 member = data.db_get_member(message.author.id)
648 if not member: 678 if not member:
...@@ -669,6 +699,7 @@ def do_bj_stay(client, message_parts, message): ...@@ -669,6 +699,7 @@ def do_bj_stay(client, message_parts, message):
669 client.send_message(message.author, out_string) 699 client.send_message(message.author, out_string)
670 return 700 return
671 701
702
672 def do_bj_bet(client, message_parts, message): 703 def do_bj_bet(client, message_parts, message):
673 member = data.db_get_member(message.author.id) 704 member = data.db_get_member(message.author.id)
674 if not member: 705 if not member:
...@@ -739,10 +770,10 @@ def do_bj_bet(client, message_parts, message): ...@@ -739,10 +770,10 @@ def do_bj_bet(client, message_parts, message):
739 client.send_message(message.author, out_string) 770 client.send_message(message.author, out_string)
740 return 771 return
741 772
773
742 def do_msg(client, message_parts, message): 774 def do_msg(client, message_parts, message):
743 channel = message.channel 775 channel = message.channel
744 author = message.author 776 author = message.author
745 #author = message.author.name
746 username = '' 777 username = ''
747 try: 778 try:
748 # TODO: Switch message_bits with message_parts 779 # TODO: Switch message_bits with message_parts
...@@ -774,7 +805,6 @@ def do_msg(client, message_parts, message): ...@@ -774,7 +805,6 @@ def do_msg(client, message_parts, message):
774 user_mention = '' 805 user_mention = ''
775 # TODO: have it look in the database. Do this AFTER on startup we add all users. 806 # TODO: have it look in the database. Do this AFTER on startup we add all users.
776 for member in client.get_all_members(): 807 for member in client.get_all_members():
777 #log("MEMBER: {}".format(byteify(member)))
778 if username.lower() == member.name.lower(): 808 if username.lower() == member.name.lower():
779 user_mention = member.mention() 809 user_mention = member.mention()
780 user_id = member.id 810 user_id = member.id
...@@ -799,6 +829,7 @@ def do_msg(client, message_parts, message): ...@@ -799,6 +829,7 @@ def do_msg(client, message_parts, message):
799 check_msg_queue() 829 check_msg_queue()
800 return 830 return
801 831
832
802 def do_addfortune(client, message_parts, message): 833 def do_addfortune(client, message_parts, message):
803 try: 834 try:
804 fortune = message.content[9:] 835 fortune = message.content[9:]
...@@ -817,6 +848,7 @@ def do_addfortune(client, message_parts, message): ...@@ -817,6 +848,7 @@ def do_addfortune(client, message_parts, message):
817 client.send_message(message.channel, 'Your shitty fortune has been added {}.'.format(message.author.mention())) 848 client.send_message(message.channel, 'Your shitty fortune has been added {}.'.format(message.author.mention()))
818 return 849 return
819 850
851
820 def do_fortune(client, message_parts, message): 852 def do_fortune(client, message_parts, message):
821 fortune = None 853 fortune = None
822 try: 854 try:
...@@ -833,6 +865,7 @@ def do_fortune(client, message_parts, message): ...@@ -833,6 +865,7 @@ def do_fortune(client, message_parts, message):
833 client.send_message(message.channel, '{} Your fortune is... {}'.format(message.author.mention(), byteify(fortune))) 865 client.send_message(message.channel, '{} Your fortune is... {}'.format(message.author.mention(), byteify(fortune)))
834 return 866 return
835 867
868
836 def do_question(client, message_parts, message): 869 def do_question(client, message_parts, message):
837 question = message.content[10:] 870 question = message.content[10:]
838 if "is it gay" in question: 871 if "is it gay" in question:
...@@ -862,13 +895,13 @@ def do_question(client, message_parts, message): ...@@ -862,13 +895,13 @@ def do_question(client, message_parts, message):
862 client.send_message(message.channel, 'I don\'t know {}.'.format(message.author.mention())) 895 client.send_message(message.channel, 'I don\'t know {}.'.format(message.author.mention()))
863 return 896 return
864 897
898
865 def do_addjoke(client, message_parts, message): 899 def do_addjoke(client, message_parts, message):
866 try: 900 try:
867 joke = message.content[9:] 901 joke = message.content[9:]
868 if 'aa737a5846' in joke: 902 if 'aa737a5846' in joke:
869 client.send_message(message.channel, '{} you stop it, you are a pedofile, stop looking at little girls.'.format(message.author.mention())) 903 client.send_message(message.channel, '{} you stop it, you are a pedofile, stop looking at little girls.'.format(message.author.mention()))
870 return 904 return
871 date_added = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
872 c = conn.cursor() 905 c = conn.cursor()
873 c.execute("INSERT INTO jokes (joke) VALUES (?)", (joke,)) 906 c.execute("INSERT INTO jokes (joke) VALUES (?)", (joke,))
874 conn.commit() 907 conn.commit()
...@@ -880,6 +913,7 @@ def do_addjoke(client, message_parts, message): ...@@ -880,6 +913,7 @@ def do_addjoke(client, message_parts, message):
880 client.send_message(message.channel, 'Your shitty joke has been added {}.'.format(message.author.mention())) 913 client.send_message(message.channel, 'Your shitty joke has been added {}.'.format(message.author.mention()))
881 return 914 return
882 915
916
883 def do_joke(client, message_parts, message): 917 def do_joke(client, message_parts, message):
884 joke = None 918 joke = None
885 try: 919 try:
...@@ -895,12 +929,37 @@ def do_joke(client, message_parts, message): ...@@ -895,12 +929,37 @@ def do_joke(client, message_parts, message):
895 client.send_message(message.channel, '{} {}'.format(message.author.mention(), byteify(joke))) 929 client.send_message(message.channel, '{} {}'.format(message.author.mention(), byteify(joke)))
896 return 930 return
897 931
932
933 def do_secret(client, message_parts, message):
934 client.send_message(message.channel, 'git gud {}! My source is here: http://git.savsoul.com/barry/discordbot\nVersion: {}'.format(message.author.mention(), VERSION))
935 return
936
937
938 def do_waifu(client, message_parts, message):
939 client.send_message(message.channel, 'http://orig13.deviantart.net/b25e/f/2014/175/3/d/no_waifu_no_laifu_by_imtheonenexttome-d7nsx3b.gif {}!'.format(message.author.mention()))
940 return
941
942
943 def do_hillary(client, message_parts, message):
944 client.send_message(message.channel, ':bomb: Ohhhhhh, now you done it...:bomb:'.format(message.author.mention()))
945 return
946
947
948 def do_squid(client, message_parts, message):
949 client.send_message(message.channel, 'くコ:彡 くコ:彡 くコ:彡 くコ:彡')
950 return
951
952
953 def do_stars(client, message_parts, message):
954 client.send_message(message.channel, '✮═━┈  ✰═━┈  ✮═━┈  ✰═━┈  ✮═━┈  ✰═━┈  ✮═━┈  ✰═━┈  ✮═━┈  ✰═━┈ ✰═━┈┈ ✰═━┈┈')
955 return
956
957
898 @client.event 958 @client.event
899 def on_message(message): 959 def on_message(message):
900 #print message.content 960 #print message.content
901 #print message.author 961 #print message.author
902 #print client.user 962 #print client.user
903 global muted_until
904 # we do not want the bot to reply to itself 963 # we do not want the bot to reply to itself
905 if message.author == client.user: 964 if message.author == client.user:
906 return 965 return
...@@ -909,7 +968,7 @@ def on_message(message): ...@@ -909,7 +968,7 @@ def on_message(message):
909 for command, method in registered_commands.iteritems(): 968 for command, method in registered_commands.iteritems():
910 if message_parts[0] == command: 969 if message_parts[0] == command:
911 try: 970 try:
912 globals()[method](client, message_parts[1:], message) 971 thread.start_new_thread(globals()[method], (client, message_parts[1:], message))
913 except Exception as e: 972 except Exception as e:
914 log("{} - {}".format(format_exception(e), e.message)) 973 log("{} - {}".format(format_exception(e), e.message))
915 return 974 return
...@@ -918,31 +977,7 @@ def on_message(message): ...@@ -918,31 +977,7 @@ def on_message(message):
918 log('Someone is talking to %s' % (client.user.name.lower(),)) 977 log('Someone is talking to %s' % (client.user.name.lower(),))
919 if ' or ' in message.content: 978 if ' or ' in message.content:
920 questions = message.content[len(client.user.name)+1:].replace('?', '').split(' or ') 979 questions = message.content[len(client.user.name)+1:].replace('?', '').split(' or ')
921 client.send_message(message.channel, '{} I choose: {}'.format(message.author.mention(), random.choice(questions).encode('utf-8',errors='ignore'))) 980 client.send_message(message.channel, '{} I choose: {}'.format(message.author.mention(), random.choice(questions).encode('utf-8', errors='ignore')))
922
923 if message.content.startswith('!secret'):
924 client.send_message(message.channel, 'git gud {}! My source is here: http://git.savsoul.com/barry/discordbot\nVersion: {}'.format(message.author.mention(), VERSION))
925 return
926
927 if message.content.startswith('!bemyirlwaifu'):
928 client.send_message(message.channel, 'http://orig13.deviantart.net/b25e/f/2014/175/3/d/no_waifu_no_laifu_by_imtheonenexttome-d7nsx3b.gif {}!'.format(message.author.mention()))
929 return
930
931 if message.content.startswith('HILLARY 2016'):
932 client.send_message(message.channel, ':bomb: Ohhhhhh, now you done it...:bomb:'.format(message.author.mention()))
933 return
934
935 if message.content.startswith('!squid'):
936 client.send_message(message.channel, 'くコ:彡 くコ:彡 くコ:彡 くコ:彡')
937 return
938
939 if message.content.startswith('!stars'):
940 client.send_message(message.channel, '✮═━┈  ✰═━┈  ✮═━┈  ✰═━┈  ✮═━┈  ✰═━┈  ✮═━┈  ✰═━┈  ✮═━┈  ✰═━┈ ✰═━┈┈ ✰═━┈┈')
941 return
942
943 if message.content.startswith('!hello'):
944 client.send_message(message.channel, 'Hello {}!'.format(message.author.mention()))
945 return
946 981
947 982
948 @client.event 983 @client.event
...@@ -959,7 +994,7 @@ def on_ready(): ...@@ -959,7 +994,7 @@ def on_ready():
959 retries = 0 994 retries = 0
960 while retries < 1000: 995 while retries < 1000:
961 try: 996 try:
962 json_data=open(credentials).read() 997 json_data = open(credentials).read()
963 creds = json.loads(json_data) 998 creds = json.loads(json_data)
964 wolf = wolframalpha.Client(creds['wolframkey']) 999 wolf = wolframalpha.Client(creds['wolframkey'])
965 client.login(creds['username'], creds['password']) 1000 client.login(creds['username'], creds['password'])
......