|
| 1 | +"""A bare minimum example of running Routines. |
| 2 | +
|
| 3 | +This example assumes you have a Client/Bot or Components already setup and configured. |
| 4 | +Consider reading the following examples first: |
| 5 | +
|
| 6 | +Basic Bot: https://github.com/PythonistaGuild/TwitchIO/tree/main/examples/basic_bot |
| 7 | +Basic Bot (Using Conduits): https://github.com/PythonistaGuild/TwitchIO/tree/main/examples/basic_conduits |
| 8 | +
|
| 9 | +Routines can be used and started anywhere, but an asyncio.Loop should generally be running first. |
| 10 | +""" |
| 11 | +import datetime |
| 12 | + |
| 13 | +from twitchio.ext import commands, routines |
| 14 | + |
| 15 | + |
| 16 | +class Bot(commands.Bot): |
| 17 | + ... |
| 18 | + |
| 19 | + @routines.routine(delta=datetime.timedelta(seconds=60), wait_first=True) |
| 20 | + async def minute_routine(self) -> None: |
| 21 | + """A basic routine which does something every minute. |
| 22 | + |
| 23 | + This routine will wait a minute first after starting, before making the first iteration. |
| 24 | + """ |
| 25 | + print("A minute has passed!") |
| 26 | + |
| 27 | + @routines.routine(delta=datetime.timedelta(seconds=1), iterations=5) |
| 28 | + async def first_five(self) -> None: |
| 29 | + """A basic routine which outputs an incrementing number every second, 5 times: E.g. 1 to 5. |
| 30 | + |
| 31 | + After 5 iterations, this routine will stop. |
| 32 | + """ |
| 33 | + print(f"{self.first_five.current_iteration}!") |
| 34 | + |
| 35 | + @routines.routine(time=datetime.datetime.now()) |
| 36 | + async def schedule(self) -> None: |
| 37 | + """A basic routine which runs at a specific time each day. |
| 38 | + |
| 39 | + For example purposes the routine will run at it's initial start time every day. |
| 40 | + """ |
| 41 | + print(f"Hello, I run at the same time everyday!") |
| 42 | + |
| 43 | + @routines.routine(delta=datetime.timedelta(hours=1)) |
| 44 | + async def hour_routine(self, number: int) -> None: |
| 45 | + """A basic routine which runs every hour. |
| 46 | + |
| 47 | + This routine has arguments parsed to it from .start() |
| 48 | + """ |
| 49 | + print(f"I say the number {number} every hour!") |
| 50 | + |
| 51 | + async def setup_hook(self) -> None: |
| 52 | + self.minute_routine.start() |
| 53 | + self.first_five.start() |
| 54 | + self.schedule.start() |
| 55 | + self.hour_routine.start(23) |
| 56 | + |
0 commit comments