From 3969661b6c325fe9e8d59b098c9c6a8ae95b64df Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Wed, 2 Nov 2016 15:04:04 +0000 Subject: [PATCH] Attach artwork to announcements. --- plugin/announcements/announcements.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/plugin/announcements/announcements.py b/plugin/announcements/announcements.py index a41bb7e..1e0e01e 100644 --- a/plugin/announcements/announcements.py +++ b/plugin/announcements/announcements.py @@ -22,7 +22,7 @@ class Announcements(DBConnection): def create_tables(self): self.open_db() - 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 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), attachment 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)') cursor = self.query('create index if not exists announcements_bot_id on announcements (bot_id)') self.dbh.commit() @@ -88,7 +88,7 @@ class Announcements(DBConnection): 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, 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='true' 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]) + cursor = self.query("select id, server_id, member_id, channel_id, voice_id, sound, attachment, 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='true' 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() @@ -212,6 +212,9 @@ class Announcements(DBConnection): '```sound FILE```', "File to play. I won't play sounds in private messages!", '', + '```attachment FILE```', + "File to attach to the message.", + '', 'I also accept some shortcuts:', '', "`tell me` means to mention you in the announcement. If you don't specify a channel with `in` the announcement will be sent in a private message.", @@ -302,6 +305,8 @@ class Announcements(DBConnection): text += ' sound {}'.format(announcement['sound']) if announcement['voice_id'] != announcement['channel_id']: text += ' in <#{}>'.format(announcement['voice_id']) + if announcement['attachment']: + text += ' attachment {}'.format(announcement['attachment']) if announcement['message']: short = announcement['message'][:100] if short != announcement['message']: @@ -381,6 +386,8 @@ class Announcements(DBConnection): text += ' voice <#{}>'.format(announcement['voice_id']) if announcement['sound']: text += ' sound "{}"'.format(announcement['sound']) + if announcement['attachment']: + text += ' attachment "{}"'.format(announcement['attachment']) if abs(announcement['probability']) < 1.0: text += ' probability {}'.format(abs(announcement['probability'])) @@ -410,7 +417,7 @@ class Announcements(DBConnection): @asyncio.coroutine def parse_announcement(self, message, raw, editing = False): # We want to split by colon but don't want to count any which are part of dates or URLs. - m = re.match(r'(?:[^:]*\s+(?:(?:at|from|to)\s+"?\d\d\d\d-?\d\d-?\d\d(?:T|\s+)\d\d:?\d\d:?\d\dZ?"?|sound\s+https?:\S+))+', raw, re.IGNORECASE) + m = re.match(r'(?:[^:]*\s+(?:(?:at|from|to)\s+"?\d\d\d\d-?\d\d-?\d\d(?:T|\s+)\d\d:?\d\d:?\d\dZ?"?|(?:attachment|sound)\s+https?:\S+))+', raw, re.IGNORECASE) if m is not None: parts = raw[len(m.group(0)):].split(':') parts[0] = m.group(0) + parts[0] @@ -549,6 +556,14 @@ class Announcements(DBConnection): else: parsed['sound'] = param ok = True + elif arg == 'attachment': + k = arg + if editing and param == 'none': + parsed[k] = None + ok = True + else: + parsed[k] = param + ok = True elif arg == 'probability': parsed['probability'] = param ok = True @@ -781,7 +796,11 @@ class Announcements(DBConnection): self.update_announcement(client, announcement['id'], **update) return - voice_only = announcement['voice_id'] == announcement['channel_id'] + attachment = None + if announcement['attachment']: + attachment = bot.parse_attachment(announcement['attachment']) + + voice_only = attachment is None and announcement['voice_id'] == announcement['channel_id'] already_posted = False if not voice_only: # @everyone mention mangles the digest @@ -830,7 +849,7 @@ class Announcements(DBConnection): yield from bot.wake_up() else: log.info('Posting announcement {} to {}: {}'.format(announcement['id'], channel, text)) - message = yield from bot.say(channel, text) + message = yield from bot.say(channel, text, attachment = attachment) update['digest'] = bot.digest(message.content) else: if announcement['probability'] < 0: -- 2.7.4