-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquote.py
More file actions
63 lines (51 loc) · 1.73 KB
/
quote.py
File metadata and controls
63 lines (51 loc) · 1.73 KB
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
import os
import random
from errbot import BotPlugin, botcmd
def add_quote(args):
location = os.path.dirname(__file__)
quote_file = "{}/data/quotes.txt".format(location)
quote = args[0].strip()
attribute = args[1].strip()
with open(quote_file, 'a') as fh:
fh.write("{};{}\n".format(quote, attribute))
def get_file_entries(filename):
with open(filename) as fh:
for count, line in enumerate(fh):
pass
return count
def get_quote():
location = os.path.dirname(__file__)
quote_file = "{}/data/quotes.txt".format(location)
quote_return = dict()
number_of_quotes = get_file_entries(quote_file)
if number_of_quotes == 0:
quote_num = 0
else:
number_of_quotes = number_of_quotes + 1
quote_num = random.randrange(number_of_quotes)
with open(quote_file, 'r') as fh:
for count, line in enumerate(fh):
if count == quote_num:
quote = line
elif count > quote_num:
break
quote = quote.rstrip()
split_value = quote.split(';')
quote_return['quote'] = split_value[0]
quote_return['attribution'] = split_value[1]
return quote_return
class Quote(BotPlugin):
"""An Err plugin for getting a quote"""
min_err_version = '3.2.1'
max_err_version = '3.2.1'
@botcmd(split_args_with=None, template='quote_display')
def quote(self, mess, args):
"""Get a quote from the database and display it"""
return get_quote()
@botcmd(split_args_with=';')
def quote_add(self, mess, args):
"""Add a quote to the database"""
if len(args) != 2:
return "Usage: !quote <quote>, <attribution>"
add_quote(args)
return "Quote added"