[PYTHON] Pass arguments to Task in discord.py

Premise

Caution

The code that appears in this article is provided for illustration purposes only and is not intended for production use. Please rewrite it appropriately according to your application.

Thing you want to do

I want to pass arguments to Task

Why i want to do

Basically, I think that Tasks that are executed regularly often start when the bot starts. However, sometimes you want to control the start timing and destination channel with commands. For example, ** "I want you to mention the time signal on the specified channel every 5 minutes for 30 minutes from now" **.

However, there is currently no example in the discord.py document that Task (https://discordpy.readthedocs.io/ja/latest/ext/tasks/index.html) takes an argument.

Method

First, add an argument to the Task you want to execute regularly. For example, consider the case where a mention is sent to the person who sent the command in the channel in which the command is input. In this case, if you receive Context as an argument when entering the command, it seems that you can identify the channel and sender.

from discord.ext import tasks, commands

class MyCog(commands.Cog):
    def __init__(self):
        pass

    def cog_unload(self):
        self.reminder.cancel()

    @tasks.loop(minutes=5.0, count=6)
    async def reminder(self, ctx, content):
       await ctx.send("<@{0}> {1}".format(ctx.author.id, content))

If you define reminder like this, you will be reminded just by starting it. So how do you pass this argument ctx?

Pass the argument of Task to start ()

As the title says. You can pass it to start () which is used when the task starts. The * args and ** kawrgs passed to start () are passed as they are as arguments of the corresponding Task. So, for example, you can define the following reminder command.

from discord.ext import tasks, commands

class MyCog(commands.Cog):
    def __init__(self):
        pass

    def cog_unload(self):
        self.reminder.cancel()

    @tasks.loop(minutes=5.0, count=6)
    async def reminder(self, ctx, content):
       await ctx.send("<@{0}> {1}".format(ctx.author.id, content))

    @commands.command()
    async def set_reminder(self, ctx, content):
        self.reminder.start(ctx, content) #Important here
        await ctx.send("I set a reminder")

Now, if you send the set_reminder command to the chat, the pre-specified content will be mentioned every 5 minutes. The point is self.reminder.start (ctx, content), and it's OK as long as the argument passed to start () is passed to the argument of Task started by that start ().

Caution

As I wrote at the beginning, this code itself is not intended for production. I'm curious about what happens if another person hits set_reminder within 30 minutes after entering set_reminder.

Please be aware that this is just a pseudo code for explaining how to pass arguments to Task. (But please let me know if there are any fatal mistakes such as grammatical mistakes)

Recommended Posts

Pass arguments to Task in discord.py
How to pass arguments to a Python script in SPSS Modeler Batch
[Python / Tkinter] How to pass arguments to command
Introduction to discord.py (2)
Introduction to discord.py
How to receive command line arguments in Python
Let's understand how to pass arguments (Python version)
How to pass settings to Item Pipeline in Scrapy
nest cog in discord.py
Execute Python function from Powershell (how to pass arguments)
Updated messages in discord.py
Do not pass self to ProcessPoolExecutor in the class
How to pass matplotlib backend settings in environment variables
Define a task to set the fabric env in YAML
How to embed multiple embeds in one message with Discord.py
[ECS] [Fargate] Script to get Task ID in TASK execution container
In omegaconf, let's pass the direct parameter file to the function
To flush stdout in Python
Login to website in Python
Receive runtime arguments in Python 2.7
How to develop in Python
Introduction to discord.py (3) Using voice
Pass text to Django genericview
Post to Slack in Python
Things to watch out for when using default arguments in Python
How to pass arguments using an instance with systemd's systemctl command
I want to pass the G test in one month Day 1
Precautions when giving default values to arguments in Python function definitions