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
from collections import defaultdict
from nltk.tag import pos_tag
import wolframalpha
import sqlite3
VERSION = 1.5
VERSION = 1.6
conn = sqlite3.connect('db.sqlite3')
member_status = 'members.json'
deliveries_file = 'deliveries.json'
......@@ -620,38 +623,36 @@ Stuff:
if message.content.startswith('!addjoke'):
try:
json_data=open(joke_file).read()
data = json.loads(json_data)
except ValueError:
data = []
if not data:
data = []
try:
if 'aa737a5846' in message.content[9:]:
joke = message.content[9:]
if 'aa737a5846' in joke:
client.send_message(message.channel, '{} you stop it, you are a pedofile, stop looking at little girls.'.format(message.author.mention()))
return
data.append(byteify(message.content[9:]))
jdata = json.dumps(data, ensure_ascii=False)
date_added = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
c = conn.cursor()
c.execute("INSERT INTO jokes (joke) VALUES (?)", (joke,))
conn.commit()
print("Added joke")
except Exception as e:
print e.message
client.send_message(message.channel, 'Your shitty joke has been rejected {}.'.format(message.author.mention()))
return
open(joke_file, 'wb+').write(jdata.encode('utf8'))
client.send_message(message.channel, 'Your shitty joke has been added {}.'.format(message.author.mention()))
return
if message.content.startswith('!joke'):
data = None
joke = None
try:
json_data=open(joke_file).read()
data = json.loads(json_data)
except ValueError:
c = conn.cursor()
joke = c.execute("SELECT joke FROM jokes ORDER BY RANDOM() LIMIT 1;").fetchone()[0]
print(joke)
except Exception as e:
print(e)
pass
if not data:
if not joke:
client.send_message(message.channel, 'Try adding a joke with "!addjoke <joke>" {}!'.format(message.author.mention()))
else:
client.send_message(message.channel, '{} {}'.format(message.author.mention(), random.choice(data).encode('utf-8',errors='ignore')))
client.send_message(message.channel, '{} {}'.format(message.author.mention(), byteify(joke)))
return
if message.content.startswith('!secret'):
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:
client.run()
except KeyboardInterrupt:
conn.close
quit()
except:
retries += 1
......