From 086253b1c3271320b5a1168367820ca69b8af854 Mon Sep 17 00:00:00 2001 From: norohind <60548839+norohind@users.noreply.github.com> Date: Fri, 15 Apr 2022 00:50:27 +0300 Subject: [PATCH] Add hooks for RU squads --- hooks/RuSquads.py | 93 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 hooks/RuSquads.py diff --git a/hooks/RuSquads.py b/hooks/RuSquads.py new file mode 100644 index 0000000..8ca5454 --- /dev/null +++ b/hooks/RuSquads.py @@ -0,0 +1,93 @@ +import json + +from HookSystem import HookSystem +from Hook import Hook +import HookUtils + + +class DeleteRuSquad(Hook): + """ + Send alert to discord if was removed russian squad + """ + + def update(self, operation_id: int) -> None: + last_record: dict = self.get_db().execute( + HookUtils.SQL_REQUESTS.GET_HISTORICAL_INFO, + { + 'limit': 1, + 'operation_id': operation_id + } + ).fetchone() + + if last_record is not None: # i.e. we have a record in db for this squad + if 32 in json.loads(last_record['user_tags']): # 32 - russian tag + message = f'Deleted RU squad `{last_record["name"]}`\nplatform: {last_record["platform"]}, members: {last_record["member_count"]}, tag: {last_record["tag"]}, ' \ + f'created: {last_record["created"]}, owner: `{last_record["owner_name"]}`' + HookUtils.notify_discord(message) + + +class UpdateRuSquad(Hook): + """ + Send alert to discord if something important was changed for ru squad + """ + + def update(self, operation_id: int) -> None: + last_records = self.get_db().execute( + HookUtils.SQL_REQUESTS.GET_HISTORICAL_INFO, + { + 'limit': 2, + 'operation_id': operation_id + } + ).fetchall() + + if len(last_records) == 1: + # Squad just discovered + record = last_records[0] + if '32' in record['user_tags']: + message = f""" +name: `{record['name']}` [{record['tag']}] +members: {record['member_count']} +created: {record['created']} +platform: {record['platform']} +owner: `{record['owner_name']}` +tags:\n{HookUtils.humanify_resolved_user_tags(HookUtils.resolve_user_tags(record['user_tags']))} +motd: `{record['motd']}` +motd author: `{record['author']}` +""" + HookUtils.notify_discord(message) + elif len(last_records) == 2: + # Squad updated + last_records[0]['user_tags'] = json.loads(last_records[0]['user_tags']) + last_records[1]['user_tags'] = json.loads(last_records[1]['user_tags']) + + latest_record = last_records[0] + + if 32 not in [*last_records[0]['user_tags'], *last_records[1]['user_tags']]: + return + + message = HookUtils.diff_columns( + last_records, + [ + HookUtils.Column('member_count', 'Members count:'), + HookUtils.Column('accepting_new_members', 'Accepting new members:'), + HookUtils.Column('user_tags', '', is_user_tags=True), + HookUtils.Column('owner_name', 'Ownership:', is_screen_values=True), + HookUtils.Column('owner_id', 'Owner FID:'), + HookUtils.Column('motd', 'Motd:', is_screen_values=True), + HookUtils.Column('author', 'Motd author:', is_screen_values=True), + HookUtils.Column('faction_name', 'Faction:') + ] + ) + + if message != '': + HookUtils.notify_discord( + HookUtils.generate_message_header(latest_record, 'RU') + message + ) + + else: + HookUtils.notify_discord(f'{operation_id=}, {len(last_records)=}') + + +def setup(hs: HookSystem): + hs.add_on_delete_hook(DeleteRuSquad()) + hs.add_on_insert_hook(UpdateRuSquad())