-
Notifications
You must be signed in to change notification settings - Fork 0
/
quote.py
136 lines (115 loc) · 4.08 KB
/
quote.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
"""
quote.py - A simple quotes module for willie
Copyright (C) 2014 Andy Chung - iamchung.com
Copyright (C) 2014 Luis Uribe - [email protected]
iamchung.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
from __future__ import unicode_literals
import sopel
from sopel import module
from sopel.module import commands, example
import sqlite3
import random
import codecs # TODO in python3, codecs.open isn't needed since the default open does encoding.
import re
@commands('quote')
def quote(bot, trigger):
filename = bot.config.quote.filename
raw_args = trigger.group(2)
output = ''
if raw_args is None or raw_args == '':
# display random quote
output = get_random_quote(bot, trigger.sender)
else:
# get subcommand
command_parts = raw_args.split(' ', 1)
if len(command_parts) < 2:
output = trigger.nick + ': nah nah'
else:
subcommand = command_parts[0]
data = command_parts[1]
# perform subcommand
if subcommand == 'add':
output = add_quote(bot, trigger.sender, data)
elif subcommand == 'search':
for quote in search_quote(bot, trigger.sender, data):
bot.say(quote)
else:
output = 'invalid subcommand'
bot.say(output)
def get_random_quote(bot, channel):
db = None
cur = None
db = connect_db(bot)
cur = db.cursor()
try:
cur.execute('SELECT quote FROM quotes WHERE channel = ? ORDER BY RANDOM() LIMIT 1;', (channel,))
res = cur.fetchone()
if res is None:
msg = 'We have no data'
else:
msg = res[0]
except Exception as e:
msg = "Error looking for quotes"
finally:
db.close()
return msg
def add_quote(bot, channel, search):
db = None
cur = None
db = connect_db(bot)
cur = db.cursor()
try:
bot.memory['chan_messages'][channel]
except:
msg = "There's no history for this channel."
return msg
for line in reversed(bot.memory['chan_messages'][channel]):
if re.search(search, line) is not None:
try:
cur.execute('INSERT INTO quotes (quote, channel) VALUES (?, ?);', (line, channel))
db.commit()
msg = "Quote added: " + line
except Exception as e:
raise e
msg = "Error adding quote"
finally:
db.close()
return msg
msg = "What are you doing, moron?"
return msg
def search_quote(bot, channel, search):
db = connect_db(bot)
cur = db.cursor()
msg = ['Quote not found.']
try:
cur.execute('SELECT quote FROM quotes WHERE quote LIKE ? and channel = ? ORDER BY RANDOM() LIMIT 5', ('%' + search + '%', channel))
msg = [quote[0] for quote in cur.fetchall()]
except Exception as e:
msg = ['Error looking for quotes']
return msg
def setup(bot):
bot.memory['chan_messages'] = {}
bot.memory['chan_quotes'] = {}
def connect_db(willie):
conn = sqlite3.connect(DB_FILE)
return conn
#save everything it's said on the channels
@module.rule('^[^\.].*')
def log_chan_message(bot, trigger):
try:
bot.memory['chan_messages'][trigger.sender].append(trigger.nick + ': ' +trigger)
except:
bot.memory['chan_messages'][trigger.sender] = []
bot.memory['chan_messages'][trigger.sender].append(trigger.nick + ': ' +trigger)