Discord Bot - Part 1: Quickstart in Python
Recently, I've been working on Discord Bot development and it took me a while to find all the information I needed. But, once I've found it, I noticed that it could take few minutes instead of hours of research on Google and reading the Discordpy documentation.
In this article, I'll show how to start an application. In the next ones, I'll be showing how to create custom functions, slash commands and even run functions on Google Cloud Platform.
You can also check all my articles related:
First things first - Creating your first Discord application (that will run a bot later on)
We need to create an appication on Discord Developer Portal. But, before that, make sure you're logged in on you discord account on Discord Website.
Then, access your Discord Developer Portal Page.
Here, click on "New Application" and give it a gracious name. Then click "Create".
You should be looking your new app's "General Information" page. Here, you can add a picture and a description to your app. Make sure you save your changes clicking on "Save Changes" on the pop-up.
Creating your Bot
Ok, once your application is created, let's create and configure our bot! On the left side menu, click on Bot > Add Bot > Yes, do it!
In this page, you can configure the permissions that your bot is going to have. For now, In "Privileged Gateway Intents" section, check "message content intent". This will allow your bot to read the content of any message sent on the server the Bot is in and all DM (Direct Messages) sent to it.
Then, click "Save Changes" on the pop-up.
Before the next step, we'll need to get our Bot's Token. Right after our Bot's icon, click on "Reset Token" and "Yes, do it!". In my case, I have 2 factor authentication enabled, so I typed it and it showed the happy-greeter's token. Copy it and save it for later.
Now that happy-greeter is created, let's add it to one Discord Server. I highly recommend you to create a "only-you" server.
So, on the side menu, click on "OAuth2" and then "URL Generator". Here, you can add the scopes of our bot. In this case, check "bot" and "applications.commands" that will allow slash commands to your bot.
Recommended by LinkedIn
Once you've clicked it, "bot permissions" section might have appeared. We'll check all text permissions and check "Read Messages/View Channels" of General Permissions.
Notice that right below this section, an URL is created.
Copy it and paste it into your browser and you'll get to this page.
Choose the server you want to add this bot and click on "Continue". Make sure all permissions are checked and click "Authorize".
Tah-dah! Your bot was added to your Server!
Discord configuration done, now, let's look at the code!
For the next steps, you'll need python3, pip and a code editor installed on your PC. I'm using Visual Studio Code as code editor.
Before any coding, you must install discord.py package. To do that, run the following code line on your terminal:
pip install discord.py
You can see the documentation here.
Once you've done, you can create a file or copy it from my GitHub Repository. You'll just have to replace 'your-bot-token-here' to your bot's token and run.
Let's say "hi" to our Happy-Greeter!
Some observations
What else can we do?
In Discord, you can make your own slash commands, UI buttons, run cloud functions and the list goes on.
In the next article, let's code our first slash command!
I wrote about it before on my website. I built a discord bot using Python from scratch! It was fun.
Que legal Leandro. Parabéns! Bem explicado e simples de entender. Eu que utilizo bastante o discord, mas sempre via os bots, não tinha conhecimento. Obrigado 🤙