def command(self, bot, comm, groups): print "intercepted addcard command" desc = groups[0] color = groups[1].replace('"', "") if color not in ["white", "black"]: return bot.reply(comm, "That color card doesn't exist!") elif color == "black": underscore_re = re.compile("(_+)+") formatted, num_replacements = underscore_re.subn("_" * 10, desc) if num_replacements == 0 or num_replacements > 3: return bot.reply(comm, "[*] You provided too few or many blanks!") desc = ude(self.plugin.init_black(formatted)) self.plugin.black_discard.append(desc) elif color == "white": desc = ude(self.plugin.format_white(desc)) self.plugin.white_discard.append(desc) new_card = CardTable(desc=desc, color=color, official=False) self.plugin.db.session.add(new_card) self.plugin.db.session.commit() return bot.reply(comm, "[*] Card: {0} Color: {1} added to db!".format(desc, color))
def try_add_factoid(self, bot, comm): if not comm['directed']: return msg = comm['message'].strip() match = re.match(r'learn(?: that)? (.+)\s+(\w+)\s+<(\w+)>\s+(.*)', msg) if not match: return if not bot.acl.has_permission(comm, 'factoid'): bot.reply(comm, "I cannot learn new things") return trigger, type_, action, response = match.groups() trigger = trigger.strip() if action not in ['say', 'reply', 'me']: bot.reply(comm, "I don't know the action {0}.".format(action)) return if type_ not in ['is', 'triggers']: bot.reply(comm, "I don't the type {0}.".format(type_)) return self.db.session.add(Factoid(ude(trigger), type_, action, ude(response))) self.db.session.commit() bot.reply(comm, 'OK, {user}'.format(**comm)) return True
def try_forget_factoid_mass(self, bot, comm): if not comm['directed']: return msg = ude(comm['message'].strip()) match = re.match(r'forget all about (.+)', msg) if not match: return if not bot.acl.has_permission(comm, 'factoid'): bot.reply(comm, "Never Forget!") return trigger = match.groups()[0] factoids = (self.db.session.query(Factoid).filter( Factoid.trigger == ude(trigger)).all()) if len(factoids) == 0: bot.reply(comm, "I don't have anything like that.") return for factoid in factoids: self.db.session.delete(factoid) self.db.session.commit() bot.reply(comm, 'Done, {user}'.format(**comm)) return True
def try_add_factoid(self, bot, comm): if not comm['directed']: return msg = comm['message'].strip() match = re.match(r'learn(?: that)? (.+)\s+(\w+)\s+<(\w+)>\s+(.*)', msg) if not match: return if not bot.acl.has_permission(comm, 'factoid'): bot.reply(comm, "I cannot learn new things") return trigger, type_, action, response = match.groups() trigger = trigger.strip() if action not in ['say', 'reply', 'me']: bot.reply(comm, "I don't know the action {0}.".format(action)) return if type_ not in ['is', 'triggers']: bot.reply(comm, "I don't the type {0}.".format(type_)) return self.db.session.add( Factoid(ude(trigger), type_, action, ude(response)) ) self.db.session.commit() bot.reply(comm, 'OK, {user}'.format(**comm)) return True
def try_forget_factoid_mass(self, bot, comm): if not comm['directed']: return msg = ude(comm['message'].strip()) match = re.match(r'forget all about (.+)', msg) if not match: return if not bot.acl.has_permission(comm, 'factoid'): bot.reply(comm, "Never Forget!") return trigger = match.groups()[0] factoids = (self.db.session.query(Factoid) .filter(Factoid.trigger == ude(trigger)) .all()) if len(factoids) == 0: bot.reply(comm, "I don't have anything like that.") return for factoid in factoids: self.db.session.delete(factoid) self.db.session.commit() bot.reply(comm, 'Done, {user}'.format(**comm))
def userQuit(self, bot, user, quitMessage): # Go through every log we have for this user and set their most recent # doing to (Quiting with message 'quitMessage') logs = self.db.session.query(SeenTable).filter( SeenTable.user == ude(user)) logs.update({ 'doing': '(Quiting) with message "%s"' % ude(quitMessage), 'seen': datetime.now() }) return super(Seen, self).userQuit(bot, user, quitMessage)
def record(self, channel, user, doing): logs = self.queryUser(channel, user) if logs.count(): # Because exists() doesn't exist? log = logs.first() log.seen = datetime.now() log.doing = ude(doing) else: self.db.session.add( SeenTable(channel, user, datetime.now(), ude(doing))) self.db.session.commit()
def userQuit(self, bot, user, quitMessage): # Go through every log we have for this user and set their most recent # doing to (Quiting with message 'quitMessage') logs = self.db.session.query(SeenTable).filter( SeenTable.user == ude(user) ) logs.update({ 'doing': '(Quiting) with message "%s"' % ude(quitMessage), 'seen': datetime.now() }) return super(Seen, self).userQuit(bot, user, quitMessage)
def record(self, channel, user, doing): logs = self.queryUser(channel, user) if logs.count(): # Because exists() doesn't exist? log = logs.first() log.seen = datetime.now() log.doing = ude(doing) else: self.db.session.add( SeenTable(channel, user, datetime.now(), ude(doing)) ) self.db.session.commit()
def update_db(self, giver, receiverkarma): """ Record a the giver of karma, the receiver of karma, and the karma amount. Typically the count will be 1, but it can be any positive or negative integer. """ for receiver in receiverkarma: if receiver != giver: urow = KarmaStatsTable(ude(giver), ude(receiver), receiverkarma[receiver]) self.db.session.add(urow) self.db.session.commit()
def command(self, bot, comm, groups): # The receiver (or in old terms, user) of the karma being tallied receiver = ude(groups[0].strip().lower()) # Manage both tables sesh = bot.factory.loader.db.session # Old Table kt = sesh.query(KarmaTable) user = kt.filter(KarmaTable.user == receiver).first() # New Table kst = sesh.query(KarmaStatsTable) kst_list = kst.filter(KarmaStatsTable.receiver == receiver).all() # The total amount of karma from both tables total = 0 # Add karma from the old table if user: total += user.kcount # Add karma from the new table if kst_list: for row in kst_list: total += row.kcount # Send the message bot.reply(comm, '%s has %d points' % (uen(receiver), total), encode=False)
def update_db(self, userkarma, username): """ Change the users karma by the karma amount (either 1 or -1) """ kt = self.db.session.query(KarmaTable) for user in userkarma: if user != username: # Modify the db accourdingly urow = kt.filter(KarmaTable.user == ude(user)).first() # If the user doesn't exist, create it if not urow: urow = KarmaTable(ude(user)) urow.kcount += userkarma[user] self.db.session.add(urow) self.db.session.commit()
def try_respond_to_factoid(self, bot, comm): msg = ude(comm['message'].strip()) if comm['directed']: msg = '!' + msg factoids = (self.db.session.query(Factoid) .filter(Factoid.trigger == msg) .all()) if len(factoids) == 0: factoids = (self.db.session.query(Factoid) .filter(Factoid.type == 'triggers') .all()) factoids = filter(lambda f: f.trigger in msg, factoids) if len(factoids) == 0: return factoid = random.choice(factoids) if factoid.action == 'say': bot.reply(comm, factoid.response) return True elif factoid.action == 'reply': bot.reply(comm, '{}: {}'.format(comm['user'], factoid.response)) return True elif factoid.action == 'me': bot.me(comm, factoid.response) return True else: bot.reply(comm, 'Um, what is the verb {}?'.format(factoid.action))
def try_respond_to_factoid(self, bot, comm): msg = ude(comm['message'].strip()) if comm['directed']: msg = '!' + msg factoids = (self.db.session.query(Factoid).filter( Factoid.trigger == msg).all()) if len(factoids) == 0: factoids = (self.db.session.query(Factoid).filter( Factoid.type == 'triggers').all()) factoids = filter(lambda f: f.trigger in msg, factoids) if len(factoids) == 0: return factoid = random.choice(factoids) if factoid.action == 'say': bot.reply(comm, factoid.response) return True elif factoid.action == 'reply': bot.reply(comm, '{}: {}'.format(comm['user'], factoid.response)) return True elif factoid.action == 'me': bot.me(comm, factoid.response) return True else: bot.reply(comm, 'Um, what is the verb {}?'.format(factoid.action))
def command(self, bot, comm, groups): # The receiver (or in old terms, user) of the karma being tallied receiver = ude(groups[0].strip().lower()) # Manage both tables sesh = bot.factory.loader.db.session # Old Table kt = sesh.query(KarmaTable) user = kt.filter(KarmaTable.user == receiver).first() # New Table kst = sesh.query(KarmaStatsTable) kst_list = kst.filter(KarmaStatsTable.receiver == receiver).all() # The total amount of karma from both tables total = 0 # Add karma from the old table if user: total += user.kcount # Add karma from the new table if kst_list: for row in kst_list: total += row.kcount # Pluralization points = "points" if total == 1 or total == -1: points = "point" # Send the message bot.reply(comm, "%s has %d %s" % (uen(receiver), total, points), encode=False)
def message(self, bot, comm): msg = ude(comm['message'].strip()).lower() if self.is_question.search(msg): if comm['directed']: self.sortq(bot, comm, msg) elif random.random() < .1: self.sortq(bot, comm, msg) return False
def message(self, bot, comm): msg = ude(comm['message'].strip()) if comm['directed'] and msg.endswith('?'): r = random.random() for resp, prob in self.responses: r -= prob if r < 0: bot.reply(comm, '{0}: {1}'.format(comm['user'], resp)) return True return False
def message(self, bot, comm): msg = ude(comm['message'].strip()) if comm['directed'] and self.is_question.search(msg): r = random.random() for resp, prob in self.responses: r -= prob if r < 0: bot.reply(comm, '{0}: {1}'.format(comm['user'], resp)) return True return False
def message(self, bot, comm): msg = ude(comm['message']).lower() for f in unfunnies: if f in msg: if comm['directed'] or random.random() > .6: self.laughfor(bot, comm) for c in chuckles: if c in msg: if len(msg.strip()) < 6 or random.random() < .8: self.laughalong() return False
def flush_db(self): """ Clear out old cards, and bring in new ones. """ self.db.session.query(CardTable).filter_by(official=True).delete() url = "http://web.engr.oregonstate.edu/~johnsdea/" whites_txt = urllib2.urlopen(url + "whites.txt").read().split("\n") blacks_txt = urllib2.urlopen(url + "blacks.txt").read().split("\n") new_whites = map(self.format_white, whites_txt) for white in new_whites: if white: self.db.session.add(CardTable(ude(white), "white")) new_blacks = map(self.init_black, blacks_txt) for black in new_blacks: if black: self.db.session.add(CardTable(ude(black), "black")) self.db.session.commit()
def message(self, bot, comm): msg = ude(comm['message'].strip()) prefix = self.discusses_food(msg) if prefix: if comm['directed']: # always reply on question or comment to self about food self.foodyreply(bot, comm) elif random.random() < .7: # often interject anyways self.foodyreply(bot, comm, prefix) return True return False
def command(self, bot, comm, groups): # Play nice when the user isn't in the db kt = bot.factory.loader.db.session.query(KarmaTable) thing = ude(groups[0].strip().lower()) user = kt.filter(KarmaTable.user == thing).first() if user: bot.reply(comm, '%s has %d points' % (uen(user.user), user.kcount), encode=False) else: bot.reply(comm, 'No karma for %s ' % uen(thing), encode=False)
def message(self, bot, comm): msg = ude(comm['message'].strip()) if comm['directed'] and self.is_question.search(msg): r = random.random() if " should " in msg: replies = self.advices else: replies = self.responses for resp, prob in replies: r -= prob if r < 0: bot.reply(comm, '{0}: {1}'.format(comm['user'], resp)) return True return False
def command(self, bot, comm, groups): # Play nice when the user isn't in the db kt = bot.factory.loader.db.session.query(KarmaTable) thing = ude(groups[0].strip().lower()) user = kt.filter(KarmaTable.user == thing).first() if user: bot.reply( comm, '%s has %d points' % (uen(user.user), user.kcount), encode=False ) else: bot.reply( comm, 'No karma for %s ' % uen(thing), encode=False )
def message(self, bot, comm): msg = ude(comm['message'].strip()) if comm['directed']: if not '?' in msg: if random.random() < .3: self.inform(bot, comm) else: self.contemplate(bot, comm) return True elif random.random() < .0001: # Occasionally pipe up if random.random() < .2: self.inform(bot, comm) else: self.contemplate(bot, comm) return True return False
def command(self, bot, comm, groups): if groups[0].isspace(): return name = groups[0].strip() if name.lower() == bot.nickname.lower(): bot.reply(comm, 'I am always here!') logs = self.plugin.queryUser(comm['channel'], name) if not logs.count(): bot.reply(comm, 'I have not seen %s' % ude(name), encode=True) else: log = logs.first() time_format = 'at %I:%M %p on %b-%d' seen = log.seen.strftime(time_format) bot.reply(comm, 'I observed %s %s -- %s' % (name, seen, log.doing))
def command(self, bot, comm, groups): if groups[0].isspace(): return name = groups[0].strip() if name.lower() == bot.nickname.lower(): bot.reply(comm, 'I am always here!') logs = self.plugin.queryUser(comm['channel'], name) if not logs.count(): bot.reply(comm, 'I have not seen %s' % ude(name), encode=True) else: log = logs.first() time_format = 'at %I:%M %p on %b-%d' seen = log.seen.strftime(time_format) bot.reply( comm, 'I observed %s %s -- %s' % (name, seen, log.doing) )
def queryUser(self, channel, user): return (self.db.session.query(SeenTable).filter( SeenTable.channel == channel).filter(SeenTable.user == ude(user)))
def queryUser(self, channel, user): return (self.db.session.query(SeenTable) .filter(SeenTable.channel == channel) .filter(SeenTable.user == ude(user)))