In order for our bot to do anything, we need to register a handler for the appropriate event with our Client object. For a complete list of the events that discord.py supports, check out the relevant documentation.
An event occurs anytime something happens in the Discord server.įor example, a client connecting or disconnecting, a user sending a message, or a user joining a group are all examples of events. We can create a Client object like this client = discord.Client()ĭiscord’s API is structured around the notion of events. This object will connect to Discord and allow us to interact with messages, channels, users, etc. To do anything with Discord’s API, we need to create a Client object. TOKEN = os.getenv('DISCORD_TOKEN')Īs you can see, this assumes that the token will be stored in the environment variable DISCORD_TOKEN. Next, we need to load the token that authenticates our bot to connect to Discord. Note that discord is the discord.py library that we just installed. First, let’s import the libraries that we are going to need… import os That way, when you are done, your bot will be ready to run.
DISCORD ACCOUNT GENERATOR BOT CODE CODE
As you follow along, I would suggest adding these lines of code to a Python file. Now that we have this library, we can finally begin to write our bot.
DISCORD ACCOUNT GENERATOR BOT CODE INSTALL
First, install it by running… python3 -m pip install -U discord.py To do so, we are going to use the library discord.py. Now, you just have to write the code for the bot itself in Python. Coding The Discord Bot with PythonĪt this point, you have created the Discord resources that you will need to create your bot. You may have to complete a reCAPTCHA after clicking Authorize to confirm your identity. Then click Authorize to grant your bot access. Under Add to Server select the server that you just created and then click Continue.
Navigate to this URL using your web browser. Now, copy the generated URL by clicking copy to the right of the URL that should appear under the Scopes section. Instead, more specific and detailed permissions should be used. While giving the bot administrator permissions is the easiest for this tutorial, it may be dangerous to do so in production. Under Scopes select bot and then under Bot Permissions select Administrator. You should see a section called OAuth2 URL Generator. Return to the Developer Portal and select your Application again. Now it is time to add your bot to your server. You should now see this server appear in the Discord interface along with its current channels and users. Click Create My Own and then For me and my friends to initialize some settings for your new server, then give it a name that you will remember. Navigate back to your home page and click the + button to add a new server. Now you have a bot!įor your bot to talk to users, however, you will need to create a server for it to inhabit. Give the bot a new name and click Save Changes.
By default, it will have the same username as its parent Application. To do so, navigate to the Bot tab under the page for the Application that you just created. Next, you need to create the bot user itself. You should now see a screen that displays information (such as the id and public key) about your Application. Make sure to give the Application a unique name. To create an Application, navigate to the Applications tab in the Developer Portal and select the New Application button. An Application will allow you to access Discord’s API (Application Programming Interface) and begin writing your bot. Now that your account is functional, the first step in creating a Discord bot is to create an Application. If you already have a Discord account, you can skip this step.Īfter registering your account (by clicking the register button under the sign-in dialog), you will need to verify your email address. To create a bot, you must first register an account on Discord’s Developer Portal. It also assumes that you have a working Python 3 environment and access to a command line and text editor.
This tutorial will assume that you have some background with Python but does not assume any experience working with Discord or bot-writing in general.