Skip to main content

Add a task system to discord-py-interactions, similar to discord.ext.tasks

Project description

Simple example, triggers every 3 seconds:

from interactions.ext.tasks import IntervalTrigger, create_task

@create_task(IntervalTrigger(3))
async def my_task():
    print("hi")

my_task.start()

Multiple conditions (example will be improved when more triggers are added)
The task will only run every 5 seconds because every trigger needs to be ready

from interactions.ext.tasks import IntervalTrigger, create_task

@create_task(IntervalTrigger(3), IntervalTrigger(5))
async def my_task():
    print("hi")

my_task.start()

To have the task run when any trigger is ready, use the OrTrigger.
The task will run every 3 and 5 seconds (it may run twice occasionally due to how timing works)

from interactions.ext.tasks import IntervalTrigger, OrTrigger, create_task

@create_task(OrTrigger(IntervalTrigger(3), IntervalTrigger(5)))
async def my_task():
    print("hi")

my_task.start()

Classes are special, due to how functions turn into methods and other python black magic
There are two ways to properly do tasks in classes

The first way is to pass self when starting the task
Any args or kwargs passed to Task.start() will be prepended to the function

from interactions import Extension
from interactions.ext.tasks import IntervalTrigger, create_task

class Cog(Extension):
    def __init__(self, client):
        self.method.start(self)

    @create_task(IntervalTrigger(1))
    async def method(self):
        print(self)

The other way is just to wrap the method manually

from interactions import Extension
from interactions.ext.tasks import IntervalTrigger, create_task

class Cog(Extension):
    def __init__(self, client):
        self.method = create_task(IntervalTrigger(1))(self.method)
        self.method.start()

    async def method(self):
        print(self)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

interactions-tasks-1.0.0.tar.gz (4.9 kB view hashes)

Uploaded Source

Built Distribution

interactions_tasks-1.0.0-py3-none-any.whl (5.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page