From 324dfe016313961198f4d791fa1c255c72cb9448 Mon Sep 17 00:00:00 2001 From: rdrescher909 <51489753+rdrescher909@users.noreply.github.com> Date: Sat, 26 Nov 2022 14:10:32 -0500 Subject: [PATCH] Add tasks.loop examples using time parameter --- docs/ext/tasks/index.rst | 51 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/docs/ext/tasks/index.rst b/docs/ext/tasks/index.rst index 43abe2384..32f606e23 100644 --- a/docs/ext/tasks/index.rst +++ b/docs/ext/tasks/index.rst @@ -129,6 +129,57 @@ Doing something during cancellation: # let's insert it to our database await self.do_bulk() +Doing something at a specific time each day: + +.. code-block:: python3 + + import datetime + from discord.ext import commands, tasks + + utc = datetime.timezone.utc + + # If no tzinfo is given then UTC is assumed. + time = datetime.time(hour=8, minute=30, tzinfo=utc) + + class MyCog(commands.Cog): + def __init__(self, bot): + self.bot = bot + self.my_task.start() + + def cog_unload(self): + self.my_task.cancel() + + @tasks.loop(time=time) + async def my_task(self): + print("My task is running!") + +Doing something at multiple specific times each day: + +.. code-block:: python3 + + import datetime + from discord.ext import commands, tasks + + utc = datetime.timezone.utc + + # If no tzinfo is given then UTC is assumed. + times = [ + datetime.time(hour=8, tzinfo=utc), + datetime.time(hour=12, minute=30, tzinfo=utc), + datetime.time(hour=16, minute=40, second=30, tzinfo=utc) + ] + + class MyCog(commands.Cog): + def __init__(self, bot): + self.bot = bot + self.my_task.start() + + def cog_unload(self): + self.my_task.cancel() + + @tasks.loop(time=times) + async def my_task(self): + print("My task is running!") .. _ext_tasks_api: