Hệ thống đang bảo trì rà soát thông tin



Watch Now This tutorial has a related Clip course created by the Real Python team. Watch it together with the written tutorial khổng lồ deepen your understanding: Creating a Discord Bot in Python

In a world where Clip games are so important to so many people, communication & community around games are vital. Discord offers both of those & more in one well-designed package. In this tutorial, you’ll learn how khổng lồ make a Discord bot in Pykhiêm tốn so that you can make the most of this fantastic platkhung.

Bạn đang xem: Cách tạo bot discord

By the end of this article you’ll learn:

What Discord is and why it’s so valuableHow lớn make a Discord bot through the Developer PortalHow lớn create Discord connectionsHow lớn handle eventsHow khổng lồ accept commands & validate assumptionsHow lớn interact with various Discord APIs

You’ll begin by learning what Discord is and why it’s valuable.

What Is Discord?

Discord is a voice and text communication platsize for gamers.

Players, streamers, & developers use Discord khổng lồ discuss games, answer questions, chat while they play, và much more. It even has a game store, complete with critical Review & a subscription service. It is nearly a one-stop siêu thị for gaming communities.

While there are many things you can build using Discord’s APIs, this tutorial will focus on a particular learning outcome: how lớn make a Discord bot in Pydong dỏng.

What Is a Bot?

Discord is growing in popularity. As such, automated processes, such as banning inappropriate users and reacting khổng lồ user requests are vital for a community lớn thrive and grow.

Automated programs that look and act like users và automatically respond lớn events and commands on Discord are called bot users. Discord bot users (or just bots) have sầu nearly unlimited applications.

For example, let’s say you’re managing a new Discord guild và a user joins for the very first time. Excited, you may personally reach out to lớn that user and welcome them khổng lồ your community. You might also tell them about your channels or ask them to lớn introduce themselves.

The user feels welcomed và enjoys the discussions that happen in your guild and they, in turn, invite friends.

Over time, your community grows so big that it’s no longer feasible to personally reach out to lớn each new thành viên, but you still want to lớn skết thúc them something khổng lồ recognize them as a new thành viên of the guild.

With a bot, it’s possible khổng lồ automatically react to the new thành viên joining your guild. You can even customize its behavior based on context and control how it interacts with each new user.

This is great, but it’s only one small example of how a bot can be useful. There are so many opportunities for you khổng lồ be creative with bots, once you know how to lớn make them.

Note: Although Discord allows you to lớn create bots that khuyến mãi with voice communication, this article will stick to lớn the text side of the service.

There are two key steps when you’re creating a bot:

Create the bot user on Discord & register it with a guild.Write code that uses Discord’s APIs & implements your bot’s behaviors.

Xem thêm: Dominance Là Gì - Tầm Quan Trọng Của Chỉ Số Này Đến

In the next section, you’ll learn how lớn make a Discord bot in Discord’s Developer Portal.

How to Make a Discord Bot in the Developer Portal

Before you can dive inkhổng lồ any Pythuôn code to handle events and create exciting automations, you need to lớn first create a few Discord components:

An accountAn applicationA botA guild

You’ll learn more about each piece in the following sections.

Once you’ve created all of these components, you’ll tie them together by registering your bot with your guild.

You can get started by heading to lớn Discord’s Developer Portal.

Creating a Discord Account

The first thing you’ll see is a trang đích cần seo where you’ll need to lớn either login, if you have sầu an existing tài khoản, or create a new account:


Next, you’ll be prompted to lớn name your application. Select a name & click Create:


Keep in mind that any program that interacts with Discord APIs requires a Discord application, not just bots. Bot-related APIs are only a subset of Discord’s total interface.

However, since this tutorial is about how lớn make a Discord bot, navigate khổng lồ the Bot tab on the left-hvà navigation danh sách.

Creating a Bot

As you learned in the previous sections, a bot user is one that listens lớn and automatically reacts to certain events và commands on Discord.

For your code to lớn actually be manifested on Discord, you’ll need to create a bot user. To vì so, select Add Bot:


Notice that, by default, your bot user will inherit the name of your application. Instead, update the username to something more bot-lượt thích, such as dnth.vnTutorialBot, và Save Changes:


From this trang chủ page, you can view and add friends, direct messages, & guilds. From here, select the + icon on the left-hand side of the website page khổng lồ Add a Server:


Once you’ve sầu finished creating your guild, you’ll be able khổng lồ see the users on the right-hand side and the channels on the left:


From this window, you’ll see the OAuth2 URL Generator.

This tool generates an authorization URL that hits Discord’s OAuth2 API and authorizes API access using your application’s credentials.

In this case, you’ll want to grant your application’s bot user access to lớn Discord APIs using your application’s OAuth2 credentials.

To vì chưng this, scroll down & select bot from the SCOPES options & Administrator from BOT PERMISSIONS:


Clichồng Authorize, và you’re done!

Note: You might get a reCAPTCHA before moving on. If so, you’ll need lớn prove you’re a human.

If you go baông chồng to lớn your guild, then you’ll see that the bot has been added:


Looking baông chồng at the bot.py code, you’ll notice a library called dotenv. This library is handy for working with .env files. load_dotenv() loads environment variables from a .env file into lớn your shell’s environment variables so that you can use them in your code.

Install dotenv with pip:

$ pip install -U python-dotenv
Finally, client.run() runs your Client using your bot’s token.

Now that you’ve phối up both bot.py and .env, you can run your code:

$ python bot.pydnth.vnTutorialBot#9643 has connected lớn Discord!
Great! Your Client has connected to Discord using your bot’s token. In the next section, you’ll build on this Client by interacting with more Discord APIs.