[PYTHON] [Latest version] Let the bot speak at regular intervals on discord.py

Overview

I will explain how to make a bot perform regular processing using discord.py, which is a Python Discord API wrapper that allows you to write a bot concisely in the chat tool Discord.

Caution

The code example wasn't really working. The code example was revised on April 10, 2020.

Premise

Notes on version

discord.py has been provided with ver0.16.12 (so-called async version) for a long time, but v1.0.0 (so-called rewrite version) was released in April 2019, and v0 series is no longer recommended for use. Is not ... In fact, v0 series does not support Python 3.7 or higher, and future update maintenance cannot be expected. We recommend that you support the latest version as much as possible. Please refer to the following article for regular processing in v0 series.

Make the bot speak at regular intervals on discord.py [async version]

Method

Use tasks which is an extension of'discord.py'. This is a library that focuses on repetitive execution, which is often used in bot development. The iterative process can be described very concisely. Official documentation

Basically, on the line above the method you want to execute periodically After adding @ tasks.loop ([execution interval]), start () is performed at the timing when you want to start periodic execution.

Let's look at a concrete example in the next section.

Concrete example

The following is an example of performing regular execution every 10 seconds immediately after starting the bot on the channel with the ID specified in advance.

from discord.ext import tasks
import discord

client = discord.Client()

channel_sent = None
"""
The part that defines the method that speaks every 10 seconds.
One line above the async def indicates periodic execution.()Specify the interval within.
For example, every 5 minutes(minutes=5)is.
"""
@tasks.loop(seconds=10)
async def send_message_every_10sec():
    await channel_sent.send("10 seconds have passed")

"""
This time I want to start regular execution immediately after starting the bot, so
Start regular execution when the bot is ready()To do
"""
@client.event
async def on_ready():
    global channel_sent 
    channel_sent = client.get_channel(any_channel_id)
    send_message_every_10sec.start() #Behind the method that runs regularly.start()Put on


client.run("hogehogetoken")

Now you can have the specified channel say "10 seconds have passed" every 10 seconds.

If you apply it, when you receive the specified command, you can execute it from that timing. You just change the timing of start ().

At the end

Make the bot speak at regular intervals on discord.py [async version] I'm sorry that the number of views of this article is still increasing, so I created a flow line to the latest version for the time being. Be sure to update ...

Please comment if you have any questions. I've mastered discord.py to some extent.

Code example supplement

The above code example is a code that has been fixed several times after receiving some bug reports. There are some unnatural parts because we have modified it so that it does not deviate from the code example that was originally written. In particular, rewriting the value of an outer variable using global should be avoided if it is not necessary (it is less readable and can unintentionally rewrite the value of a variable).

For example, referring to the following article, if the looping function itself takes the destination channel as an argument, you can just pass it as an argument of start () without rewriting it with global. https://qiita.com/rareshana/items/b84bec58acd48cb14118

Alternatively, you can use Cog, a function that groups commands, events, and tasks, to store channels in instance variables, so you don't need to use global variables either. Please rewrite appropriately according to the situation.

Recommended Posts

[Latest version] Let the bot speak at regular intervals on discord.py
[Latest version] Play YouTube videos on discord.py!
Use the latest version of PyCharm on Ubuntu
Install the latest version of CMake on Ubuntu 18.04.4 LTS
Put the latest version of Python on linux (Debian) on Chromebook
Install the latest version of Git on your Linux server
Suspend Thread on Linux (corrected version at the bottom of the article)
Install the latest version of Apache httpd 2.4 from source on Cent OS 8
Be careful when retrieving tweets at regular intervals with the Twitter API