From f075370bd30ccfb9a4daa81528a0550fecff959c Mon Sep 17 00:00:00 2001 From: GaMeNu <98153342+GaMeNu@users.noreply.github.com> Date: Tue, 5 Dec 2023 12:25:40 +0200 Subject: [PATCH] proper district poppery (v2.2.4) Reworked the active district and timeout system to be district based. --- cog_notificator.py | 79 ++++++++++++++++++++++++++++++++++------------ errlogging.py | 2 +- main.py | 4 --- 3 files changed, 59 insertions(+), 26 deletions(-) diff --git a/cog_notificator.py b/cog_notificator.py index 29aad50..666d835 100644 --- a/cog_notificator.py +++ b/cog_notificator.py @@ -1,3 +1,4 @@ +import copy import datetime import platform import re @@ -25,6 +26,27 @@ AUTHOR_ID = int(os.getenv('AUTHOR_ID')) # 2023-10-26 I have decided to start documenting the project. +# ^ +# ^ ^ ^ ^ | ^ ^ ^ ^ V +# | | | | | | | | | E +# \ \ \ \|/ / / / R +# \ \ \ | / / / Y +# \ \ \|/ / / +# \ \ | / / T +# \ \|/ / A +# \ | / L +# \|/ L +# | +# | L +# <|> O +# _____/-|-\_____ L +# /-----<-|->-----\ +# +# /-----<-|->-----\ +# | HAPPY | +# | HANUKKAH! | +# \-----<-|->-----/ + class AlertReqs: """ @@ -101,6 +123,7 @@ class Notificator(commands.Cog): self.db = DBAccess() self.active_districts = [] + self.district_timeouts = {} self.reset_district_checker = 0 self.alert_reqs = AlertReqs() @@ -196,40 +219,51 @@ class Notificator(commands.Cog): return self.log.debug(f'Alert response: {current_alert}') - if current_alert is None or len(current_alert) == 0: - # Alert is empty + # This code reduces district timeouts, and removes them from the dict entirely when reached 0 + # poppery :D + for dist_name in self.district_timeouts.copy().keys(): + self.district_timeouts[dist_name] -= 1 + # I like <= over ==, due to a (probably unreasonable) fear that something might go wrong, and it would get decremented twice + if self.district_timeouts[dist_name] <= 0: + self.district_timeouts.pop(dist_name, None) + self.log.debug(f'Popped district {dist_name}') - if current_alert is None: - # None - an error while retrieving alert - self.log.warning('Error while current alert data.') + # Check for SUCCESSFUL data retrieval + if current_alert is None: + # None - an error while retrieving alert + self.log.warning('Error while current alert data.') + return + + # Check if there's NO NEW DATA + if len(current_alert) == 0: # from here, len(current_alert) == 0 - # current_alert is an empty dict. + # current_alert is an empty dict - no new data. - if len(self.active_districts) == 0: - # Alert is empty and there are no active districts - return - - # Check if we should reset active districts. 3 empty alerts in a row will trigger a reset - self.reset_district_checker += 1 - if self.reset_district_checker == 3: - print('reset') - self.active_districts = [] - self.reset_district_checker = 0 + # DO **NOT** + # TOUCH THAT RETURN + # PLEASE return + # FROM HERE ON, WE CAN ASSUME THAT THERE ARE NEW DISTRICTS + # data = active districts data: list[str] = current_alert["data"] new_districts: list[str] = [] - for district in data: + for district_name in data: # Get all new_districts - if district in self.active_districts: - continue + if district_name in self.district_timeouts.keys(): + new_dist = False + else: + new_dist = True - new_districts.append(district) + self.district_timeouts[district_name] = 60 + + if new_dist: + new_districts.append(district_name) if len(new_districts) == 0: # There are no new districts. @@ -240,7 +274,6 @@ class Notificator(commands.Cog): except Exception as e: self.log.error(f'Could not send message!\nError info: {e.__str__()}') - self.active_districts = data self.reset_district_checker = 0 @check_for_updates.after_loop @@ -555,6 +588,10 @@ class Notificator(commands.Cog): # Goddamnit Linux too many distros system_name = f'{distro.name()} {distro.version_parts()[0]}.{distro.version_parts()[1]} ({distro.codename()})' b_to_mb = 1000000 + + pid = os.getpid() + process = psutil.Process(pid) + e = discord.Embed(color=discord.Color.orange()) e.title = 'Home Front Command Notificator' e.description = 'Info about this bot instance' diff --git a/errlogging.py b/errlogging.py index d9a3094..6b179bb 100644 --- a/errlogging.py +++ b/errlogging.py @@ -33,7 +33,7 @@ class ErrLogger: ctx = e.__context__ - while ctx is not None: + if ctx is not None: context_ls.append(ctx) context_str = '\n'.join([context.__str__() for context in context_ls]) diff --git a/main.py b/main.py index ae33115..51f6e17 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,4 @@ -import datetime import sys -import traceback -import types import discord from discord.ext import commands, tasks @@ -9,7 +6,6 @@ from dotenv import load_dotenv import logging import os -import cog_notificator import errlogging from cog_notificator import Notificator