migrate uptime.py to logging

This commit is contained in:
Nexus 2023-12-05 17:45:29 +00:00
parent 9cdee767e9
commit 4c28bec11c
Signed by: nex
GPG key ID: 0FA334385D0B689F

View file

@ -2,6 +2,7 @@ import asyncio
import datetime import datetime
import hashlib import hashlib
import json import json
import logging
import random import random
import time import time
from datetime import timedelta from datetime import timedelta
@ -66,15 +67,17 @@ class UptimeCompetition(commands.Cog):
def __init__(self, bot: commands.Bot): def __init__(self, bot: commands.Bot):
self.bot = bot self.bot = bot
self.http = AsyncClient(verify=False) self.log = logging.getLogger("jimmy.cogs.uptime")
self.http = AsyncClient(verify=False, http2=True)
self._warning_posted = False self._warning_posted = False
self.test_uptimes.add_exception_type(Exception) self.test_uptimes.add_exception_type(Exception)
self.test_uptimes.start() self.test_uptimes.start()
self.last_result: list[UptimeEntry] = [] self.last_result: list[UptimeEntry] = []
self.task_lock = asyncio.Lock() self.task_lock = asyncio.Lock()
self.task_event = asyncio.Event() self.task_event = asyncio.Event()
if not (pth := Path("targets.json")).exists(): self.path = Path.home() / ".cache" / "lcc-bot" / "targets.json"
pth.write_text(BASE_JSON) if not self.path.exists():
self.path.write_text(BASE_JSON)
self._cached_targets = self.read_targets() self._cached_targets = self.read_targets()
@property @property
@ -103,7 +106,7 @@ class UptimeCompetition(commands.Cog):
return response return response
def read_targets(self) -> List[Dict[str, str]]: def read_targets(self) -> List[Dict[str, str]]:
with open("targets.json") as f: with self.path.open() as f:
data: list = json.load(f) data: list = json.load(f)
data.sort(key=lambda x: x["name"]) data.sort(key=lambda x: x["name"])
self._cached_targets = data.copy() self._cached_targets = data.copy()
@ -111,7 +114,7 @@ class UptimeCompetition(commands.Cog):
def write_targets(self, data: List[Dict[str, str]]): def write_targets(self, data: List[Dict[str, str]]):
self._cached_targets = data self._cached_targets = data
with open("targets.json", "w") as f: with self.path.open("w") as f:
json.dump(data, f, indent=4, default=str) json.dump(data, f, indent=4, default=str)
def cog_unload(self): def cog_unload(self):
@ -214,7 +217,7 @@ class UptimeCompetition(commands.Cog):
okay_statuses = list(filter(None, okay_statuses)) okay_statuses = list(filter(None, okay_statuses))
guild: discord.Guild = self.bot.get_guild(guild_id) guild: discord.Guild = self.bot.get_guild(guild_id)
if guild is None: if guild is None:
console.log( self.log.warning(
f"[yellow]:warning: Unable to locate the guild for {target['name']!r}! Can't uptime check." f"[yellow]:warning: Unable to locate the guild for {target['name']!r}! Can't uptime check."
) )
else: else:
@ -224,7 +227,7 @@ class UptimeCompetition(commands.Cog):
try: try:
user = await guild.fetch_member(user_id) user = await guild.fetch_member(user_id)
except discord.HTTPException: except discord.HTTPException:
console.log(f"[yellow]:warning: Unable to locate {target['name']!r}! Can't uptime check.") self.log.warning(f"[yellow]Unable to locate {target['name']!r}! Can't uptime check.")
user = None user = None
if user: if user:
create_tasks.append( create_tasks.append(
@ -240,7 +243,7 @@ class UptimeCompetition(commands.Cog):
) )
else: else:
if self._warning_posted is False: if self._warning_posted is False:
console.log( self.log.warning(
"[yellow]:warning: Jimmy does not have the presences intent enabled. Uptime monitoring of the" "[yellow]:warning: Jimmy does not have the presences intent enabled. Uptime monitoring of the"
" shronk bot is disabled." " shronk bot is disabled."
) )