From c928110c6e3f8bdc74f397cbd5903f91416d09ba Mon Sep 17 00:00:00 2001 From: norohind <60548839+norohind@users.noreply.github.com> Date: Mon, 30 Oct 2023 20:00:50 +0300 Subject: [PATCH] Put WRYRHook.py under git --- hooks/WRYRHook.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 hooks/WRYRHook.py diff --git a/hooks/WRYRHook.py b/hooks/WRYRHook.py new file mode 100755 index 0000000..3235226 --- /dev/null +++ b/hooks/WRYRHook.py @@ -0,0 +1,79 @@ +import copy + +import requests +import config +from HookSystem import HookSystem +from Hook import Hook +from EDMCLogging import get_main_logger + +logger = get_main_logger() + +EMBED_TEMPLATE = { + "embeds": [ + { + "type": "rich", + "title": "", + "description": "", + "color": 65535, + "fields": [ + { + "name": "XP diff", + "value": "", + "inline": True + }, + { + 'name': "Involved count", + 'value': '', + 'inline': True + } + ], + "url": "https://sapi.demb.uk/diff/{action_id}" + } + ] +} +# as an idea: notifications as a service +TRACKED_TAGS = ['COFF', 'PLCN', 'CQCD', 'RMLK', 'AXIN', '7725', 'MYCF', 'GXIN', 'L4ND', 'NEWP', 'CCLS', 'BAAS', 'MAXA', 'EGPU', 'LLPC', 'DAOS', 'J3DI', 'ENEX', 'G911', 'IHCF'] + + +def notify_discord(message: dict) -> None: + """Just sends message to discord, without rate limits respect""" + logger.debug('Sending discord message') + + hookURL: str = config.discord_hook_url_1 + + discord_request: requests.Response = requests.post( + url=hookURL, + json=message, + ) + + try: + discord_request.raise_for_status() + + except Exception as e: + logger.exception(f'Fail on sending message to discord ({"/".join(hookURL.split("/")[-2:])})' + f'\n{discord_request.content}', exc_info=e) + return + + logger.debug('Sending successful') + return + + +class WRYRHook(Hook): + def update(self, action_id: int, diff: list[dict]) -> None: + for squad in diff: + if ( + squad['tag'] in TRACKED_TAGS and + squad['platform'] == 'PC' and + squad['leaderboard_type'] == 'cqc' + ): + embed = copy.deepcopy(EMBED_TEMPLATE) + embed['embeds'][0]['url'] = EMBED_TEMPLATE['embeds'][0]['url'].format(action_id=action_id) + embed['embeds'][0]['title'] = squad['tag'] + embed['embeds'][0]['fields'][0]['value'] = squad['total_experience_diff'] + embed['embeds'][0]['fields'][1]['value'] = str(len(diff)) + notify_discord(embed) + return + + +def setup(hs: HookSystem): + hs.add_update_hook(WRYRHook())