]>
Commit | Line | Data |
---|---|---|
80d02bd8 | 1 | # Erebus IRC bot - Author: Erebus Team |
c695f740 | 2 | # trivia module |
80d02bd8 | 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', | |
a62d0d18 | 9 | 'compatible': [1,2], |
10 | 'depends': ['userinfo'], | |
11 | 'softdeps': ['help'], | |
80d02bd8 | 12 | } |
13 | ||
14 | # preamble | |
15 | import modlib | |
16 | lib = modlib.modlib(__name__) | |
17 | def modstart(parent, *args, **kwargs): | |
5871567f | 18 | state.gotParent(parent) |
19 | lib.hookchan(state.db['chan'])(trivia_checkanswer) # we need parent for this. so it goes here. | |
80d02bd8 | 20 | return lib.modstart(parent, *args, **kwargs) |
21 | def modstop(*args, **kwargs): | |
c0eee1b4 | 22 | global state |
8ef938d0 | 23 | try: |
24 | stop() | |
25 | state.closeshop() | |
26 | del state | |
27 | except Exception: pass | |
80d02bd8 | 28 | return lib.modstop(*args, **kwargs) |
29 | ||
30 | # module code | |
67faf5d7 | 31 | import json, random, threading, re, time, datetime, os |
b16b8c05 | 32 | |
c0eee1b4 | 33 | try: |
be8072b5 | 34 | import twitter |
c53f6514 | 35 | except: pass # doesn't matter if we don't have twitter, updating the status just will fall through the try-except if so... |
c0eee1b4 | 36 | |
b16b8c05 | 37 | def findnth(haystack, needle, n): #http://stackoverflow.com/a/1884151 |
38 | parts = haystack.split(needle, n+1) | |
39 | if len(parts)<=n+1: | |
40 | return -1 | |
41 | return len(haystack)-len(parts[-1])-len(needle) | |
80d02bd8 | 42 | |
00ccae72 | 43 | def person(num, throwindexerror=False): |
44 | try: | |
45 | return state.db['users'][state.db['ranks'][num]]['realnick'] | |
46 | except IndexError: | |
47 | if throwindexerror: | |
48 | raise | |
49 | else: | |
50 | return '' | |
51 | ||
3b89ebff | 52 | def pts(num): |
00ccae72 | 53 | try: |
54 | return str(state.db['users'][state.db['ranks'][num]]['points']) | |
55 | except IndexError: | |
56 | return 0 | |
2bb267e0 | 57 | |
00ccae72 | 58 | def country(num, default="??"): |
2bb267e0 | 59 | return lib.mod('userinfo')._get(person(num), 'country', default=default).upper() |
8ef938d0 | 60 | |
be8072b5 | 61 | class MyTimer(threading._Timer): |
62 | def __init__(self, *args, **kwargs): | |
63 | threading._Timer.__init__(self, *args, **kwargs) | |
64 | self.daemon = True | |
65 | ||
80d02bd8 | 66 | class TriviaState(object): |
5871567f | 67 | def __init__(self, parent=None, pointvote=False): |
68 | if parent is not None: | |
69 | self.gotParent(parent, pointvote) | |
70 | ||
71 | def gotParent(self, parent, pointvote=False): | |
bf8676ae | 72 | self.parent = parent |
73 | self.questionfile = self.parent.cfg.get('trivia', 'jsonpath', default="./modules/trivia.json") | |
74 | self.db = json.load(open(self.questionfile, "r")) | |
8ad3a154 | 75 | self.questions = self.db['questions'][self.db['category']] |
bf8676ae | 76 | self.chan = self.db['chan'] |
77 | self.curq = None | |
78 | self.nextq = None | |
79 | self.nextquestiontimer = None | |
80 | self.steptimer = None | |
81 | self.hintstr = None | |
82 | self.hintanswer = None | |
83 | self.hintsgiven = 0 | |
b16b8c05 | 84 | self.revealpossibilities = None |
bf8676ae | 85 | self.gameover = False |
86 | self.missedquestions = 0 | |
87 | self.curqid = None | |
88 | self.lastqid = None | |
80d02bd8 | 89 | |
00ccae72 | 90 | if 'lastwon' not in self.db or self.db['lastwon'] is None: |
91 | self.db['lastwon'] = time.time() | |
7bd5e7d5 | 92 | |
c53f6514 | 93 | if pointvote: |
94 | self.getchan().msg("Vote for the next round target points! Options: %s. Vote using !vote <choice>" % (', '.join([str(x) for x in self.db['targetoptions']]))) | |
95 | self.getchan().msg("You have %s seconds." % (self.db['votetimer'])) | |
96 | self.voteamounts = dict([(x, 0) for x in self.db['targetoptions']]) # make a dict {pointsoptionA: 0, pointsoptionB: 0, ...} | |
be8072b5 | 97 | self.pointvote = MyTimer(self.db['votetimer'], self.endPointVote) |
c53f6514 | 98 | self.pointvote.start() |
99 | else: | |
100 | self.pointvote = None | |
101 | ||
80d02bd8 | 102 | def __del__(self): |
77c61775 | 103 | self.closeshop() |
104 | def closeshop(self): | |
2bb487be | 105 | try: |
106 | self.steptimer.cancel() | |
107 | self.steptimer = None | |
108 | except: pass | |
109 | try: | |
110 | self.nextquestiontimer.cancel() | |
111 | self.nextquestiontimer = None | |
112 | except: pass | |
113 | #TODO remove if the replacement works | |
114 | # if threading is not None and threading._Timer is not None: | |
115 | # if isinstance(self.steptimer, threading._Timer): | |
116 | # self.steptimer.cancel() | |
117 | # if isinstance(self.nextquestiontimer, threading._Timer): | |
118 | # self.nextquestiontimer.cancel() | |
119 | # self.nextquestiontimer = None | |
120 | # self.savedb() | |
b9daa51a | 121 | |
67faf5d7 | 122 | def savedb(self): #returns whether or not it was able to save |
c695f740 | 123 | if json is not None and json.dump is not None: |
67faf5d7 | 124 | # json.dump(self.db, open(self.questionfile, "w"))#, indent=4, separators=(',', ': ')) |
125 | dbjson = json.dumps(self.db) | |
126 | if len(dbjson) > 0: | |
3f04e466 | 127 | os.rename(self.questionfile, self.questionfile+".auto.bak") |
67faf5d7 | 128 | tmpfn = os.tempnam('.', 'trivia') |
129 | try: | |
130 | f = open(tmpfn, "w") | |
131 | f.write(dbjson) | |
132 | f.close() | |
133 | os.rename(tmpfn, self.questionfile) | |
134 | return True | |
135 | except: #if something happens, restore the backup | |
3f04e466 | 136 | os.rename(self.questionfile+".auto.bak", self.questionfile) |
67faf5d7 | 137 | try: |
138 | os.unlink(tmpfn) | |
139 | except OSError: # temp file is already gone | |
140 | pass | |
141 | raise #TODO: we may be better off just swallowing exceptions? | |
142 | return False | |
80d02bd8 | 143 | |
fadbf980 | 144 | def getchan(self): |
145 | return self.parent.channel(self.chan) | |
146 | def getbot(self): | |
147 | return self.getchan().bot | |
148 | ||
b16b8c05 | 149 | def nexthint(self, hintnum): |
b16b8c05 | 150 | answer = self.hintanswer |
151 | ||
b5c89dfb | 152 | if self.hintstr is None or self.revealpossibilities is None or self.reveal is None: |
93ce52fd | 153 | oldhintstr = "" |
b16b8c05 | 154 | self.hintstr = list(re.sub(r'[a-zA-Z0-9]', '*', answer)) |
155 | self.revealpossibilities = range(''.join(self.hintstr).count('*')) | |
93ce52fd | 156 | self.reveal = int(round(''.join(self.hintstr).count('*') * (7/24.0))) |
157 | else: | |
158 | oldhintstr = ''.join(self.hintstr) | |
b16b8c05 | 159 | |
b5c89dfb | 160 | for i in range(self.reveal): |
b16b8c05 | 161 | revealcount = random.choice(self.revealpossibilities) |
162 | revealloc = findnth(''.join(self.hintstr), '*', revealcount) | |
163 | self.revealpossibilities.remove(revealcount) | |
164 | self.hintstr[revealloc] = answer[revealloc] | |
2bb267e0 | 165 | if oldhintstr != ''.join(self.hintstr): self.getchan().fastmsg("\00304,01Here's a hint: %s" % (''.join(self.hintstr))) |
b16b8c05 | 166 | |
77c61775 | 167 | self.hintsgiven += 1 |
168 | ||
c4763e66 | 169 | if hintnum < self.db['hintnum']: |
be8072b5 | 170 | self.steptimer = MyTimer(self.db['hinttimer'], self.nexthint, args=[hintnum+1]) |
b16b8c05 | 171 | self.steptimer.start() |
172 | else: | |
be8072b5 | 173 | self.steptimer = MyTimer(self.db['hinttimer'], self.nextquestion, args=[True]) |
b16b8c05 | 174 | self.steptimer.start() |
175 | ||
77c61775 | 176 | def doGameOver(self): |
7bd5e7d5 | 177 | msg = self.getchan().msg |
c0eee1b4 | 178 | winner = person(0) |
77c61775 | 179 | try: |
180 | msg("\00312THE GAME IS OVER!!!") | |
00ccae72 | 181 | msg("THE WINNER IS: %s (%s)" % (person(0, True), pts(0))) |
182 | msg("2ND PLACE: %s (%s)" % (person(1, True), pts(1))) | |
183 | msg("3RD PLACE: %s (%s)" % (person(2, True), pts(2))) | |
184 | [msg("%dth place: %s (%s)" % (i+1, person(i, True), pts(i))) for i in range(3,10)] | |
77c61775 | 185 | except IndexError: pass |
09566235 | 186 | except Exception as e: |
187 | msg("DERP! %r" % (e)) | |
c0eee1b4 | 188 | |
5f42c250 | 189 | self.db['lastwinner'] = winner |
190 | self.db['lastwon'] = time.time() | |
191 | ||
7bd5e7d5 | 192 | if self.db['hofpath'] is not None and self.db['hofpath'] != '': |
193 | self.writeHof() | |
194 | ||
77c61775 | 195 | self.db['users'] = {} |
196 | self.db['ranks'] = [] | |
197 | stop() | |
198 | self.closeshop() | |
c0eee1b4 | 199 | |
c53f6514 | 200 | try: |
c0eee1b4 | 201 | t = twitter.Twitter(auth=twitter.OAuth(self.getbot().parent.cfg.get('trivia', 'token'), |
202 | self.getbot().parent.cfg.get('trivia', 'token_sec'), | |
203 | self.getbot().parent.cfg.get('trivia', 'con'), | |
204 | self.getbot().parent.cfg.get('trivia', 'con_sec'))) | |
205 | t.statuses.update(status="Round is over! The winner was %s" % (winner)) | |
c53f6514 | 206 | except: pass #don't care if errors happen updating twitter. |
207 | ||
5871567f | 208 | self.__init__(self.parent, True) |
c53f6514 | 209 | |
7bd5e7d5 | 210 | def writeHof(self): |
211 | def person(num): | |
212 | try: return self.db['users'][self.db['ranks'][num]]['realnick'] | |
213 | except: return "none" | |
214 | def pts(num): | |
215 | try: return str(self.db['users'][self.db['ranks'][num]]['points']) | |
216 | except: return 0 | |
217 | ||
aaf381b8 | 218 | status = False |
7bd5e7d5 | 219 | try: |
220 | f = open(self.db['hofpath'], 'rb+') | |
221 | for i in range(self.db['hoflines']): #skip this many lines | |
222 | f.readline() | |
223 | insertpos = f.tell() | |
224 | fcontents = f.read() | |
225 | f.seek(insertpos) | |
226 | f.write((self.db['hofformat']+"\n") % { | |
227 | 'date': time.strftime("%F", time.gmtime()), | |
00ccae72 | 228 | 'duration': str(datetime.timedelta(seconds=time.time()-self.db['lastwon'])), |
7bd5e7d5 | 229 | 'targetscore': self.db['target'], |
230 | 'firstperson': person(0), | |
231 | 'firstscore': pts(0), | |
232 | 'secondperson': person(1), | |
233 | 'secondscore': pts(1), | |
234 | 'thirdperson': person(2), | |
235 | 'thirdscore': pts(2), | |
236 | }) | |
237 | f.write(fcontents) | |
aaf381b8 | 238 | status = True |
7bd5e7d5 | 239 | except Exception as e: |
aaf381b8 | 240 | status = False |
7bd5e7d5 | 241 | finally: |
242 | f.close() | |
aaf381b8 | 243 | return status |
7bd5e7d5 | 244 | |
c53f6514 | 245 | def endPointVote(self): |
246 | self.getchan().msg("Voting has ended!") | |
247 | votelist = sorted(self.voteamounts.items(), key=lambda item: item[1]) #sort into list of tuples: [(option, number_of_votes), ...] | |
248 | for i in range(len(votelist)-1): | |
249 | item = votelist[i] | |
250 | self.getchan().msg("%s place: %s (%s votes)" % (len(votelist)-i, item[0], item[1])) | |
251 | self.getchan().msg("Aaaaand! The next round will be to \002%s\002 points! (%s votes)" % (votelist[-1][0], votelist[-1][1])) | |
c0eee1b4 | 252 | |
c53f6514 | 253 | self.db['target'] = votelist[-1][0] |
254 | self.pointvote = None | |
77c61775 | 255 | |
38b29993 | 256 | self.nextquestion() #start the game! |
257 | ||
442ed923 | 258 | def nextquestion(self, qskipped=False, iteration=0, skipwait=False): |
7b832b55 | 259 | self.lastqid = self.curqid |
6bdfec48 | 260 | self.curq = None |
7b832b55 | 261 | self.curqid = None |
77c61775 | 262 | if self.gameover == True: |
263 | return self.doGameOver() | |
fadbf980 | 264 | if qskipped: |
be8072b5 | 265 | self.getchan().fastmsg("\00304Fail! The correct answer was: %s" % (self.hintanswer)) |
c0eee1b4 | 266 | self.missedquestions += 1 |
267 | else: | |
268 | self.missedquestions = 0 | |
f8cc0124 | 269 | if 'topicformat' in self.db and self.db['topicformat'] is not None: |
270 | self.getbot().conn.send("TOPIC %s" % (self.db['chan'])) | |
fadbf980 | 271 | |
b16b8c05 | 272 | if isinstance(self.steptimer, threading._Timer): |
273 | self.steptimer.cancel() | |
442ed923 | 274 | if isinstance(self.nextquestiontimer, threading._Timer): |
275 | self.nextquestiontimer.cancel() | |
276 | self.nextquestiontimer = None | |
c0eee1b4 | 277 | |
b16b8c05 | 278 | self.hintstr = None |
77c61775 | 279 | self.hintsgiven = 0 |
b16b8c05 | 280 | self.revealpossibilities = None |
b5c89dfb | 281 | self.reveal = None |
b16b8c05 | 282 | |
af349654 | 283 | self.savedb() |
284 | ||
c4763e66 | 285 | if self.missedquestions > self.db['maxmissedquestions']: |
c0eee1b4 | 286 | stop() |
ce03ceda | 287 | self.getbot().msg(self.getchan(), "%d questions unanswered! Stopping the game." % (self.missedquestions)) |
288 | return | |
b16b8c05 | 289 | |
442ed923 | 290 | if skipwait: |
6bdfec48 | 291 | self._nextquestion(iteration) |
442ed923 | 292 | else: |
be8072b5 | 293 | self.nextquestiontimer = MyTimer(self.db['questionpause'], self._nextquestion, args=[iteration]) |
442ed923 | 294 | self.nextquestiontimer.start() |
295 | ||
6bdfec48 | 296 | def _nextquestion(self, iteration): |
e5a3970b | 297 | if self.nextq is not None: |
2520caee | 298 | nextqid = None |
e5a3970b | 299 | nextq = self.nextq |
300 | self.nextq = None | |
c695f740 | 301 | else: |
8ad3a154 | 302 | nextqid = random.randrange(0, len(self.questions)) |
303 | nextq = self.questions[nextqid] | |
b5c89dfb | 304 | |
ebee6edb | 305 | if nextq[0][0] == "!": |
2520caee | 306 | nextqid = None |
b5c89dfb | 307 | nextq = specialQuestion(nextq) |
308 | ||
ebee6edb | 309 | if len(nextq) > 2 and nextq[2] - time.time() < 7*24*60*60 and iteration < 10: |
442ed923 | 310 | return self._nextquestion(iteration=iteration+1) #short-circuit to pick another question |
ebee6edb | 311 | if len(nextq) > 2: |
312 | nextq[2] = time.time() | |
313 | else: | |
314 | nextq.append(time.time()) | |
b5c89dfb | 315 | |
93ce52fd | 316 | if isinstance(nextq[1], basestring): |
317 | nextq[1] = nextq[1].lower() | |
318 | else: | |
319 | nextq[1] = [s.lower() for s in nextq[1]] | |
c695f740 | 320 | |
00ccae72 | 321 | qtext = "\00312,01Next up: " |
bf8676ae | 322 | qtext += "(%5d)" % (random.randint(0,99999)) |
ebee6edb | 323 | qary = nextq[0].split(None) |
71e0b5fb | 324 | qtext += " " |
c695f740 | 325 | for qword in qary: |
af349654 | 326 | spacer = random.choice( |
327 | range(0x61,0x7A) + ([0x20]*4) | |
328 | ) | |
329 | qtext += "\00304,01"+qword+"\00301,01"+chr(spacer) #a-z | |
be8072b5 | 330 | self.getbot().fastmsg(self.chan, qtext) |
80d02bd8 | 331 | |
b5c89dfb | 332 | self.curq = nextq |
7b832b55 | 333 | self.curqid = nextqid |
b5c89dfb | 334 | |
ebee6edb | 335 | if isinstance(self.curq[1], basestring): self.hintanswer = self.curq[1] |
336 | else: self.hintanswer = random.choice(self.curq[1]) | |
c0eee1b4 | 337 | |
be8072b5 | 338 | self.steptimer = MyTimer(self.db['hinttimer'], self.nexthint, args=[1]) |
b16b8c05 | 339 | self.steptimer.start() |
340 | ||
80d02bd8 | 341 | def checkanswer(self, answer): |
9557ee54 | 342 | if self.curq is None: |
343 | return False | |
ebee6edb | 344 | elif isinstance(self.curq[1], basestring): |
345 | return answer.lower() == self.curq[1] | |
80d02bd8 | 346 | else: # assume it's a list or something. |
ebee6edb | 347 | return answer.lower() in self.curq[1] |
77c61775 | 348 | |
c53f6514 | 349 | def addpoint(self, user_obj, count=1): |
350 | user_nick = str(user_obj) | |
351 | user = user_nick.lower() # save this separately as we use both | |
80d02bd8 | 352 | if user in self.db['users']: |
353 | self.db['users'][user]['points'] += count | |
354 | else: | |
c53f6514 | 355 | self.db['users'][user] = {'points': count, 'realnick': user_nick, 'rank': len(self.db['ranks'])} |
9557ee54 | 356 | self.db['ranks'].append(user) |
80d02bd8 | 357 | |
e5a3970b | 358 | self.db['ranks'].sort(key=lambda nick: self.db['users'][nick]['points'], reverse=True) #re-sort ranks, rather than dealing with anything more efficient |
6374d61f | 359 | for i in range(0, len(self.db['ranks'])): |
360 | nick = self.db['ranks'][i] | |
361 | self.db['users'][nick]['rank'] = i | |
77c61775 | 362 | |
e5a3970b | 363 | if self.db['users'][user]['points'] >= self.db['target']: |
77c61775 | 364 | self.gameover = True |
365 | ||
80d02bd8 | 366 | return self.db['users'][user]['points'] |
367 | ||
368 | def points(self, user): | |
9557ee54 | 369 | user = str(user).lower() |
80d02bd8 | 370 | if user in self.db['users']: |
371 | return self.db['users'][user]['points'] | |
372 | else: | |
373 | return 0 | |
374 | ||
375 | def rank(self, user): | |
c695f740 | 376 | user = str(user).lower() |
fadbf980 | 377 | if user in self.db['users']: |
378 | return self.db['users'][user]['rank']+1 | |
379 | else: | |
380 | return len(self.db['users'])+1 | |
77c61775 | 381 | |
c695f740 | 382 | def targetuser(self, user): |
77c61775 | 383 | if len(self.db['ranks']) == 0: return "no one is ranked!" |
384 | ||
c695f740 | 385 | user = str(user).lower() |
fadbf980 | 386 | if user in self.db['users']: |
387 | rank = self.db['users'][user]['rank'] | |
388 | if rank == 0: | |
389 | return "you're in the lead!" | |
390 | else: | |
391 | return self.db['ranks'][rank-1] | |
c695f740 | 392 | else: |
fadbf980 | 393 | return self.db['ranks'][-1] |
c695f740 | 394 | def targetpoints(self, user): |
77c61775 | 395 | if len(self.db['ranks']) == 0: return 0 |
396 | ||
c695f740 | 397 | user = str(user).lower() |
fadbf980 | 398 | if user in self.db['users']: |
399 | rank = self.db['users'][user]['rank'] | |
400 | if rank == 0: | |
af349654 | 401 | return "" |
fadbf980 | 402 | else: |
af349654 | 403 | return "("+str(self.db['users'][self.db['ranks'][rank-1]]['points'])+")" |
c695f740 | 404 | else: |
af349654 | 405 | return "("+str(self.db['users'][self.db['ranks'][-1]]['points'])+")" |
80d02bd8 | 406 | |
5871567f | 407 | state = TriviaState() |
9557ee54 | 408 | |
5871567f | 409 | # we have to hook this in modstart, since we don't know the channel until then. |
80d02bd8 | 410 | def trivia_checkanswer(bot, user, chan, *args): |
80d02bd8 | 411 | line = ' '.join([str(arg) for arg in args]) |
412 | if state.checkanswer(line): | |
6bdfec48 | 413 | state.curq = None |
93ce52fd | 414 | if state.hintanswer.lower() == line.lower(): |
af349654 | 415 | bot.fastmsg(chan, "\00312%s\003 has it! The answer was \00312%s\003. New score: %d. Rank: %d. Target: %s %s" % (user, line, state.addpoint(user), state.rank(user), state.targetuser(user), state.targetpoints(user))) |
93ce52fd | 416 | else: |
af349654 | 417 | bot.fastmsg(chan, "\00312%s\003 has it! The answer was \00312%s\003 (hinted answer: %s). New score: %d. Rank: %d. Target: %s%s" % (user, line, state.hintanswer, state.addpoint(user), state.rank(user), state.targetuser(user), state.targetpoints(user))) |
77c61775 | 418 | if state.hintsgiven == 0: |
6374d61f | 419 | bot.msg(chan, "\00312%s\003 got an extra point for getting it before the hints! New score: %d." % (user, state.addpoint(user))) |
9557ee54 | 420 | state.nextquestion() |
80d02bd8 | 421 | |
67faf5d7 | 422 | @lib.hook(glevel=1, needchan=False) |
423 | @lib.help(None, "saves the trivia database") | |
424 | def save(bot, user, chan, realtarget, *args): | |
425 | if chan is not None and realtarget == chan.name: replyto = chan | |
426 | else: replyto = user | |
427 | ||
428 | if state.savedb(): | |
429 | bot.msg(replyto, "Save successful.") | |
430 | else: | |
431 | bot.msg(replyto, "Save failed!") | |
432 | ||
fb20be7c | 433 | @lib.hook(needchan=False) |
5f03d045 | 434 | @lib.help("[<user>]", "shows how many points you or someone has") |
fb20be7c | 435 | def points(bot, user, chan, realtarget, *args): |
6bdfec48 | 436 | if chan is not None and realtarget == chan.name: replyto = chan |
80d02bd8 | 437 | else: replyto = user |
438 | ||
439 | if len(args) != 0: who = args[0] | |
440 | else: who = user | |
441 | ||
442 | bot.msg(replyto, "%s has %d points." % (who, state.points(who))) | |
443 | ||
fb20be7c | 444 | @lib.hook(glevel=lib.STAFF, needchan=False) |
5f03d045 | 445 | @lib.help("<user> [<amount>]", "gives someone points", "defaults to 1 point") |
80d02bd8 | 446 | @lib.argsGE(1) |
fb20be7c | 447 | def give(bot, user, chan, realtarget, *args): |
80d02bd8 | 448 | whoto = args[0] |
c695f740 | 449 | if len(args) > 1: |
450 | numpoints = int(args[1]) | |
451 | else: | |
452 | numpoints = 1 | |
453 | balance = state.addpoint(whoto, numpoints) | |
fadbf980 | 454 | |
c695f740 | 455 | bot.msg(chan, "%s gave %s %d points. New balance: %d" % (user, whoto, numpoints, balance)) |
456 | ||
fb20be7c | 457 | @lib.hook(glevel=1, needchan=False) |
5f03d045 | 458 | @lib.help("<qid>", "sets next question to one in the database") |
459 | @lib.argsEQ(1) | |
fb20be7c | 460 | def setnextid(bot, user, chan, realtarget, *args): |
9306587e | 461 | try: |
462 | qid = int(args[0]) | |
8ad3a154 | 463 | state.nextq = state.questions[qid] |
71e0b5fb | 464 | if user.glevel >= lib.STAFF: |
465 | respstr = "Done. Next question is: %s" % (state.nextq[0]) | |
466 | else: | |
467 | respstr = "Done." | |
468 | bot.msg(user, respstr) | |
9306587e | 469 | except Exception as e: |
470 | bot.msg(user, "Error: %s" % (e)) | |
471 | ||
fb20be7c | 472 | @lib.hook(glevel=lib.STAFF, needchan=False) |
5f03d045 | 473 | @lib.help("<q>*<a>", "sets next question to one not in database") |
c695f740 | 474 | @lib.argsGE(1) |
fb20be7c | 475 | def setnext(bot, user, chan, realtarget, *args): |
c695f740 | 476 | line = ' '.join([str(arg) for arg in args]) |
477 | linepieces = line.split('*') | |
b5c89dfb | 478 | if len(linepieces) < 2: |
479 | bot.msg(user, "Error: need <question>*<answer>") | |
480 | return | |
c695f740 | 481 | question = linepieces[0].strip() |
482 | answer = linepieces[1].strip() | |
ebee6edb | 483 | state.nextq = [question, answer] |
c695f740 | 484 | bot.msg(user, "Done.") |
485 | ||
fb20be7c | 486 | @lib.hook(glevel=1, needchan=False) |
5f03d045 | 487 | @lib.help(None, "skips to next question") |
fb20be7c | 488 | def skip(bot, user, chan, realtarget, *args): |
442ed923 | 489 | state.nextquestion(qskipped=True, skipwait=True) |
c695f740 | 490 | |
fb20be7c | 491 | @lib.hook(needchan=False) |
5f03d045 | 492 | @lib.help(None, "starts the trivia game") |
fb20be7c | 493 | def start(bot, user, chan, realtarget, *args): |
6bdfec48 | 494 | if chan is not None and realtarget == chan.name: replyto = chan |
c695f740 | 495 | else: replyto = user |
496 | ||
77a265cd | 497 | if chan is not None and chan.name != state.db['chan']: |
498 | bot.msg(replyto, "That command isn't valid here.") | |
499 | return | |
500 | ||
442ed923 | 501 | if state.curq is None and state.pointvote is None and state.nextquestiontimer is None: |
77a265cd | 502 | bot.msg(state.db['chan'], "%s has started the game!" % (user)) |
442ed923 | 503 | state.nextquestion(skipwait=True) |
c53f6514 | 504 | elif state.pointvote is not None: |
00ccae72 | 505 | bot.msg(user, "There's a vote in progress!") |
c695f740 | 506 | else: |
00ccae72 | 507 | bot.msg(user, "Game is already started!") |
c695f740 | 508 | |
9306587e | 509 | @lib.hook('stop', glevel=1, needchan=False) |
5f03d045 | 510 | @lib.help(None, "stops the trivia game") |
c695f740 | 511 | def cmd_stop(bot, user, chan, realtarget, *args): |
fadbf980 | 512 | if stop(): |
513 | bot.msg(state.chan, "Game stopped by %s" % (user)) | |
514 | else: | |
515 | bot.msg(user, "Game isn't running.") | |
c695f740 | 516 | |
fadbf980 | 517 | def stop(): |
3b89ebff | 518 | state.curq = None |
519 | state.nextq = None | |
09566235 | 520 | try: |
521 | state.steptimer.cancel() | |
522 | except Exception as e: | |
523 | print "!!! steptimer.cancel(): %s %r" % (e,e) | |
3b89ebff | 524 | state.steptimer = None |
09566235 | 525 | try: |
526 | state.nextquestiontimer.cancel() | |
527 | except Exception as e: | |
528 | print "!!! nextquestiontimer.cancel(): %s %r" % (e,e) | |
3b89ebff | 529 | state.nextquestiontimer = None |
530 | return True | |
80d02bd8 | 531 | |
fb20be7c | 532 | @lib.hook(needchan=False) |
5f03d045 | 533 | @lib.help("<reason>", "reports a bad question to the admins") |
7b832b55 | 534 | @lib.argsGE(1) |
fb20be7c | 535 | def badq(bot, user, chan, realtarget, *args): |
7b832b55 | 536 | lastqid = state.lastqid |
537 | curqid = state.curqid | |
538 | ||
539 | reason = ' '.join(args) | |
8ad3a154 | 540 | state.db['badqs'].append([state.db['category'], lastqid, curqid, reason]) |
7b832b55 | 541 | bot.msg(user, "Reported bad question.") |
2520caee | 542 | |
fb20be7c | 543 | @lib.hook(glevel=lib.STAFF, needchan=False) |
5f03d045 | 544 | @lib.help(None, "shows a list of BADQ reports") |
fb20be7c | 545 | def badqs(bot, user, chan, realtarget, *args): |
2520caee | 546 | if len(state.db['badqs']) == 0: |
547 | bot.msg(user, "No reports.") | |
548 | ||
549 | for i in range(len(state.db['badqs'])): | |
550 | try: | |
551 | report = state.db['badqs'][i] | |
8ad3a154 | 552 | bot.msg(user, "Report #%d: Cat=%s LastQ=%r CurQ=%r: %s" % (i, report[0], report[1], report[2], report[3])) |
553 | try: lq = state.db['questions'][report[0]][int(report[1])] | |
7b832b55 | 554 | except Exception as e: lq = (None,None) |
8ad3a154 | 555 | try: cq = state.db['questions'][report[0]][int(report[2])] |
7b832b55 | 556 | except Exception as e: cq = (None, None) |
557 | bot.msg(user, "- Last: %s*%s" % (lq[0], lq[1])) | |
558 | bot.msg(user, "- Curr: %s*%s" % (cq[0], cq[1])) | |
2520caee | 559 | except Exception as e: |
560 | bot.msg(user, "- Exception: %r" % (e)) | |
561 | ||
fb20be7c | 562 | @lib.hook(glevel=lib.STAFF, needchan=False) |
5f03d045 | 563 | @lib.hook(None, "clears list of BADQ reports") |
fb20be7c | 564 | def clearbadqs(bot, user, chan, realtarget, *args): |
2520caee | 565 | state.db['badqs'] = [] |
566 | bot.msg(user, "Cleared reports.") | |
567 | ||
fb20be7c | 568 | @lib.hook(glevel=lib.STAFF, needchan=False) |
5f03d045 | 569 | @lib.hook("<badqid>", "removes a BADQ report") |
2520caee | 570 | @lib.argsEQ(1) |
fb20be7c | 571 | def delbadq(bot, user, chan, realtarget, *args): |
4d98501a | 572 | try: |
573 | qid = int(args[0]) | |
574 | del state.db['badqs'][qid] | |
4d98501a | 575 | bot.msg(user, "Removed report #%d" % (qid)) |
576 | except: | |
577 | bot.msg(user, "Failed!") | |
2520caee | 578 | |
fb20be7c | 579 | @lib.hook(needchan=False) |
5f03d045 | 580 | @lib.help("[<user>]", "shows you or someone else's rank") |
fb20be7c | 581 | def rank(bot, user, chan, realtarget, *args): |
6bdfec48 | 582 | if chan is not None and realtarget == chan.name: replyto = chan |
80d02bd8 | 583 | else: replyto = user |
584 | ||
c695f740 | 585 | if len(args) != 0: who = args[0] |
586 | else: who = user | |
587 | ||
af349654 | 588 | bot.msg(replyto, "%s is in %d place (%s points). Target is: %s %s" % (who, state.rank(who), state.points(who), state.targetuser(who), state.targetpoints(who))) |
fadbf980 | 589 | |
fb20be7c | 590 | @lib.hook(needchan=False) |
5f03d045 | 591 | @lib.help(None, "shows top10 list") |
fb20be7c | 592 | def top10(bot, user, chan, realtarget, *args): |
77c61775 | 593 | if len(state.db['ranks']) == 0: |
594 | return bot.msg(state.db['chan'], "No one is ranked!") | |
fadbf980 | 595 | |
8ef938d0 | 596 | max = len(state.db['ranks']) |
597 | if max > 10: | |
598 | max = 10 | |
2bb267e0 | 599 | replylist = ', '.join(["%s (%s) %s" % (person(x), country(x), pts(x)) for x in range(max)]) |
00ccae72 | 600 | bot.msg(state.db['chan'], "Top 10: %s" % (replylist)) |
77c61775 | 601 | |
fb20be7c | 602 | @lib.hook(glevel=lib.ADMIN, needchan=False) |
5f03d045 | 603 | @lib.help("<target score>", "changes the target score for this round") |
fb20be7c | 604 | def settarget(bot, user, chan, realtarget, *args): |
77c61775 | 605 | try: |
606 | state.db['target'] = int(args[0]) | |
607 | bot.msg(state.db['chan'], "Target has been changed to %s points!" % (state.db['target'])) | |
38b29993 | 608 | |
7bd5e7d5 | 609 | if state.pointvote is not None: |
610 | state.pointvote.cancel() | |
611 | state.pointvote = None | |
38b29993 | 612 | bot.msg(state.db['chan'], "Vote has been cancelled!") |
7bd5e7d5 | 613 | except Exception as e: |
614 | print e | |
77c61775 | 615 | bot.msg(user, "Failed to set target.") |
616 | ||
fb20be7c | 617 | @lib.hook(needchan=False) |
5f03d045 | 618 | @lib.help("<option>", "votes for a trarget score for next round") |
fb20be7c | 619 | def vote(bot, user, chan, realtarget, *args): |
7bd5e7d5 | 620 | if state.pointvote is not None: |
38b29993 | 621 | if int(args[0]) in state.voteamounts: |
622 | state.voteamounts[int(args[0])] += 1 | |
623 | bot.msg(user, "Your vote has been recorded.") | |
624 | else: | |
625 | bot.msg(user, "Sorry - that's not an option!") | |
626 | else: | |
627 | bot.msg(user, "There's no vote in progress.") | |
628 | ||
fb20be7c | 629 | @lib.hook(glevel=lib.ADMIN, needchan=False) |
5f03d045 | 630 | @lib.help("<number>", "sets the max missed question before game stops") |
fb20be7c | 631 | def maxmissed(bot, user, chan, realtarget, *args): |
c4763e66 | 632 | try: |
633 | state.db['maxmissedquestions'] = int(args[0]) | |
634 | bot.msg(state.db['chan'], "Max missed questions before round ends has been changed to %s." % (state.db['maxmissedquestions'])) | |
635 | except: | |
636 | bot.msg(user, "Failed to set maxmissed.") | |
637 | ||
fb20be7c | 638 | @lib.hook(glevel=lib.ADMIN, needchan=False) |
5f03d045 | 639 | @lib.help("<seconds>", "sets the time between hints") |
fb20be7c | 640 | def hinttimer(bot, user, chan, realtarget, *args): |
c4763e66 | 641 | try: |
642 | state.db['hinttimer'] = float(args[0]) | |
643 | bot.msg(state.db['chan'], "Time between hints has been changed to %s." % (state.db['hinttimer'])) | |
644 | except: | |
645 | bot.msg(user, "Failed to set hint timer.") | |
646 | ||
fb20be7c | 647 | @lib.hook(glevel=lib.ADMIN, needchan=False) |
5f03d045 | 648 | @lib.help("<number>", "sets the number of hints given") |
fb20be7c | 649 | def hintnum(bot, user, chan, realtarget, *args): |
c4763e66 | 650 | try: |
651 | state.db['hintnum'] = int(args[0]) | |
652 | bot.msg(state.db['chan'], "Max number of hints has been changed to %s." % (state.db['hintnum'])) | |
653 | except: | |
654 | bot.msg(user, "Failed to set hintnum.") | |
655 | ||
fb20be7c | 656 | @lib.hook(glevel=lib.ADMIN, needchan=False) |
5f03d045 | 657 | @lib.help("<seconds>", "sets the pause between questions") |
fb20be7c | 658 | def questionpause(bot, user, chan, realtarget, *args): |
442ed923 | 659 | try: |
660 | state.db['questionpause'] = float(args[0]) | |
661 | bot.msg(state.db['chan'], "Pause between questions has been changed to %s." % (state.db['questionpause'])) | |
662 | except: | |
663 | bot.msg(user, "Failed to set questionpause.") | |
664 | ||
fb20be7c | 665 | @lib.hook(glevel=1, needchan=False) |
5f03d045 | 666 | @lib.help("<full question>", "finds a qid given a complete question") |
fb20be7c | 667 | def findq(bot, user, chan, realtarget, *args): |
8ad3a154 | 668 | args = list(args) |
669 | if args[0][0] == "@": | |
670 | cat = args.pop(0)[1:].lower() | |
671 | questions = state.db['questions'][cat] | |
672 | else: | |
673 | questions = state.questions | |
674 | ||
4d98501a | 675 | if len(args) == 0: |
676 | bot.msg(user, "You need to specify the question.") | |
677 | return | |
678 | ||
679 | searchkey = ' '.join(args).lower() | |
8ad3a154 | 680 | matches = [str(i) for i in range(len(questions)) if questions[i][0].lower() == searchkey] |
4d98501a | 681 | if len(matches) > 1: |
682 | bot.msg(user, "Multiple matches: %s" % (', '.join(matches))) | |
683 | elif len(matches) == 1: | |
684 | bot.msg(user, "One match: %s" % (matches[0])) | |
685 | else: | |
686 | bot.msg(user, "No match.") | |
687 | ||
688 | @lib.hook(glevel=1, needchan=False) | |
8ad3a154 | 689 | @lib.help("[@<category>] <regex>", "finds a qid given a regex or partial question") |
4d98501a | 690 | def findqre(bot, user, chan, realtarget, *args): |
8ad3a154 | 691 | args = list(args) |
692 | if args[0][0] == "@": | |
693 | cat = args.pop(0)[1:].lower() | |
694 | questions = state.db['questions'][cat] | |
695 | else: | |
696 | questions = state.questions | |
697 | ||
bf8676ae | 698 | if len(args) == 0: |
699 | bot.msg(user, "You need to specify a search string.") | |
700 | return | |
701 | ||
4d98501a | 702 | searcher = re.compile(' '.join(args), re.IGNORECASE) |
8ad3a154 | 703 | matches = [str(i) for i in range(len(questions)) if searcher.search(questions[i][0]) is not None] |
bf8676ae | 704 | if len(matches) > 25: |
705 | bot.msg(user, "Too many matches! (>25)") | |
706 | elif len(matches) > 1: | |
c4763e66 | 707 | bot.msg(user, "Multiple matches: %s" % (', '.join(matches))) |
708 | elif len(matches) == 1: | |
709 | bot.msg(user, "One match: %s" % (matches[0])) | |
710 | else: | |
711 | bot.msg(user, "No match.") | |
712 | ||
b9daa51a | 713 | @lib.hook(glevel=lib.STAFF, needchan=False) |
8ad3a154 | 714 | @lib.help("[@<category>] <qid>", "displays the q*a for a qid", "category defaults to current") |
b9daa51a | 715 | def showq(bot, user, chan, realtarget, *args): |
8ad3a154 | 716 | args = list(args) |
717 | if args[0][0] == "@": | |
718 | cat = args.pop(0)[1:].lower() | |
719 | questions = state.db['questions'][cat] | |
720 | else: | |
721 | questions = state.questions | |
722 | ||
b9daa51a | 723 | try: |
724 | qid = int(args[0]) | |
725 | except: | |
726 | bot.msg(user, "Specify a numeric question ID.") | |
727 | return | |
728 | try: | |
8ad3a154 | 729 | q = questions[qid] |
b9daa51a | 730 | except: |
731 | bot.msg(user, "ID not valid.") | |
732 | return | |
733 | bot.msg(user, "%s: %s*%s" % (qid, q[0], q[1])) | |
734 | ||
fb20be7c | 735 | @lib.hook(('delq', 'deleteq'), glevel=lib.STAFF, needchan=False) |
8ad3a154 | 736 | @lib.help("[@<category>] <qid>", "removes a question from the database") |
fb20be7c | 737 | def delq(bot, user, chan, realtarget, *args): |
8ad3a154 | 738 | args = list(args) |
739 | if args[0][0] == "@": | |
740 | cat = args.pop(0)[1:].lower() | |
741 | questions = state.db['questions'][cat] | |
742 | else: | |
743 | questions = state.questions | |
744 | ||
c4763e66 | 745 | try: |
8ad3a154 | 746 | backup = questions[int(args[0])] |
747 | del questions[int(args[0])] | |
ebee6edb | 748 | bot.msg(user, "Deleted %s*%s" % (backup[0], backup[1])) |
c4763e66 | 749 | except: |
b9daa51a | 750 | bot.msg(user, "Couldn't delete that question. %r" % (e)) |
c4763e66 | 751 | |
fb20be7c | 752 | @lib.hook(glevel=lib.STAFF, needchan=False) |
8ad3a154 | 753 | @lib.help("[@<category>] <q>*<a>", "adds a question") |
fb20be7c | 754 | def addq(bot, user, chan, realtarget, *args): |
8ad3a154 | 755 | args = list(args) |
756 | if args[0][0] == "@": | |
757 | cat = args.pop(0)[1:].lower() | |
758 | questions = state.db['questions'][cat] | |
759 | else: | |
760 | questions = state.questions | |
761 | ||
c4763e66 | 762 | line = ' '.join([str(arg) for arg in args]) |
763 | linepieces = line.split('*') | |
764 | if len(linepieces) < 2: | |
765 | bot.msg(user, "Error: need <question>*<answer>") | |
766 | return | |
767 | question = linepieces[0].strip() | |
768 | answer = linepieces[1].strip() | |
8ad3a154 | 769 | questions.append([question, answer]) |
770 | bot.msg(user, "Done. Question is #%s" % (len(questions)-1)) | |
c4763e66 | 771 | |
8ad3a154 | 772 | @lib.hook(glevel=1, needchan=False) |
773 | @lib.help("<category>", "change category") | |
774 | def setcat(bot, user, chan, realtarget, *args): | |
775 | category = args[0].lower() | |
776 | if category in state.db['questions']: | |
777 | state.db['category'] = category | |
778 | state.questions = state.db['questions'][category] | |
779 | bot.msg(user, "Changed category to %s" % (category)) | |
780 | else: | |
781 | bot.msg(user, "That category doesn't exist.") | |
782 | ||
783 | @lib.hook(needchan=False) | |
784 | @lib.help(None, "list categories") | |
785 | def listcats(bot, user, chan, realtarget, *args): | |
786 | cats = ["%s (%d)" % (c, len(state.db['questions'][c])) for c in state.db['questions'].keys()] | |
787 | bot.msg(user, "Categories: %s" % (', '.join(cats))) | |
788 | ||
789 | @lib.hook(glevel=lib.STAFF, needchan=False) | |
790 | @lib.help("<category>", "adds an empty category") | |
791 | def addcat(bot, user, chan, realtarget, *args): | |
792 | category = args[0].lower() | |
793 | if category not in state.db['questions']: | |
794 | state.db['questions'][category] = [] | |
795 | bot.msg(user, "Added category %s" % (category)) | |
796 | else: | |
797 | bot.msg(user, "Category already exists.") | |
798 | ||
799 | @lib.hook(glevel=lib.MANAGER, needchan=False) | |
800 | @lib.help("<category>", "deletes an entire category") | |
801 | def delcat(bot, user, chan, realtarget, *args): | |
802 | category = args[0].lower() | |
803 | if category == state.db['category']: | |
804 | bot.msg(user, "Category currently in use!") | |
805 | elif category in state.db['questions']: | |
806 | length = len(state.db['questions'][category]) | |
807 | del state.db['questions'][category] | |
808 | bot.msg(user, "Deleted category %s (%d questions)" % (category, length)) | |
809 | else: | |
810 | bot.msg(user, "Category does not exist.") | |
c4763e66 | 811 | |
fb20be7c | 812 | @lib.hook(needchan=False) |
813 | def triviahelp(bot, user, chan, realtarget, *args): | |
2bb267e0 | 814 | bot.slowmsg(user, "START") |
815 | bot.slowmsg(user, "TOP10") | |
816 | bot.slowmsg(user, "POINTS [<user>]") | |
817 | bot.slowmsg(user, "RANK [<user>]") | |
818 | bot.slowmsg(user, "BADQ <reason> (include info to identify question)") | |
9306587e | 819 | if user.glevel >= 1: |
b9daa51a | 820 | bot.slowmsg(user, "SKIP (KNOWN)") |
821 | bot.slowmsg(user, "STOP (KNOWN)") | |
4d98501a | 822 | bot.slowmsg(user, "FINDQ <full question> (KNOWN)") |
823 | bot.slowmsg(user, "FINDQRE <regex> (KNOWN)") | |
b9daa51a | 824 | bot.slowmsg(user, "SETNEXTID <qid> (KNOWN)") |
9306587e | 825 | if user.glevel >= lib.STAFF: |
b9daa51a | 826 | bot.slowmsg(user, "GIVE <user> [<points>] (STAFF)") |
827 | bot.slowmsg(user, "SETNEXT <q>*<a> (STAFF)") | |
828 | bot.slowmsg(user, "ADDQ <q>*<a> (STAFF)") | |
829 | bot.slowmsg(user, "DELQ <q>*<a> (STAFF) [aka DELETEQ]") | |
830 | bot.slowmsg(user, "SHOWQ <qid> (STAFF)") | |
831 | bot.slowmsg(user, "BADQS (STAFF)") | |
832 | bot.slowmsg(user, "CLEARBADQS (STAFF)") | |
833 | bot.slowmsg(user, "DELBADQ <reportid> (STAFF)") | |
9306587e | 834 | if user.glevel >= lib.ADMIN: |
b9daa51a | 835 | bot.slowmsg(user, "SETTARGET <points> (ADMIN)") |
836 | bot.slowmsg(user, "MAXMISSED <questions> (ADMIN)") | |
837 | bot.slowmsg(user, "HINTTIMER <float seconds> (ADMIN)") | |
838 | bot.slowmsg(user, "HINTNUM <hints> (ADMIN)") | |
839 | bot.slowmsg(user, "QUESTIONPAUSE <float seconds> (ADMIN)") | |
b5c89dfb | 840 | |
6374d61f | 841 | @lib.hooknum(417) |
842 | def num_417(bot, textline): | |
93ce52fd | 843 | # bot.fastmsg(state.db['chan'], "Whoops, it looks like that question didn't quite go through! (E:417). Let's try another...") |
442ed923 | 844 | state.nextquestion(qskipped=False, skipwait=True) |
6374d61f | 845 | |
f8cc0124 | 846 | @lib.hooknum(332) |
847 | def num_TOPIC(bot, textline): | |
848 | pieces = textline.split(None, 4) | |
849 | chan = pieces[3] | |
850 | if chan != state.db['chan']: | |
851 | return | |
852 | gottopic = pieces[4][1:] | |
853 | ||
854 | formatted = state.db['topicformat'] % { | |
855 | 'chan': state.db['chan'], | |
8ef938d0 | 856 | 'top1': "%s (%s)" % (person(0), pts(0)), |
857 | 'top3': '/'.join([ | |
858 | "%s (%s)" % (person(x), pts(x)) | |
859 | for x in range(3) if x < len(state.db['ranks']) | |
860 | ]), | |
861 | 'top3c': ' '.join([ | |
862 | "%s (%s, %s)" % (person(x), pts(x), country(x)) | |
863 | for x in range(3) if x < len(state.db['ranks']) | |
864 | ]), | |
865 | 'top10': ' '.join([ | |
866 | "%s (%s)" % (person(x), pts(x)) | |
867 | for x in range(10) if x < len(state.db['ranks']) | |
868 | ]), | |
869 | 'top10c': ' '.join([ | |
870 | "%s (%s, %s)" % (person(x), pts(x), country(x)) | |
871 | for x in range(10) if x < len(state.db['ranks']) | |
872 | ]), | |
5f42c250 | 873 | 'lastwinner': state.db['lastwinner'], |
874 | 'lastwon': time.strftime("%b %d", time.gmtime(state.db['lastwon'])), | |
f8cc0124 | 875 | 'target': state.db['target'], |
876 | } | |
877 | if gottopic != formatted: | |
878 | state.getbot().conn.send("TOPIC %s :%s" % (state.db['chan'], formatted)) | |
879 | ||
b5c89dfb | 880 | |
881 | def specialQuestion(oldq): | |
ebee6edb | 882 | newq = [oldq[0], oldq[1]] |
883 | qtype = oldq[0].upper() | |
b5c89dfb | 884 | |
885 | if qtype == "!MONTH": | |
ebee6edb | 886 | newq[0] = "What month is it currently (in UTC)?" |
887 | newq[1] = time.strftime("%B", time.gmtime()).lower() | |
b5c89dfb | 888 | elif qtype == "!MATH+": |
889 | randnum1 = random.randrange(0, 11) | |
890 | randnum2 = random.randrange(0, 11) | |
ebee6edb | 891 | newq[0] = "What is %d + %d?" % (randnum1, randnum2) |
892 | newq[1] = spellout(randnum1+randnum2) | |
b5c89dfb | 893 | return newq |
894 | ||
895 | def spellout(num): | |
896 | return [ | |
00ccae72 | 897 | "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", |
b5c89dfb | 898 | "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", |
899 | "sixteen", "seventeen", "eighteen", "nineteen", "twenty" | |
900 | ][num] |