/
quote.py
67 lines (51 loc) · 1.8 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
from datetime import datetime
import random
from zope.interface import implements
from sqlalchemy import Integer, String, Date, Column
from sqlalchemy.ext.declarative import declarative_base
import sqlalchemy
from hamper.interfaces import Command
SQLAlchemyBase = declarative_base()
class Quotes(Command):
'''Remember quotes, and recall on demand.'''
name = 'quotes'
priority = 0
regex = r'^quotes?(?: +(.*))?$'
def setup(self, factory):
SQLAlchemyBase.metadata.create_all(factory.db_engine)
def command(self, bot, comm, groups):
if groups[0]:
args = groups[0].split(' ')
args = [a.strip() for a in args if a.strip()]
else:
args = []
if len(args) == 0:
# Deliver a quote
index = random.randrange(0, bot.db.query(Quote).count() + 1)
quote = bot.factory.db.query(Quote)[index]
# Lame twisted irc doesn't support unicode.
bot.say(str(quote.text))
elif args[0] == '--args':
# Add a quote
text = ' '.join(args[1:])
quote = Quote(text, comm['user'])
bot.factory.db.add(quote)
bot.say('Succesfully added quote.')
elif args[0] == '--count':
bot.say('I know {0} quotes.'.format(bot.db.query(Quote).count()))
else:
bot.say('Wait, what?')
class Quote(SQLAlchemyBase):
'''The object that will get persisted by the database.'''
__tablename__ = 'quotes'
id = Column(Integer, primary_key=True)
text = Column(String)
adder = Column(String)
added = Column(Date)
def __init__(self, text, adder, added=None):
if not added:
added = datetime.now()
self.text = text
self.adder = adder
self.added = added
quotes = Quotes()