diff --git a/newtest.py b/newtest.py new file mode 100644 index 00000000..382cad8c --- /dev/null +++ b/newtest.py @@ -0,0 +1,34 @@ +from twitchio.ext import routines, commands +from dotenv import load_dotenv +import os + +load_dotenv() +from datetime import datetime, timedelta + + +THRESHOLD = 0.1 + + +class Bot(commands.Bot): + call_count = 0 + + def __init__(self): + super().__init__( + token=os.getenv("access_token"), + prefix="#!", + initial_channels=["dreamingofelectricsheep"], + ) + + async def event_ready(self): + print(f"Logged in as | {self.nick}") + print(f"User id is | {self.user_id}") + bot.basic_scheduled_routine.start() + + @routines.routine(time=datetime.now() + timedelta(seconds=5), wait_first=True) + async def basic_scheduled_routine(self): + self.call_count += 1 + print(f"{self.call_count} the time is now {datetime.now()}") + + +bot = Bot() +bot.run() diff --git a/test.py b/test.py new file mode 100644 index 00000000..c1655573 --- /dev/null +++ b/test.py @@ -0,0 +1,68 @@ +import asyncio +from twitchio.ext import routines, commands +from dotenv import load_dotenv +import os + +load_dotenv() +from datetime import datetime, timedelta + + +THRESHOLD = 0.1 + + +class Bot(commands.Bot): + call_count = 0 + + def __init__(self): + super().__init__( + token=os.getenv("access_token"), + prefix="#!", + initial_channels=["dreamingofelectricsheep"], + ) + + async def event_ready(self): + print(f"Logged in as | {self.nick}") + print(f"User id is | {self.user_id}") + + @routines.routine(seconds=1.0, iterations=5) + async def basic_timed_routine(self, arg: str): + print(f"Hello {arg}!") + + @routines.routine(seconds=0.1) + async def basic_test(self): + print(self.basic_timed_routine.next_execution_time) + if self.basic_timed_routine.remaining_iterations == 0: + self.basic_timed_routine.start("Restarted") + + @routines.routine(seconds=1.0, iterations=5, wait_first=True) + async def basic_timed_wait_routine(self, arg: str): + print(f"Hello {arg}!") + + @routines.routine(seconds=0.1) + async def basic_wait_test(self): + print(self.basic_timed_wait_routine.next_execution_time) + if self.basic_timed_wait_routine.remaining_iterations == 0: + self.basic_timed_wait_routine.start("Restarted") + + @routines.routine(time=datetime.now() + timedelta(seconds=5), wait_first=True) + async def basic_scheduled_routine(self): + # Note this seems to get called twice in execution. I don't think this is a result of my changes + self.call_count += 1 + print(f"{self.call_count} the time is now {datetime.now()}") + + @routines.routine(seconds=0.1) + async def basic_schedule_test(self): + print(self.basic_scheduled_routine.next_execution_time) + + @routines.routine(seconds=2) + async def restart_schedule_test(self): + pass + + +bot = Bot() +# bot.basic_timed_wait_routine.start("Test") +# bot.basic_wait_test.start() + +bot.basic_scheduled_routine.start() + +bot.run() diff --git a/twitchio/ext/routines/__init__.py b/twitchio/ext/routines/__init__.py index 7354582d..d3c30ce6 100644 --- a/twitchio/ext/routines/__init__.py +++ b/twitchio/ext/routines/__init__.py @@ -97,9 +97,10 @@ def __init__( self._instance = None - self._args: tuple | None = None - self._kwargs: dict | None = None - self.next_event_time: datetime.datetime | None = None + self._args: Optional[tuple] = None + self._kwargs: Optional[dict] = None + + self.next_event_time: Optional[datetime.datetime] = None def __get__(self, instance, owner): if instance is None: @@ -332,9 +333,10 @@ def next_execution_time(self) -> datetime.timedelta | None: if self.next_event_time is None: return None - - return max(self.next_event_time - datetime.datetime.now(self.next_event_time.tzinfo), datetime.timedelta(seconds=0)) - + + return max( + self.next_event_time - datetime.datetime.now(self.next_event_time.tzinfo), datetime.timedelta(seconds=0) + ) @property def start_time(self) -> Optional[datetime.datetime]: @@ -370,7 +372,7 @@ async def _routine(self, *args, **kwargs) -> None: self.next_event_time = self._time wait = compute_timedelta(self._time) await asyncio.sleep(wait) - + if self._wait_first and not self._time: self.next_event_time = self._delta + datetime.datetime.now(datetime.timezone.utc) await asyncio.sleep(self._delta)