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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file interactions-tasks-1.0.0.tar.gz.

File metadata

  • Download URL: interactions-tasks-1.0.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for interactions-tasks-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8ab83788bdd06fb43fd79ed867bcfa6fba268ce94916ac4c986bd3182d7c4d5c
MD5 246e1f7938f2d194972ee36bcaf815d3
BLAKE2b-256 7de4b727cecef2002315ed5dfd1239d3a8ded290199e8d911fcae26931b59b42

See more details on using hashes here.

File details

Details for the file interactions_tasks-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for interactions_tasks-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 73f389670a8968dc1e77dedaec4345a509f6c039a092da5715137b4f09e2c19e
MD5 6398c70d15770e9827eed9c2321eac03
BLAKE2b-256 93615c787239e3bf051f69cc7f45b57067d213c1120151c443d0b0c58582f26f

See more details on using hashes here.

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