def checkfacts(self, bot, room, user, args): if room is not None and not room.allowed('facts'): return False body = args.group() response = None count = None if room is not None and room["_id"] in self.triggered and (self.triggered[room["_id"]]['count'] > 3 and datetime.datetime.now() - self.triggered[room["_id"]]['time'] < min_repeat): return for trigger in self.trigger_cache.values(): check = trigger['rex'].search(body) if check is not None: for factid in trigger['facts']: fact = db.facts.find_one({'_id': factid}) match = trigger['rex'].match(body) if trigger['triggered'] is not None and (match is None or match.group() != body): delta = datetime.datetime.now() - trigger['triggered'] if delta < min_repeat: print "Would have spouted fact {0} but was too soon (absolute)".format(str(fact['triggers'])) continue elif (delta - min_repeat) < datetime.timedelta(seconds=random.randrange(1, (max_repeat - min_repeat).seconds)): print "Would have spouted fact {0} but was too soon (random)".format(str(fact['triggers'])) continue if count is None or fact['count'] < count: response = fact triggered = trigger count = fact['count'] if response is None: return False if room is not None: if room["_id"] not in self.triggered: self.triggered[room["_id"]] = {'count': 1, 'time': datetime.datetime.now()} else: if datetime.datetime.now() - self.triggered[room["_id"]]['time'] < short_time: self.triggered[room["_id"]] = {'count': self.triggered[room["_id"]]['count'] + 1, 'time': datetime.datetime.now()} else: self.triggered[room["_id"]] = {'count': 1, 'time': datetime.datetime.now()} response['count'] = response['count'] + 1 response['triggered'] = datetime.datetime.now() triggered['triggered'] = response['triggered'] db.facts.update({'_id': response['_id']}, response) factoid = random.choice(response['factoids']) try: what = args.group(1) except IndexError: what = itemmodule.getSomething() reply = factoid["reply"].replace('$who', user['nick']).replace('$what', what) while '$something' in reply: reply = reply.replace('$something', itemmodule.getSomething(), 1) while '$someone' in reply: if room is None: reply = reply.replace('$someone', "someone", 1) else: reply = reply.replace('$someone', random.choice(room.roster.values())['nick'], 1) while '$inventory' in reply: reply = reply.replace('$inventory', itemmodule.getPossession(), 1) while '$takeitem' in reply: reply = reply.replace('$takeitem', itemmodule.takeItem(), 1) while '$giveitem' in reply: reply = reply.replace('$giveitem', itemmodule.giveItem(), 1) if room is not None: room.send(reply, delay=True) room["factSpouted"] = {"fact": response, "trigger": triggered["original"], "factoid": factoid} room.save() if self.triggered[room["_id"]]['count'] > 3: room.send("/me sparks and sputters. He's shorted out!", 5) print "shorted out until " + str(self.triggered[room["_id"]]['time'] + min_repeat) else: user.send(reply, delay=True) return True
def checkfacts(self, bot, room, user, args): if room is not None and not room.allowed('facts'): return False body = args.group() response = None triggered = None count = None for trigger in self.trigger_cache.values(): check = trigger['rex'].search(body) if check is not None: for factid in trigger['facts']: fact = db.facts.find_one({'_id': factid}) match = trigger['rex'].match(body) if trigger['triggered'] is not None and (match is None or match.group() != body): delta = (datetime.datetime.now() - trigger['triggered']).total_seconds() if delta < min_repeat: print "Would have spouted fact {0} but was too soon (absolute)".format(str(fact['triggers'])) continue elif (delta - min_repeat) > random.randrange(1, max_repeat - min_repeat): print "Would have spouted fact {0} but was too soon (random)".format(str(fact['triggers'])) continue if count is None or fact['count'] < count: response = fact triggered = trigger count = fact['count'] if response is None: return False response['count'] = response['count'] + 1 db.facts.update({'_id': response['_id']}, response) triggered['triggered'] = datetime.datetime.now() reply = random.choice(response['factoids'])['reply'] try: what = args.group('what') except IndexError: what = itemmodule.getSomething() reply = reply.replace('$who', user['nick']).replace('$what', what) while '$something' in reply: reply = reply.replace('$something', itemmodule.getSomething(), 1) while '$someone' in reply: if room is None: reply = reply.replace('$someone', "someone", 1) else: reply = reply.replace('$someone', random.choice(room.roster.values())['nick'], 1) while '$inventory' in reply: reply = reply.replace('$inventory', itemmodule.getPossession(), 1) while '$takeitem' in reply: reply = reply.replace('$takeitem', itemmodule.takeItem(), 1) while '$giveitem' in reply: reply = reply.replace('$giveitem', itemmodule.giveItem(), 1) if room is not None: room.send(reply, delay=True) else: user.send(reply, delay=True) return True