Skip to content

Instantly share code, notes, and snippets.

@FallenDeity
Last active September 18, 2023 10:25
Show Gist options
  • Save FallenDeity/065f488657c0b59bbfbe6cac551bd72d to your computer and use it in GitHub Desktop.
Save FallenDeity/065f488657c0b59bbfbe6cac551bd72d to your computer and use it in GitHub Desktop.

Breaking Changes for discord.py 2.0


Upon resumation of the most popular discord API wrapper library for python, discord.py, while catching on to the latest features of the discord API, there have been numerous changes with addition of features to the library. Some additions to the library are -> Buttons support, Select Menus Support, Forms (AKA Modals), Slash Commands (AKA Application Commands) and a bunch of more handy features! All the changes can be found here. Original discord.py gist regarding resumation can be found here.


Requirements


First of all, you should have some fundamental understanding of Python:

  • Basic knowledge of variables, data types, keywords and some commonly used in-built functions in Python
  • String methods, List methods, Dictionary methods, Set methods
  • Familiar with asynchronous programming within Python. [Use case of keywords such as async and await and the library asyncio]
  • Have familiarity with Object Oriented Programming within Python
  • An understanding of indentation
  • Know scopes of variables, et cetera
  • Importing of libraries and local files into each other
  • Type Hinting of variables and parameters!

Steps to Get Started


  1. Get Python 3.8 or higher

Make sure you have python version of 3.8 or higher. discord.py requires Python of version 3.8 or higher. You can download it over here.

NOTE: Don't forget to click add to path check box when installing Python. This will definitely save you from some common issues if you are a beginner.

  • Install Python
  • Add it to PATH!
  1. Update your discord.py

The latest discord.py was released on pypi on August 18th 2022. BEFORE MIGRATING TO DISCORD.PY 2.0, PLEASE READ THE CONSEQUENCES OF THE UPDATE HERE. Since there are many breaking changes which may break your existing bot's code.

pip install -U discord.py  # or pip install discord.py 

Major changes we will be covering in this gist


  1. Intents

So this a new change in 2.0 intents are now required for the bot to run. The intents kwarg from discord.Client or any of it's subclasses can be used to specify your intents.

  1. Extension Loading

Adding and loading cog extensions are now asynchronous.

  1. Setup Hook

A new setup_hook() method has also been added to the Client class. This method is called after login but before connecting to the discord gateway. This method can be used for various stuff to setup your bot before starting. For example loading cogs, add database pool connections etc. In general methods that need to be run in asynchronous context.

  1. AsyncIterators returned for certain functions

The following functions now return an AsyncIterator objects.

  1. abc.Messageable.history()
  2. Client.fetch_guilds()
  3. Guild.audit_logs()
  4. Guild.fetch_members()
  5. Reaction.users()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment