1 # Erebus IRC bot - Author: Erebus Team
2 # simple module example
3 # This file is released into the public domain; see http://unlicense.org/
7 'author': 'Erebus Team',
8 'license': 'public domain',
9 'compatible': [1], # compatible module API versions
10 'depends': [], # other modules required to work properly?
15 lib
= modlib
.modlib(__name__
)
16 def modstart(parent
, *args
, **kwargs
):
18 return lib
.modstart(parent
, *args
, **kwargs
)
19 def modstop(*args
, **kwargs
):
22 return lib
.modstop(*args
, **kwargs
)
27 class TriviaState(object):
28 def __init__(self
, questionfile
):
29 self
.questionfile
= questionfile
30 self
.db
= json
.load(open(questionfile
, "r"))
31 self
.chan
= self
.db
['chan']
35 json
.dump(self
.db
, open(self
.questionfile
, "w"), indent
=4, separators
=(',',': '))
37 def nextquestion(self
):
38 nextq
= random
.choice(self
.db
['questions'])
40 self
.parent
.channel(self
.chan
).bot
.msg(self
.chan
, "Next up: %s" % (nextq
['question']))
42 def checkanswer(self
, answer
):
45 elif isinstance(self
.curq
['answer'], basestring
):
46 return answer
.lower() == self
.curq
['answer']
47 else: # assume it's a list or something.
48 return answer
.lower() in self
.curq
['answer']
50 def addpoint(self
, _user
, count
=1):
53 if user
in self
.db
['users']:
54 self
.db
['users'][user
]['points'] += count
56 self
.db
['users'][user
] = {'points': count, 'realnick': _user, 'rank': len(self.db['ranks'])}
57 self
.db
['ranks'].append(user
)
59 oldrank
= self
.db
['users'][user
]['rank']
61 nextperson
= self
.db
['ranks'][oldrank
-1]
62 if self
.db
['users'][user
]['points'] > self
.db
['users'][nextperson
]['points']:
63 self
.db
['ranks'][oldrank
-1] = user
64 self
.db
['ranks'][oldrank
] = nextperson
68 return self
.db
['users'][user
]['points']
70 def points(self
, user
):
71 user
= str(user
).lower()
72 if user
in self
.db
['users']:
73 return self
.db
['users'][user
]['points']
79 return self
.db
['users'][user
]['rank']
81 def targetuser(self
, user
): return "TODO" #TODO
82 def targetpoints(self
, user
): return 0 #TODO
84 state
= TriviaState("/home/jrunyon/erebus/modules/trivia.json") #TODO
86 @lib.hookchan(state
.db
['chan'])
87 def trivia_checkanswer(bot
, user
, chan
, *args
):
88 line
= ' '.join([str(arg
) for arg
in args
])
89 bot
.msg('dimecadmium', line
)
90 if state
.checkanswer(line
):
91 bot
.msg(chan
, "\00308%s\003 has it! The answer was \00308%s\003. Current points: %d. Rank: %d. Target: %s (%d)." % (user
, line
, state
.addpoint(user
), state
.rank(user
), state
.targetuser(user
), state
.targetpoints(user
)))
95 def cmd_points(bot
, user
, chan
, realtarget
, *args
):
96 if chan
is not None: replyto
= chan
99 if len(args
) != 0: who
= args
[0]
102 bot
.msg(replyto
, "%s has %d points." % (who
, state
.points(who
)))
104 @lib.hook('give', clevel
=lib
.OP
)
106 def cmd_give(bot
, user
, chan
, realtarget
, *args
):
107 if len(args
) > 1 and args
[1] != 1:
108 bot
.msg(user
, "Giving more than one point is not yet implemented.")
109 return NotImplemented
112 balance
= state
.addpoint(whoto
)
113 bot
.msg(chan
, "%s gave %s %d points. New balance: %d" % (user
, whoto
, 1, balance
))
117 def cmd_rank(bot
, user
, chan
, realtarget
, *args
):
118 if chan
is not None: replyto
= chan
121 bot
.msg(replyto
, "%s is in %d place." % (args
[0], state
.rank(args
[0])))