class DBConnection(object):
def __init__(self, filename = 'bot.sqlite'):
+ self.filename = filename
self.ONLINE_AVATAR = 'avatar_online.png'
self.IDLE_AVATAR = 'avatar_idle.png'
- self.open_db(filename)
+ self.open_db()
self.create_tables()
+ self.close_db()
def log_level(self, level):
log.setLevel(level)
def now(self):
return int(time.time())
- def open_db(self, filename):
+ def open_db(self, filename = None):
+ if filename is None:
+ filename = self.filename
self.dbh = sqlite3.connect(filename)
self.dbh.row_factory = sqlite3.Row
cursor = self.query('create table if not exists announcements (id char(36) not null, bot_id varchar(32) not null, server_id varchar(32) not null, member_id varchar(32) not null, channel_id varchar(32) not null, voice_id varchar(32), sound varchar(128), mention varchar(32), start_date datetime, end_date datetime, interval int, probability float not null default 1.0, last_spoke datetime, message text, digest char(56))')
self.dbh.commit()
- def close(self):
- self.dbh.close()
+ def close_db(self):
+ if self.dbh is not None:
+ self.dbh.close()
+ self.dbh = None
def query(self, sql, params = ()):
+ if self.dbh is None:
+ self.open_db()
log.debug(sql, params)
cursor = self.dbh.cursor()
cursor.execute(sql, params)
def get_state(self, client):
cursor = self.query('select avatar, idle, last_spoke from state where bot_id=?', [client.user.id])
row = cursor.fetchone()
+ self.close_db()
if row is None:
return None
return dict(row)
if not cursor.rowcount:
cursor = self.query('insert into state (id, bot_id) values (?, ?)', [self.uuid(), client.user.id])
cursor = self.query(sql, params)
+ self.close_db()
self.dbh.commit()
def get_all_announcements(self, client, servers = []):
cursor = self.query(sql, params)
for row in cursor.fetchall():
yield dict(row)
+ self.close_db()
def get_announcement(self, client, id):
cursor = self.query('select * from announcements where bot_id=? and id=?', [client.user.id, id])
row = cursor.fetchone()
if row:
- return dict(row)
+ announcement = dict(row)
else:
- return None
+ announcement = None
+ self.close_db()
+ return annoucement
def get_announcements(self, client):
now = self.now()
cursor = self.query('select id, server_id, channel_id, voice_id, sound, mention, start_date, end_date, interval, probability, last_spoke, message, digest from announcements where (start_date is null or start_date <= ?) and (end_date is null or end_date > ? or end_date=start_date) and (last_spoke is null or last_spoke < ? - interval) and bot_id=?', [now, now, now, client.user.id])
for row in cursor.fetchall():
yield dict(row)
+ self.close_db()
def set_announcement(self, client, id, digest):
now = self.now()
cursor = self.query('update announcements set last_spoke=?, digest=? where id=?', [now, digest, id])
self.dbh.commit()
+ self.close_db()
def delete_announcement(self, client, id):
cursor = self.query('delete from announcements where bot_id=? and id=?', [client.user.id, id])
- if not cursor.rowcount:
+ if cursor.rowcount:
+ self.dbh.commit()
+ ret = True
+ else:
log.error('No such announcement {} for {}'.format(id, client.user.name))
- self.dbh.rollback()
- return False
- self.dbh.commit()
- return True
+ ret = False
+ self.close_db()
+ return ret