Skip to content

Commit

Permalink
Add timezone handling (#194)
Browse files Browse the repository at this point in the history
* Update timezone handling in TwitchCog

* Update TwitchCog print statements for better logging

* Run Ruff to fix some issues
  • Loading branch information
klaasnicolaas authored May 27, 2024
1 parent 52439f7 commit 0dad79d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
BASEDIR = Path(Path(__file__).parent).resolve()
load_dotenv(BASEDIR / ".env")

TIMEZONE = "Europe/Amsterdam"

# Bot setup
VERSION = "0.4.0"
PREFIX = "!"
Expand Down
12 changes: 9 additions & 3 deletions modules/twitch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import config
import discord
import pytz
from discord.commands import SlashCommandGroup
from discord.ext import commands, tasks
from twitchAPI.twitch import Twitch as TwitchAPI
Expand All @@ -14,6 +15,7 @@ class TwitchCog(commands.Cog, name="Twitch"):
def __init__(self, bot: commands.Bot) -> None:
"""Initialize the Twitch class."""
self.bot = bot
self.timezone = config.TIMEZONE
self.client_id = config.TWITCH_CLIENT_ID
self.client_secret = config.TWITCH_CLIENT_SECRET
self.twitch_api = TwitchAPI(self.client_id, self.client_secret)
Expand Down Expand Up @@ -45,7 +47,7 @@ async def on_ready(self) -> None:
except TwitchAuthorizationException:
print("Failed to authenticate with Twitch.")
else:
print("Authenticated with Twitch.")
print("INFO: Authenticated with Twitch.")
# Initialize the live status of the channels
await self.init_live_status()
# Start the background task
Expand Down Expand Up @@ -112,7 +114,10 @@ async def get_streamers_monitor_list(self, ctx: discord.ApplicationContext) -> N
streamers_info = ""
for channel, status in self.live_status.items():
if status["live"]:
start_time = status["start_time"].strftime("%Y-%m-%d %H:%M:%S")
local_tz = pytz.timezone(self.timezone)
start_time = (
status["start_time"].astimezone(local_tz).strftime("%Y-%m-%d %H:%M")
)
streamers_info += f"🟢 **{channel}**: Live since {start_time}\n"
else:
streamers_info += f"🔴 **{channel}**: Offline\n"
Expand Down Expand Up @@ -204,7 +209,7 @@ async def init_live_status(self) -> None:
Without this function, the bot will notify the server every time it starts
"""
print("Initializing live status of Twitch streamers...")
print("INFO: Initializing live status of Twitch streamers...")
for channel in self.channels:
async for stream_info in self.twitch_api.get_streams(
user_login=channel, stream_type="live"
Expand All @@ -216,6 +221,7 @@ async def init_live_status(self) -> None:
}
break
# print(self.live_status)
print("INFO: Live status of Twitch streamers initialized.")


def setup(bot: commands.Bot) -> None:
Expand Down
15 changes: 13 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ packages = [
py-cord = "^2.4.1"
python = "^3.11"
python-dotenv = "^1.0.1"
pytz = "^2024.1"
requests = "^2.31.0"
twitchapi = "^4.2.0"

Expand Down

0 comments on commit 0dad79d

Please sign in to comment.