-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathduncan.py
113 lines (85 loc) · 2.75 KB
/
duncan.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
import os
import json
import random
import datetime
import shutil
def iso():
return datetime.datetime.now().isoformat()
def str2bool(v):
return (v.lower() in ("yes", "true", "t", "1"))
class Duncan(object):
def __init__(self):
self.data = {}
ddir = "data"
if not os.path.exists:
os.makedirs(ddir)
dfile = "data/data.json"
self._prefix = "."
if not os.path.exists(dfile):
shutil.copy("data/data.default.json", dfile)
with open(dfile) as f:
data = json.loads(f.read())
#else:
# print("file does not exist")
# data = {}
self._debug = False
if 'debug' in data:
self._debug = data['debug']
#self._debug = True
self.data = data
self.dfile = dfile
def eightball(self, author, channel, text):
dat = self.data['eightball']
return random.choice(dat)
def debug(self, author, channel, text):
self._debug = not self._debug
return("debug: " + str(self._debug))
def greet(self, author, channel, text):
return "Hello, %s" % author
def grab(self, author, channel, text):
return "Unfortunately .grab is not implemented yet. (workin on it)"
def dune(self, author, channel, text):
return random.choice(self.data['quotes']['dune'])
def decide(self, author, channel, text):
if text.strip() == '':
return "you didn't give me any options"
ch = text.split(",")
ch = [x.strip() for x in ch]
return random.choice(ch)
def last(self, author, channel, text):
return "UNIMPLEMENTED TRAP"
def fixme(self, author, channel, text):
return "https://github.com/vaporstack/d2"
def _common_list(self, author, channel, text, key, msg):
if text.strip() == '':
if key not in self.data:
return msg
if text.strip() == '':
return random.choice(self.data[key])['text']
if key in self.data:
arr = self.data[key]
else:
arr = []
rec = {}
rec['author'] = author
rec['text'] = text
rec['timestamp'] = iso()
arr.append(rec)
self.data[key] = arr
return "Added. %s [%d]" % (key, len(arr))
def lunch(self, author, channel, text):
return self._common_list(author, channel, text, "lunch", "can't tell you what's for lunch, no lunch options!\n(use `%slunch <some type of meal> to add one" % self._prefix)
def joke(self, author, channel, text):
return self._common_list(author, channel, text, "joke", "can't tell you a joke, I don't know any jokes.\n(use `%sjoke <some type of humor> to add one" % self._prefix)
def _list(self):
funcs = dir(self)
funcs = [x for x in funcs if "_" not in x]
funcs = [x for x in funcs if x != "data" and x != "dfile" and x != "stop"]
return funcs
def list(self, author, channel, text):
return "Commands: " + ", ".join(self._list())
def stop(self):
# write stuff
self.data['debug'] = self._debug
with open(self.dfile, 'w') as f:
f.write(json.dumps(self.data, indent=4))