From 97e92dd07a5f74031205d7701daffa5c4e16b134 Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Sat, 15 Apr 2017 21:56:26 +0100 Subject: [PATCH] Report stale data. --- plugin/faction/faction.py | 54 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/plugin/faction/faction.py b/plugin/faction/faction.py index dfbb241..128fbae 100644 --- a/plugin/faction/faction.py +++ b/plugin/faction/faction.py @@ -24,6 +24,8 @@ class TrackedFaction(object): for k, v in dict(row).items(): setattr(self, k, v) self.is_private = hasattr(self, 'channel_id') and self.channel_id == 'private' + if not hasattr(self, 'newsflash_channel_id'): + self.newsflash_channel_id = None def owned_by(self, owner): try: @@ -45,7 +47,7 @@ class Faction(DBConnection): def create_tables(self): self.open_db() - cursor = self.query('create table if not exists factions (id char(36) not null, bot_id varchar(32) not null, member_id varchar(32) not null, server_id varchar(32) not null, channel_id varchar(32) not null, name varchar(128) not null, state_id int not null, interval int not null default 86400, updated datetime)') + cursor = self.query('create table if not exists factions (id char(36) not null, bot_id varchar(32) not null, member_id varchar(32) not null, server_id varchar(32) not null, channel_id varchar(32) not null, newsflash_channel_id varchar(32), name varchar(128) not null, state_id int not null, interval int not null default 86400, updated datetime)') cursor = self.query('create unique index if not exists factions_id on factions (id)') cursor = self.query('create index if not exists factions_bot_id on factions (bot_id)') cursor = self.query('create table if not exists faction_names (faction_id int not null, name varchar(128) not null)') @@ -69,7 +71,7 @@ class Faction(DBConnection): def get_tracked_factions(self): now = self.now() - cursor = self.query("select id, name, server_id, member_id, channel_id, interval, name, state_id, updated from factions where updated is null or updated < ? - interval and bot_id=?", [now, client.user.id]) + cursor = self.query("select id, name, server_id, member_id, channel_id, newsflash_channel_id, interval, name, state_id, updated from factions where updated is null or updated < ? - interval and bot_id=?", [now, client.user.id]) for row in cursor.fetchall(): yield TrackedFaction(row) self.close_db() @@ -323,6 +325,8 @@ class Faction(DBConnection): member = server.get_member(faction.member_id) text += ' by {}'.format(member.name) text += ' in <#{}>'.format(faction.channel_id) + if faction.newsflash_channel_id: + text += ' news <#{}>'.format(faction.newsflash_channel_id) text += ' last state {} at {}'.format(eddb.state_name(faction.state_id), bot.iso8601(faction.updated)) results.append(text) @@ -366,6 +370,8 @@ class Faction(DBConnection): text += ' in private' else: text += ' in <#{}>'.format(faction.channel_id) + if faction.newsflash_channel_id: + text += ' news <#{}>'.format(faction.newsflash_channel_id) lines.append(text) await bot.say_many(message.channel, lines) @@ -439,11 +445,14 @@ class Faction(DBConnection): update['updated'] = self.now() channel = None + newsflash_channel = None if tracked.is_private: for server in client.servers: try: member = server.get_member(tracked.member_id) channel = await client.start_private_message(member) + if tracked.newsflash_channel_id is not None: + newsflash_channel = channel break except: log.exception('report_tracked_faction') @@ -544,7 +553,8 @@ class Faction(DBConnection): def faction_key(self, k): d = { 'channel_id': 'in', - 'interval': 'every' + 'interval': 'every', + 'newsflash_channel_id': 'news' } if k in d: return d[k] @@ -583,26 +593,33 @@ class Faction(DBConnection): ok = True else: break - elif arg == 'in': - k = 'channel_id' + elif arg in ['in', 'news']: + if arg == 'in': + k = 'channel_id' + else: + k = 'newsflash_channel_id' if param == 'here': if message.channel.is_private: parsed[k] = 'private' - parsed['server_id'] = 'private' + if arg == 'in': + parsed['server_id'] = 'private' ok = True else: parsed[k] = message.channel.id - parsed['server_id'] = message.channel.server.id + if arg == 'in': + parsed['server_id'] = message.channel.server.id ok = True elif param == 'private': parsed[k] = 'private' - parsed['server_id'] = 'private' + if arg == 'in': + parsed['server_id'] = 'private' ok = True elif param: channel = bot.parse_channel(param, author = message.author, voice_ok = False) if channel is not None: parsed[k] = channel.id - parsed['server_id'] = channel.server.id + if arg == 'in': + parsed['server_id'] = channel.server.id ok = True else: break @@ -647,6 +664,21 @@ class Faction(DBConnection): await bot.say(message.channel, 'Channel?') return None + if 'newflash_channel_id' in parsed: + if parsed['newsflash_channel_id'] is not None: + if parsed['channel'] == 'private': + if parsed['newsflash_channel_id'] != 'private': + log.info('News flash channel must be private if report channel is private.') + await bot.say(message.channel, 'Flash private!') + return None + else: + channel = bot.parse_channel(parsed['channel_id'], author = message.author, voice_ok = False) + newsflash_channel = bot.parse_channel(parsed['newsflash_channel_id'], author = message.author, voice_ok = False) + if channel.server != newsflash_channel.server: + log.info('News flash channel must be on same server.') + await bot.say(message.channel, 'Flash channel server!') + return None + if not editing: parsed['updated'] = self.now() if 'name' in parsed or not editing: @@ -786,6 +818,8 @@ class Faction(DBConnection): faction_id = faction_ids[name] for tracked in self.tracked_factions: if tracked.name == name: + if tracked.newsflash_channel_id is None: + break channel = None if tracked.is_private: for server in client.servers: @@ -795,7 +829,7 @@ class Faction(DBConnection): except: pass else: - channel = client.get_channel(tracked.channel_id) + channel = client.get_channel(tracked.newsflash_channel_id) if channel is not None: announce[system] = channel break -- 2.7.4