[PYTHON] Even monkeys can understand! About Intents on discord.py!

What are Intents?

Intents is a new feature in discord.py 1.5. With this, you can choose to "receive some events and not some events". You can reduce the amount of communication and memory usage.

How To Use It is applied by passing it as an argument when creating an object of Client or Bot. However, ** Some Intents (Privieged Intents) are limited ** To enable this, in addition to writing in the code, go to the Developer Portal (https://discord.com/developers/applications/), select the application and then the Bot tab (get / reset token) You need to enable it manually (where you want to). Click the two buttons in the image to activate it. (Already enabled in the image) image.png

The explanation about Privieged Intents is quoted from discord.py official Discord server #news.

--Presence Intent (above): Required to receive Member.status, Member.activity, ʻon_member_update(only for status and activity). When set,presences = True --Server Members Intent (below): Required to receive events starting with ʻon_member and ʻon_user_update. It is also required for Guild.get_memberetc. When set,members = True`

Note: For bots with more than 100 servers, Privileged Intents can only be used with Discord authentication. If your bot is already authenticated, please contact Discord.

After making the above settings, let's actually use it! Since ʻon_typing is rarely used, here is an example of not receiving ʻon_typing.

main.py


import discord
intents = discord.Intents.default()  #Create default Intents object
intents.typing = False  #Don't receive typing
client = discord.Client(intents=intents)
# discord.When using ext
# from discord.ext import commands
# bot = commands.Bot(command_prefix="/", intents=intents)
# or
# super().__init__(command_prefix="/", intents=intents)

The second line, discord.Intents.default (), creates a default Intents object. By default, members and presences are set to False and the others are set to True.

Other than default (), there are methods that create Intents objects that are all True with ʻall ()and allFalse with none () . For the attributes of Intents when manually setting True`` False, see [Reference] of discord.Intents` (https://discordpy.readthedocs.io/ja/latest/api.html#discord.Intents". )Please refer to.

Yes Yes question! Which Intents should I use?

Depending on the person, for small bots (continuing to use traditional code and having less than 100 servers), set all Intents to True ʻintents = discord.Intents.all ()` No problem!

Most users have no problem with the above method, but I think that users who operate large-scale bots need individual consultation.

on_ready () does not ignite easily! Why?

Due to changes in the Discord API, there are also changes in member loading. Previously, 75 servers could be requested at the same time, and only servers with Guild.large being True (= 250 or more members) were required, but now all servers need it, and 1 per request. You can now request only the server. This causes a speed reduction of about 75 times.

The following is a quote from the official documentation for speed verification.

Example: A bot that belongs to 840 servers, of which 95 servers belong to more than 250 people (Guild.large == True).

Currently: Approximately 60 seconds (75 servers, 20 servers) ʻIntents.members == True and Intents.presences == False: Approximately 7 minutes (840 requests, speed limit 120 servers / minute) ʻIntents.members == True and Intents.presences == True: Approximately 100 seconds (95 requests)

Official documentation to introduce.

Return the request mechanism

First, there is a way to enable Privileged Intents for both Presences and Server Members. Now, the request mechanism is partly the same as before, so the startup speed is also the same as before.

Shift member loading

Next, if you set the chunk_guilds_at_startup argument of Client or Bot to False, the members will not be loaded at the start, so the startup will be faster. After that, use Guild.cunk etc. (if necessary) to get the members of the server. Other acquisition methods are described in Reference.

I don't like this spec! What should I do?

At this point, the old API is still available, so discord.py 1.4 will be available until the end of support for the v6 gateway, but it's a good idea to update your code to a new way for the future of your bot. For downgrade

# Windows
py -3 -m pip install -U discord.py>=1.4, 1.5
# Linux Mac
python3 -m pip install -U "discord.py>=1.4,<1.5"

Is possible by running in the terminal. However, we do not know when the v6 gateway will end support, so we recommend that you update your code.

Important: ** After October 7th, you will need to configure it in the Developer Portal (if you want to use it) regardless of the version of discord.py. ** **

If you really don't like the changes and directions of the Discord API, you can also contact Support (https://support.discord.com/hc/ja/requests/new).

wrap up

You can limit the events you receive by setting Intents. Most users can set ʻintent = discord.Intents.all ()` and check both Intents in the Developer Portal.

Recommended Posts

Even monkeys can understand! About Intents on discord.py!
An introduction to Python that even monkeys can understand (Part 3)
An introduction to Python that even monkeys can understand (Part 1)
An introduction to Python that even monkeys can understand (Part 2)
An introduction to Word2Vec that even cats can understand
[Django] About users that can be used on template