Skip to content

Commit bf2005d

Browse files
committed
address comments, mainly tz stuff
1 parent 7f972be commit bf2005d

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

cogs/commands/system.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import logging
22
import platform
3-
from datetime import datetime
3+
from datetime import datetime, timezone
44
from os import environ
55
from typing import Any
66

77
import aiohttp
88
import discord
9+
from dateutil import parser
910
from discord.ext import commands
10-
from discord.ext.commands import Bot, Context
11+
from discord.ext.commands import Bot, Context, check
1112
from sqlalchemy import select
1213

1314
from config import CONFIG
1415
from models.models import db_session
1516
from models.system import EventKind, SystemEvent
17+
from utils import is_compsoc_exec_in_guild
18+
19+
APOLLO_JSON_URL = (
20+
"https://portainer.uwcs.co.uk/api/endpoints/2/docker/containers/apollo/json"
21+
)
1622

1723

1824
class System(commands.Cog):
@@ -49,11 +55,10 @@ async def info(self, ctx: Context[Bot]):
4955
py_version = platform.python_version()
5056
dpy_version = discord.__version__
5157

52-
# the timestamp docker gives does not conform to ISO.
53-
# we strip the fractional secconds and add the suffix which assumes UTC (may cause issues in summer)
58+
# the timestamp docker gives is in ISO 8601 format, but py3.10 does not fully support it
5459
timestamp: str = json["State"]["StartedAt"]
55-
started = datetime.fromisoformat(timestamp.split(".")[0])
56-
uptime = datetime.now() - started
60+
started = parser.parse(timestamp)
61+
uptime = datetime.utcnow() - started.astimezone(timezone.utc)
5762

5863
if version and built:
5964
reply = f"""Apollo, {description}\n
@@ -77,6 +82,7 @@ async def version(self, ctx: Context[Bot]):
7782
await ctx.reply("Could not find version")
7883

7984
@commands.hybrid_command()
85+
@check(is_compsoc_exec_in_guild)
8086
async def restart(self, ctx: Context[Bot]):
8187
headers = {"X-API-Key": f"{CONFIG.PORTAINER_API_KEY}"}
8288
async with aiohttp.ClientSession(headers=headers) as session:
@@ -127,19 +133,17 @@ async def on_ready(self):
127133
async def get_docker_json() -> dict[Any, Any] | None:
128134
headers = {"X-API-Key": f"{CONFIG.PORTAINER_API_KEY}"}
129135
async with aiohttp.ClientSession(headers=headers) as session:
130-
url = "https://portainer.uwcs.co.uk/api/endpoints/2/docker/containers/apollo/json"
131-
resp = await session.get(url)
136+
resp = await session.get(APOLLO_JSON_URL)
132137
if not resp.ok:
133138
logging.error("Could not reach Portainer API")
134139
return None
135140
return await resp.json()
136141

137142

138143
async def setup(bot: Bot):
139-
url = "https://portainer.uwcs.co.uk/api/endpoints/2/docker/containers/apollo/json"
140144
headers = {"X-API-Key": f"{CONFIG.PORTAINER_API_KEY}"}
141145
async with aiohttp.ClientSession(headers=headers) as session:
142-
resp = await session.get(url)
146+
resp = await session.get(APOLLO_JSON_URL)
143147
match (resp.ok, (environ.get("CONTAINER") is not None)):
144148
case (True, True):
145149
await bot.add_cog(System(bot))

0 commit comments

Comments
 (0)