Skip to content

Tasks

Tasks are background processes that can be used to asynchronously run code with a specified trigger.

How They Work

Tasks work by creating an asyncio.Task to run a loop to check if the task is ready to be run based on the provided trigger. Using them is fairly easy, and the easiest way is via Decorators.

Decorators are by far the easier way to run tasks, with very simple syntax to get started.

1
2
3
4
5
from interactions import Task, IntervalTrigger

@Task.create(IntervalTrigger(minutes=10)) # (1)!
async def print_every_ten():
    print("It's been 10 minutes!")

  1. This will create a task that runs every 10 minutes

You can also manually register tasks

1
2
3
4
5
6
from interactions import Task, IntervalTrigger

async def print_every_ten():
    print("It's been 10 minutes!")

task = Task(print_every_ten, IntervalTrigger(minutes=10))

By default, there are a few triggers available to the user.

These triggers run every set interval.

1
2
3
4
5
from interactions import Task, IntervalTrigger

@Task.create(IntervalTrigger(minutes=10))
async def print_every_ten():
    print("It's been 10 minutes!")

These triggers are similar to IntervalTriggers, but instead run when a specified datetime is reached.

1
2
3
4
5
6
7
8
from datetime import datetime, timedelta
from interactions import Task, DateTrigger

future = datetime.strptime("%d-%m-%Y", "01-01-2100") # (1)!

@Task.create(DateTrigger(future)) # (2)!
async def new_century():
    print("Welcome to the 22nd Century!")

  1. This create a datetime object for January 1, 2100
  2. This uses the future object to create a Task scheduled for January 1, 2100

These triggers are similar to DateTriggers, but trigger daily at the specified hour, minute, and second.

1
2
3
4
5
from interactions import Task, TimeTrigger

@Task.create(TimeTrigger(hour=0, minute=0)) # (1)!
async def midnight():
    print("It's midnight!")

  1. This creates a task to run at midnight every day

These triggers are special, in that you can pass in a list of different triggers, and if any of them are triggered, it runs the function.

1
2
3
4
5
from interactions import Task, OrTrigger, TimeTrigger

@Task.create(OrTrigger(TimeTrigger(hour=5, minute=0), TimeTrigger(hour=17, minute=0)) # (1)!
async def five():
    print("It's 5 O'clock somewhere, and that somewhere is here!")

  1. This creates a task that triggers at either 5 AM local time or 5 PM local time

Starting a task

To start a task that has been created, you need to run the Task.start() method from an async function. A good place to do this is during on_startup:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from interactions import Client, Intents, Task, IntervalTrigger, listen

@Task.create(IntervalTrigger(minutes=10))
async def print_every_ten():
    print("It's been 10 minutes!")

bot = Client(intents=Intents.DEFAULT)

@listen()
async def on_startup(): # (1)!
    print_every_ten.start()

  1. See Events for more information
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from interactions import Client, Intents, Task, IntervalTrigger, listen

async def print_every_ten():
    print("It's been 10 minutes!")

bot = Client(intents=Intents.DEFAULT)
task = Task(print_every_ten, IntervalTrigger(minutes=10))

@listen()
async def on_startup():
    task.start()