/
quote.py
64 lines (48 loc) · 1.68 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
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].strip()
else:
args = ''
if args.startswith('--add'):
# Add a quote
text = args.split(' ', 1)[1]
print('trying to add quote: "{}".'.format(text))
quote = Quote(text, comm['user'])
bot.factory.db.add(quote)
bot.say('Succesfully added quote.')
else:
# Deliver a quote
print('trying to repeat a quote')
index = random.randrange(0, bot.db.query(Quote).count())
quote = bot.factory.db.query(Quote)[index]
# Lame twisted irc doesn't support unicode.
bot.say(str(quote.text))
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()