]>
Commit | Line | Data |
---|---|---|
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 | |
6 | modinfo = { | |
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 | |
14 | import modlib | |
15 | lib = modlib.modlib(__name__) | |
16 | def modstart(parent_arg, *args, **kwargs): | |
17 | global parent | |
18 | parent = parent_arg | |
19 | gotParent() | |
20 | return lib.modstart(parent, *args, **kwargs) | |
21 | def modstop(*args, **kwargs): | |
22 | closeshop() | |
23 | return lib.modstop(*args, **kwargs) | |
24 | ||
25 | # module code | |
26 | import json | |
27 | ||
28 | #setup | |
29 | def gotParent(): | |
30 | global jsonfile, db | |
31 | jsonfile = parent.cfg.get('userinfo', 'jsonpath', default="./modules/userinfo.json") | |
32 | db = json.load(open(jsonfile, "r")) | |
33 | def 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 | |
38 | def 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 | ||
fb20be7c | 50 | def _has(user, key): |
36f91d21 | 51 | return ( |
52 | key in db.get(getauth(user), {}) or | |
53 | key in db.get(str(user).lower(), {}) | |
54 | ) | |
fb20be7c | 55 | def _get(user, key, default=None): |
36f91d21 | 56 | return ( |
fb20be7c | 57 | db.get(getauth(user), {}). #try to get the auth |
58 | get(key, #try to get the info-key by auth | |
59 | db.get(str(user).lower(), {}). #fallback to using the nick | |
60 | get(key, #and try to get the info-key from that | |
61 | default #otherwise throw out whatever default | |
36f91d21 | 62 | ))) |
fb20be7c | 63 | def _set(user, key, value): |
64 | if getauth(user) is not None: | |
65 | db.setdefault(getauth(user), {})[key] = value #use auth if we can | |
66 | db.setdefault(str(user).lower(), {})[key] = value #but set nick too | |
36f91d21 | 67 | |
68 | #commands | |
fb20be7c | 69 | @lib.hook(needchan=False) |
70 | def getinfo(bot, user, chan, realtarget, *args): | |
b79721ee | 71 | if chan is not None and realtarget == chan.name: replyto = chan |
36f91d21 | 72 | else: replyto = user |
73 | ||
74 | if len(args) > 1: | |
75 | target = args[0] | |
76 | item = args[1] | |
77 | else: | |
78 | target = user | |
79 | item = args[0] | |
80 | ||
fb20be7c | 81 | value = _get(target, item, None) |
36f91d21 | 82 | if value is None: |
83 | bot.msg(replyto, "%(user)s: %(item)s on %(target)s is not set." % {'user':user,'item':item,'target':target}) | |
84 | else: | |
85 | bot.msg(replyto, "%(user)s: %(item)s on %(target)s: %(value)s" % {'user':user,'item':item,'target':target,'value':value}) | |
86 | ||
fb20be7c | 87 | @lib.hook(needchan=False) |
36f91d21 | 88 | @lib.argsGE(2) |
fb20be7c | 89 | def setinfo(bot, user, chan, realtarget, *args): |
90 | _set(user, args[0], ' '.join(args[1:])) | |
36f91d21 | 91 | bot.msg(user, "Done.") |
92 | ||
fb20be7c | 93 | @lib.hook(glevel=lib.STAFF, needchan=False) |
36f91d21 | 94 | @lib.argsGE(3) |
fb20be7c | 95 | def osetinfo(bot, user, chan, realtarget, *args): |
96 | _set(args[0], args[1], ' '.join(args[2:])) | |
36f91d21 | 97 | bot.msg(user, "Done.") |