-
Notifications
You must be signed in to change notification settings - Fork 1
/
QuoteDB.py
124 lines (112 loc) · 4.09 KB
/
QuoteDB.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
"""
QuoteDB.py - A willie module to tag words
Copyright 2013, TecknoJock
Licensed under the Eiffel Forum License 2.
"""
from willie.tools import Nick
from willie.module import commands, example, priority, rule
import re
import random
try:
import imp
import sys
from permissions import perm_chk
except:
try:
ffp, pathname, description = imp.find_module('permissions',['/home/dropbox/Dropbox/WillieBot'])
permissions = imp.load_source('permissions', pathname, ffp)
sys.modules['permissions'] = permissions
finally:
if ffp:
ffp.close()
from permissions import perm_chk
def setup(willie):
Db = willie.db.connect()
cur = Db.cursor()
cur.execute('create table if not exists quote_table (quoteID INTEGER, quote, Primary Key (quoteID))')
listoftags = cur.fetchall()
Db.close()
@commands('addquote')
@example(u'!addquote <example> Im going to mount it')
def quoteadd(willie, trigger):
'''Adds a new quote. !tagadd <example> I'm going to mount it.'''
if not perm_chk(trigger.hostmask, "Bc", willie):
return
if not trigger.group(2):
willie.say("See !help !addquote for help")
return
quote = re.sub('([\00-\x02]|[\x04-\x1f])|(\x03[1-9][0-6]?(,[1-9][0-6]?)?)', '', trigger.group(2))
# try:
Db = willie.db.connect()
cur = Db.cursor()
params = (quote,)
cur.execute('Insert Into quote_table VALUES (null, ?)', params)
Db.commit()
willie.reply('Quote has been added.')
# except:
pass
# finally:
Db.close()
@commands('quoteremove')
@example(u'!quoteremove -1')
def quoteremove(willie, trigger):
'''Removes a quote by quoteID. !quoteremove -1'''
if not perm_chk(trigger.hostmask, "Ad", willie):
return
if not trigger.group(2):
willie.say("See !help !quoteremove for help")
return
try:
quoteref = int(trigger.group(2))
except:
willie.say("Quote to remove must be an int.")
try:
Db = willie.db.connect()
cur = Db.cursor()
if quoteref > 0:
cur.execute('Delete from quote_table where quoteID=?', (quoteref,))
else:
cur.execute('Delete from quote_table where quoteID=((select max(quoteID) from quote_table)+?)', (quoteref,))
Db.commit()
except:
willie.reply("Could not remove quote. It either does not exist or something went wrong.")
finally:
Db.close()
@commands('quote')
def getquote(willie, trigger):
try:
db = willie.db.connect()
cur = db.cursor()
if trigger.group(2):
try:
quoteref = trigger.group(2)
try:
quoteref = int(quoteref)
try:
if quoteref > 0:
cur.execute('Select quote from quote_table where quoteID=?', (quoteref,))
else:
cur.execute('Select quote from quote_table where quoteID=((select max(quoteID) from quote_table)+?)', (quoteref,))
willie.say(cur.fetchone()[0])
return
except:
quoteref = str(quoteref)
raise
except:
quoteref = "%%%s%%" % quoteref
cur.execute("Select quote FROM quote_table where quote LIKE ?", (quoteref,))
listofquotes = [x[0] for x in cur.fetchall()]
willie.say(random.choice(listofquotes))
return
except:
willie.say("Could not find quote.")
else:
cur.execute("Select quoteID from quote_table where quoteID=(select max(quoteID) from quote_table)")
numberofquotes = cur.fetchone()
quoteref = random.randint(1,numberofquotes[0])
cur.execute('Select quote from quote_table where quoteID=?', (quoteref,))
willie.say(cur.fetchone()[0])
finally:
db.close()
if __name__ == "__main__":
print __doc__.strip()