From 0a3806ca9eb7a3f6b799a3215719d78ade78d278 Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Fri, 18 Nov 2016 17:07:53 +0000 Subject: [PATCH] Use Announcement class in announcements. --- plugin/announcements/announcements.py | 54 +++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/plugin/announcements/announcements.py b/plugin/announcements/announcements.py index beccaad..12b7a31 100644 --- a/plugin/announcements/announcements.py +++ b/plugin/announcements/announcements.py @@ -16,6 +16,19 @@ class Announcement(object): def __init__(self, row): for k, v in dict(row).items(): setattr(self, k, v) + self.is_private = hasattr(self, 'channel_id') and self.channel_id == 'private' + self.has_attachment = hasattr(self, 'attachment') and bool(self.attachment) + self.has_sound = bool(self.sound) + self.has_text = bool(self.text) + self.voice_only = hasattr(self, 'channel_id') and hasattr(self, 'voice_id') and self.channel_id == self.voice_id and not self.has_attachment + + def owned_by(self, owner): + try: + # owner is a User object. + return self.member_id == owner.id + except AttributeError: + # owner is a string. + return self.member_id == owner class Announcements(DBConnection): def __init__(self, filename = None): @@ -129,10 +142,10 @@ class Announcements(DBConnection): # Anyone on the server can show details of an announcement. if command == 'show': if announcement is not None: - if announcement.member_id == author.id: + if announcement.owned_by(author): log.debug('Anyone can show own announcements.') return announcement - if announcement.channel_id != 'private': + if not announcement.is_private: server = client.get_server(announcement.server_id) if server is not None: member = server.get_member(author.id) @@ -145,7 +158,7 @@ class Announcements(DBConnection): if announcement.member_id == author.id: log.debug('Anyone can manage own announcements.') return announcement - if announcement.channel_id != 'private': + if not announcement.is_private: server = client.get_server(announcement.server_id) announcer = server.get_member(announcement.member_id) announcer_role = bot.highest_role(announcer.roles) @@ -160,8 +173,8 @@ class Announcements(DBConnection): return announcement if command == 'create': - if announcement.channel_id == 'private': - log.debug('Anyone can schedule a private announcement.') + if announcement.is_private: + log.debug('Anyone can create a private announcement.') return announcement server = client.get_server(announcement.server_id) member = server.get_member(announcement.member_id) @@ -290,17 +303,17 @@ class Announcements(DBConnection): results = [] for announcement in self.get_all_announcements(client, self.get_message_servers(client, message, True)): - if announcement.channel_id == 'private' and announcement.member_id != message.author.id: + if announcement.is_private and not announcement.owned_by(message.author): continue text = 'announcement **{}**'.format(announcement.id) - if announcement.channel_id == 'private': + if announcement.is_private: text += ' in private' else: server = client.get_server(announcement.server_id) member = server.get_member(announcement.member_id) text += ' by {}'.format(member.name) if announcement.channel_id: - if announcement.channel_id != 'private': + if not announcement.is_private: text += ' in <#{}>'.format(announcement.channel_id) if announcement.voice_id: if announcement.sound: @@ -328,7 +341,7 @@ class Announcements(DBConnection): return lines = [] - if announcement.channel_id == 'private': + if announcement.is_private: for server in client.servers: try: member = server.get_member(announcement.member_id) @@ -337,7 +350,7 @@ class Announcements(DBConnection): else: server = client.get_server(announcement.server_id) member = server.get_member(announcement.member_id) - text = '**{}announcement {} by {}'.format('private ' if announcement.channel_id == 'private' else '', id, member.name if member is not None else announcement.member_id) + text = '**{}announcement {} by {}'.format('private ' if announcement.is_private else '', id, member.name if member is not None else announcement.member_id) now = int(time.time()) if announcement.last_spoke: offset = announcement.last_spoke @@ -380,7 +393,7 @@ class Announcements(DBConnection): else: text += ' tell <@{}>'.format(mention) if announcement.channel_id != announcement.voice_id: - if announcement.channel_id == 'private': + if announcement.is_private: text += ' in private' else: text += ' in <#{}>'.format(announcement.channel_id) @@ -458,7 +471,7 @@ class Announcements(DBConnection): if arg == command: # UUID missing 'id'. - if editing and re.match(r'[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$', param): + if editing and self.is_uuid(param): parsed['id'] = param else: i -= 1 @@ -788,9 +801,7 @@ class Announcements(DBConnection): update['last_spoke'] = int(time.time()) channel = None - channel_id = str(announcement.channel_id) - private = channel_id == 'private' - if private: + if announcement.is_private: for server in client.servers: try: member = server.get_member(announcement.member_id) @@ -799,7 +810,7 @@ class Announcements(DBConnection): except: log.exception('announce') else: - channel = client.get_channel(channel_id) + channel = client.get_channel(announcement.channel_id) if channel is None: log.warning("Can't get channel for announcement {}".format(announcement.id)) # Set last_spoke so we don't spam. @@ -810,10 +821,9 @@ class Announcements(DBConnection): if announcement.attachment: attachment = bot.parse_attachment(announcement.attachment) - voice_only = attachment is None and announcement.voice_id == announcement.channel_id - log.debug('Announcement {} {}.'.format(announcement.id, 'is voice only' if voice_only else 'has text')) + log.debug('Announcement {} {}.'.format(announcement.id, 'is voice only' if announcement.voice_only else 'has text')) already_posted = False - if voice_only: + if announcement.voice_only: text = None else: # @everyone mention mangles the digest @@ -828,7 +838,7 @@ class Announcements(DBConnection): digest = announcement.digest # Always post private messages. Try not to spam public announcements. - if not private and not bot.parse_boolean(announcement.asap): + if not announcement.is_private and not bot.parse_boolean(announcement.asap): cutoff = datetime.datetime.utcnow() - datetime.timedelta(0, interval) log.debug('Getting logs from {}.'.format(cutoff)) result = yield from client.logs_from(channel, after = cutoff) @@ -860,7 +870,7 @@ class Announcements(DBConnection): logtext = 'No text' if bot.get('dryrun'): if not already_posted: - if voice_only: + if announcement.voice_only: log.info('Dryrun: Not playing announcement {} {} in {}'.format(announcement.id, filename, channel)) else: log.info('Dryrun: Not posting announcement {} to {}: {}'.format(announcement.id, channel, logtext)) @@ -870,7 +880,7 @@ class Announcements(DBConnection): if random.random() <= announcement.probability: announce = True if announce: - if voice_only: + if announcement.voice_only: yield from bot.wake_up() else: log.info('Posting announcement {} to {}: {}'.format(announcement.id, channel, logtext)) -- 2.7.4