ASAP overrides schedule.
authorCMDR furrycat <elite@furrycat.net>
Fri, 7 Oct 2016 10:25:08 +0000 (11:25 +0100)
committerCMDR furrycat <elite@furrycat.net>
Fri, 7 Oct 2016 10:25:08 +0000 (11:25 +0100)
bot.py
db.py

diff --git a/bot.py b/bot.py
index 8c79598..074c4ca 100755 (executable)
--- a/bot.py
+++ b/bot.py
@@ -839,6 +839,8 @@ def show_announcement(message, id):
   if offset <= now:
     if announcement['interval']:
       text += ' scheduled for {}'.format(iso8601(offset + announcement['interval']))
+  if announcement['asap']:
+    text += ' will be given ASAP'
   if announcement['probability'] < 0:
     text += ' paused'
   text += '**'
@@ -1069,7 +1071,7 @@ def schedule_announcement(message, id, **args):
 
   # ASAP.
   if 'asap' in args:
-    update['last_spoke'] = None
+    update['asap'] = 'true'
 
   if dryrun:
     log.info('Not updating announcement {}: {}'.format(id, update))
@@ -1115,7 +1117,11 @@ def manage_announcements(message, command, raw):
 def announce(announcement):
   # Adhere to schedule.
   last_spoke = None
-  if announcement['interval']:
+  asap = None
+  if announcement['asap']:
+    log.info('Announcement {} was requested ASAP'.format(announcement['id']))
+    asap = 'false'
+  elif announcement['interval']:
     if announcement['start_date'] or announcement['last_spoke']:
       now = int(time.time())
       if announcement['start_date']:
@@ -1149,7 +1155,7 @@ def announce(announcement):
   if channel is None:
     log.warning("Can't get channel for announcement {}".format(announcement['id']))
     # Set last_spoke so we don't spam.
-    db.set_announcement(client, announcement['id'], digest = announcement['digest'] if 'digest' in announcement else None, last_spoke = last_spoke)
+    db.set_announcement(client, announcement['id'], digest = announcement['digest'] if 'digest' in announcement else None, last_spoke = last_spoke, asap = asap)
     return
 
   voice_only = announcement['voice_id'] == announcement['channel_id']
@@ -1195,7 +1201,7 @@ def announce(announcement):
       else:
         log.info("Didn't bother with announcement {}".format(announcement['id']))
       # Set last_spoke even if we chose not to announce.
-      db.set_announcement(client, announcement['id'], digest = digest, last_spoke = last_spoke)
+      db.set_announcement(client, announcement['id'], digest = digest, last_spoke = last_spoke, asap = asap)
       if not announce:
         return
       if announcement['voice_id']:
diff --git a/db.py b/db.py
index 409d7fa..fddeab3 100644 (file)
--- a/db.py
+++ b/db.py
@@ -33,7 +33,7 @@ class DBConnection(object):
     cursor = self.query('create table if not exists state (id char(36) not null, bot_id varchar(32) not null, avatar varchar(128), idle boolean not null default false, last_spoke datetime)')
     cursor = self.query('create unique index if not exists state_id on state (id)')
     cursor = self.query('create unique index if not exists state_bot_id on state (bot_id)')
-    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))')
+    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, asap boolean not null default false, message text, digest char(56))')
     cursor = self.query('create unique index if not exists announcements_id on announcements (id)')
     self.dbh.commit()
     self.close_db()
@@ -101,15 +101,15 @@ class DBConnection(object):
 
   def get_announcements(self, client):
     now = self.now()
-    cursor = self.query('select id, server_id, member_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 (last_spoke is null or interval > 0) and bot_id=?', [now, now, now, client.user.id])
+    cursor = self.query('select id, server_id, member_id, channel_id, voice_id, sound, mention, start_date, end_date, interval, probability, last_spoke, asap, 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 (asap=1 or last_spoke is null or last_spoke < ? - interval) and (last_spoke is null or interval > 0) 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 = None, last_spoke = None):
+  def set_announcement(self, client, id, digest = None, last_spoke = None, asap = 'asap'):
     if last_spoke is None:
       last_spoke = self.now()
-    cursor = self.query('update announcements set last_spoke=?, digest=? where id=?', [last_spoke, digest, id])
+    cursor = self.query('update announcements set last_spoke=?, asap=?, digest=? where id=?', [last_spoke, asap, digest, id])
     self.dbh.commit()
     self.close_db()