Markov: randomly say something to a list of approved channels
check interval is every 10 minutes, rows in markov_chatter_target have a 1 in chance chance of leading to a line being generated, every 10 minutes. (so an interval of 144 = 10 min * 6 * 24 = one line per day, on average)
This commit is contained in:
parent
1e87fe59d8
commit
7220025f0a
@ -68,6 +68,8 @@ class Markov(Module):
|
||||
Module.__init__(self, irc, config, server)
|
||||
|
||||
self.next_shut_up_check = 0
|
||||
self.next_chatter_check = 0
|
||||
self.connection = None
|
||||
thread.start_new_thread(self.thread_do, ())
|
||||
|
||||
def db_init(self):
|
||||
@ -185,6 +187,20 @@ class Markov(Module):
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
if (version < 7):
|
||||
db = self.get_db()
|
||||
try:
|
||||
version = 7
|
||||
db.execute('''
|
||||
ALTER TABLE markov_chatter_target ADD COLUMN chance INTEGER NOT NULL DEFAULT 99999''')
|
||||
db.commit()
|
||||
db.close()
|
||||
self.db_register_module_version(self.__class__.__name__, version)
|
||||
except sqlite3.Error as e:
|
||||
db.rollback()
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
def register_handlers(self):
|
||||
"""Handle pubmsg/privmsg, to learn and/or reply to IRC events."""
|
||||
@ -298,8 +314,29 @@ class Markov(Module):
|
||||
|
||||
while not self.is_shutdown:
|
||||
self._do_shut_up_checks()
|
||||
self._do_random_chatter_check()
|
||||
time.sleep(1)
|
||||
|
||||
def _do_random_chatter_check(self):
|
||||
"""Randomly say something to a channel."""
|
||||
|
||||
# don't immediately potentially chatter, let the bot
|
||||
# join channels first
|
||||
if self.next_chatter_check == 0:
|
||||
self.next_chatter_check = time.time() + 600
|
||||
|
||||
if self.next_chatter_check < time.time():
|
||||
self.next_chatter_check = time.time() + 600
|
||||
|
||||
if self.connection is None:
|
||||
# i haven't seen any text yet...
|
||||
return
|
||||
|
||||
targets = self._get_chatter_targets()
|
||||
for t in targets:
|
||||
if random.randint(1, t['chance']) == 1:
|
||||
self.sendmsg(self.connection, t['target'], self._generate_line(t['target']))
|
||||
|
||||
def _do_shut_up_checks(self):
|
||||
"""Check to see if we've been talking too much, and shut up if so."""
|
||||
|
||||
@ -440,14 +477,10 @@ class Markov(Module):
|
||||
dbfile = self.config.get('dr.botzo', 'database')
|
||||
db = sqlite3.connect(dbfile)
|
||||
db.row_factory = sqlite3.Row
|
||||
query = 'SELECT target FROM markov_chatter_target'
|
||||
query = 'SELECT target, chance FROM markov_chatter_target'
|
||||
cursor = db.execute(query)
|
||||
results = cursor.fetchall()
|
||||
|
||||
for result in results:
|
||||
values.append(result['target'])
|
||||
|
||||
return values
|
||||
return results
|
||||
except sqlite3.Error as e:
|
||||
db.close()
|
||||
print('sqlite error: ' + str(e))
|
||||
|
Loading…
Reference in New Issue
Block a user