Migrating from discord.py
After the discontinuation of discord.py (refer this gist), many forks of the API wrapper branched onward to maintain the library, in order to keep it updated with the latest features and Discord API changes - disnake is one such fork.
Thus, if you've chosen disnake as your fork of choice in order to implement interactions/components and other features, this page will help you understand the changes in syntax, and aim for making your migrating process as smooth as possible.
Differences between libraries
disnake is based on discord.py 2.0, which had major syntax changes from its previous version. Therefore, if you're shifting to disnake from a version of discord.py lower than 2.0, you will have to make some important syntax changes in your code. You can refer this page for the full list of breaking changes in discord.py 2.0, though we will list some primary API reference changes here:
- Methods and attributes that returned
TextChannel, etc can now returnThread. - Attributes that returned
Assetare renamed, e.g. attributes ending with_url(i.e.avatar_url) are changed toavatar.url.User.avatarreturnsNonein case the default avatar is used. on_presence_updatereplaceson_member_updatefor updates toMember.statusandMember.activities.- Webhooks are changed significantly:
WebhookAdapteris removed, and synchronous requests usingrequestsis now insideSyncWebhook. editmethods no longer updates cache and instead returns modified instance.Client.logoutis removed; useClient.closeinstead.Message.typefor replies are nowMessageType.reply.Reaction.custom_emojiproperty is changed toReaction.is_custom_emojimethod.missing_permsattributes and arguments are renamed tomissing_permissions.- Many arguments are now specified as positional-only or keyword-only; e.g.
oauth_urlnow takes keyword-only arguments, and methods starting withget_orfetch_take positional-only arguments.
Changing requirements
In order to avoid conflicts between the libraries, you must uninstall discord.py. You can do so by using the following command in your terminal:
py -3 -m pip uninstall discordTo install disnake, you can follow the instructions on this page.
Rewriting your bot
As discussed above, rewriting your code from an older discord.py version to disnake will require some major syntax
changes. But if you're migrating from discord.py 2.0, all that's left now is changing the library references
throughout the code, since the base code for both the libraries is practically the same.
There are three ways to switch between libraries:
Replace discord with disnake
- Import
disnakeinto your code (and delete the lines where you importdiscord).import disnakefrom disnake.ext import commands - With your favorite editor, replace every
discordreference in your code withdisnake(this is fairly simple, if your editor has a "Find & Replace" tool).
Import disnake as discord
Import disnake as discord into your code (and delete the lines where you import discord). This reduces the effort of
changing all references throughout your code.
import disnake as discordfrom disnake.ext import commandsUsing the discord shim
Using the discord shim allows you to use disnake, without the need to import it directly or importing it as
discord - thus eliminating the need to change your code at all. To install the shim, you can use the following command
in your terminal:
py -3 -m pip install disnake[discord]And that's it! Since disnake is a fork of discord.py, it inherits a lot of similarities - though we recommend you to
always run your code to fix any possible issues.