From 6a58864641cb9c1c02176fa9d33e70305ad6d9f4 Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Wed, 5 Oct 2016 21:37:12 +0100 Subject: [PATCH] Announcements can be paused, resumed or scheduled ASAP. --- bot.py | 42 +++++++++++++++++++++++++++++++++++++++--- db.py | 20 ++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/bot.py b/bot.py index e5b6d86..5bb5e2e 100755 --- a/bot.py +++ b/bot.py @@ -733,7 +733,7 @@ def can_manage_announcements(member, channel, command, **args): log.debug('Member {} on server {} can show announcement {}.'.format(member.name, server.name, announcement['id'])) return announcement - if command in ['delete']: + if command in ['delete', 'schedule']: if announcement is not None: server = client.get_server(announcement['server_id']) announcer = server.get_member(announcement['member_id']) @@ -801,6 +801,8 @@ def show_announcement(message, id): if offset <= now: if announcement['interval']: text += ' scheduled for {}'.format(iso8601(offset + announcement['interval'])) + if announcement['probability'] < 0: + text += ' paused' text += '**' lines.append(text) @@ -827,8 +829,8 @@ def show_announcement(message, id): text += ' voice <#{}>'.format(announcement['voice_id']) if announcement['sound']: text += ' play "{}"'.format(announcement['sound']) - if announcement['probability'] < 1.0: - text += ' probability {}'.format(announcement['probability']) + if abs(announcement['probability']) < 1.0: + text += ' probability {}'.format(abs(announcement['probability'])) if announcement['message']: text += ':' @@ -853,6 +855,34 @@ def delete_announcement(message, id): yield from say(message.channel, 'yelp!') @asyncio.coroutine +def schedule_announcement(message, id, **args): + announcement = yield from can_manage_announcements(message.author, message.channel, 'schedule', id = id) + if not announcement: + return + + update = {} + + # Pause announcement. + if 'pause' in args: + if args['pause']: + update['probability'] = -abs(announcement['probability']) + else: + update['probability'] = abs(announcement['probability']) + + # ASAP. + if 'asap' in args: + update['last_spoke'] = None + + if dryrun: + log.info('Not updating announcement {}: {}'.format(id, update)) + else: + log.info('Updating announcement {}: {}'.format(id, update)) + if db.update_announcement(client, id, **update): + yield from say(message.channel, 'purr') + else: + yield from say(message.channel, 'yelp!') + +@asyncio.coroutine def manage_announcements(message, command, raw): log.debug('Command: {}'.format(raw)) @@ -873,6 +903,12 @@ def manage_announcements(message, command, raw): yield from show_announcement(message, args[1]) elif args[0] == 'delete': yield from delete_announcement(message, args[1]) + elif args[0] == 'asap': + yield from schedule_announcement(message, args[1], asap = True) + elif args[0] == 'pause': + yield from schedule_announcement(message, args[1], pause = True) + elif args[0] == 'resume': + yield from schedule_announcement(message, args[1], pause = False) @asyncio.coroutine def announce(announcement): diff --git a/db.py b/db.py index 01575e0..f87e0d4 100644 --- a/db.py +++ b/db.py @@ -109,6 +109,26 @@ class DBConnection(object): self.dbh.commit() self.close_db() + def update_announcement(self, client, id, **args): + values = [] + params = [] + for k, v in args.items(): + values.append(k) + params.append(v) + params.append(client.user.id) + params.append(id) + sql = 'update announcements set {} where bot_id=? and id=?'.format(', '.join(['{}=?'.format(v) for v in values])) + cursor = self.query(sql, params) + if cursor.rowcount: + self.dbh.commit() + ret = True + else: + log.error('No such announcement {} for {}'.format(id, client.user.name)) + self.dbh.rollback() + ret = False + self.close_db() + return ret + def delete_announcement(self, client, id): cursor = self.query('delete from announcements where bot_id=? and id=?', [client.user.id, id]) if cursor.rowcount: -- 2.7.4