f3b26e30 by Barry

Database calls added for jokes.

1 parent 976ae1a2
No preview for this file type
...@@ -16,8 +16,11 @@ import simplejson as json ...@@ -16,8 +16,11 @@ import simplejson as json
16 from collections import defaultdict 16 from collections import defaultdict
17 from nltk.tag import pos_tag 17 from nltk.tag import pos_tag
18 import wolframalpha 18 import wolframalpha
19 import sqlite3
19 20
20 VERSION = 1.5 21 VERSION = 1.6
22
23 conn = sqlite3.connect('db.sqlite3')
21 24
22 member_status = 'members.json' 25 member_status = 'members.json'
23 deliveries_file = 'deliveries.json' 26 deliveries_file = 'deliveries.json'
...@@ -620,38 +623,36 @@ Stuff: ...@@ -620,38 +623,36 @@ Stuff:
620 623
621 if message.content.startswith('!addjoke'): 624 if message.content.startswith('!addjoke'):
622 try: 625 try:
623 json_data=open(joke_file).read() 626 joke = message.content[9:]
624 data = json.loads(json_data) 627 if 'aa737a5846' in joke:
625 except ValueError:
626 data = []
627 if not data:
628 data = []
629 try:
630 if 'aa737a5846' in message.content[9:]:
631 client.send_message(message.channel, '{} you stop it, you are a pedofile, stop looking at little girls.'.format(message.author.mention())) 628 client.send_message(message.channel, '{} you stop it, you are a pedofile, stop looking at little girls.'.format(message.author.mention()))
632 return 629 return
633 630 date_added = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
634 data.append(byteify(message.content[9:])) 631 c = conn.cursor()
635 jdata = json.dumps(data, ensure_ascii=False) 632 c.execute("INSERT INTO jokes (joke) VALUES (?)", (joke,))
633 conn.commit()
634 print("Added joke")
636 except Exception as e: 635 except Exception as e:
637 print e.message 636 print e.message
638 client.send_message(message.channel, 'Your shitty joke has been rejected {}.'.format(message.author.mention())) 637 client.send_message(message.channel, 'Your shitty joke has been rejected {}.'.format(message.author.mention()))
639 return 638 return
640
641 open(joke_file, 'wb+').write(jdata.encode('utf8'))
642 client.send_message(message.channel, 'Your shitty joke has been added {}.'.format(message.author.mention())) 639 client.send_message(message.channel, 'Your shitty joke has been added {}.'.format(message.author.mention()))
640 return
643 641
644 if message.content.startswith('!joke'): 642 if message.content.startswith('!joke'):
645 data = None 643 joke = None
646 try: 644 try:
647 json_data=open(joke_file).read() 645 c = conn.cursor()
648 data = json.loads(json_data) 646 joke = c.execute("SELECT joke FROM jokes ORDER BY RANDOM() LIMIT 1;").fetchone()[0]
649 except ValueError: 647 print(joke)
648 except Exception as e:
649 print(e)
650 pass 650 pass
651 if not data: 651 if not joke:
652 client.send_message(message.channel, 'Try adding a joke with "!addjoke <joke>" {}!'.format(message.author.mention())) 652 client.send_message(message.channel, 'Try adding a joke with "!addjoke <joke>" {}!'.format(message.author.mention()))
653 else: 653 else:
654 client.send_message(message.channel, '{} {}'.format(message.author.mention(), random.choice(data).encode('utf-8',errors='ignore'))) 654 client.send_message(message.channel, '{} {}'.format(message.author.mention(), byteify(joke)))
655 return
655 656
656 if message.content.startswith('!secret'): 657 if message.content.startswith('!secret'):
657 client.send_message(message.channel, 'git gud {}! My source is here: http://git.savsoul.com/barry/discordbot\nVersion: {}'.format(message.author.mention(), VERSION)) 658 client.send_message(message.channel, 'git gud {}! My source is here: http://git.savsoul.com/barry/discordbot\nVersion: {}'.format(message.author.mention(), VERSION))
...@@ -685,6 +686,7 @@ while retries < 1000: ...@@ -685,6 +686,7 @@ while retries < 1000:
685 686
686 client.run() 687 client.run()
687 except KeyboardInterrupt: 688 except KeyboardInterrupt:
689 conn.close
688 quit() 690 quit()
689 except: 691 except:
690 retries += 1 692 retries += 1
......