]> jfr.im git - erebus.git/blame - modules/userinfo.py
add load OK checking/logging to bot statup module loading
[erebus.git] / modules / userinfo.py
CommitLineData
36f91d21 1# Erebus IRC bot - Author: Erebus Team
2# trivia module
3# This file is released into the public domain; see http://unlicense.org/
4
5# module info
6modinfo = {
7 'author': 'Erebus Team',
8 'license': 'public domain',
9 'compatible': [1], # compatible module API versions
10 'depends': [], # other modules required to work properly?
11}
12
13# preamble
14import modlib
15lib = modlib.modlib(__name__)
16def modstart(parent_arg, *args, **kwargs):
17 global parent
18 parent = parent_arg
19 gotParent()
20 return lib.modstart(parent, *args, **kwargs)
21def modstop(*args, **kwargs):
22 closeshop()
23 return lib.modstop(*args, **kwargs)
24
25# module code
26import json
27
28#setup
29def gotParent():
30 global jsonfile, db
31 jsonfile = parent.cfg.get('userinfo', 'jsonpath', default="./modules/userinfo.json")
32 db = json.load(open(jsonfile, "r"))
33def closeshop():
34 if json is not None and json.dump is not None:
35 json.dump(db, open(jsonfile, "w"))#, indent=4, separators=(',', ': '))
36
37#functions
38def getauth(thing):
39 if isinstance(thing, parent.User):
40 if thing.auth is not None:
41 return "#"+thing.auth
42 elif isinstance(thing, basestring):
43 if thing[0] == "#":
44 return thing
45 else:
46 if parent.user(thing).auth is not None:
47 return "#"+parent.user(thing).auth
48 return None
49
50def has(user, key):
51 return (
52 key in db.get(getauth(user), {}) or
53 key in db.get(str(user).lower(), {})
54 )
55def get(user, key, default=None):
56 return (
57 db.get(getauth(user), {}).get(key,
58 db.get(str(user).lower(), {}).get(key,
59 default
60 )))
61def set(user, key, value):
62 if getauth(user) is not None: db.setdefault(getauth(user), {})[key] = value
63 db.setdefault(str(user).lower(), {})[key] = value
64
65#commands
66@lib.hook('get', needchan=False)
67def cmd_get(bot, user, chan, realtarget, *args):
b79721ee 68 if chan is not None and realtarget == chan.name: replyto = chan
36f91d21 69 else: replyto = user
70
71 if len(args) > 1:
72 target = args[0]
73 item = args[1]
74 else:
75 target = user
76 item = args[0]
77
78 value = get(target, item, None)
79 if value is None:
80 bot.msg(replyto, "%(user)s: %(item)s on %(target)s is not set." % {'user':user,'item':item,'target':target})
81 else:
82 bot.msg(replyto, "%(user)s: %(item)s on %(target)s: %(value)s" % {'user':user,'item':item,'target':target,'value':value})
83
84@lib.hook('set', needchan=False)
85@lib.argsGE(2)
86def cmd_set(bot, user, chan, realtarget, *args):
87 set(user, args[0], ' '.join(args[1:]))
88 bot.msg(user, "Done.")
89
90@lib.hook('oset', glevel=lib.STAFF, needchan=False)
91@lib.argsGE(3)
92def cmd_oset(bot, user, chan, realtarget, *args):
93 set(args[0], args[1], ' '.join(args[2:]))
94 bot.msg(user, "Done.")