[go: up one dir, main page]

0% found this document useful (0 votes)
451 views12 pages

Discord Bot With Python

This document discusses how to create a Discord bot using Python. It begins with an introduction to Discord and what bots are. Discord is described as a platform for gamers to communicate via voice and text. Bots allow automating tasks like welcoming new members. The document then explains how to make a Discord bot with Python using the discord.py library. It shows how to install discord.py, create a Client object to represent the bot's connection to Discord, and handle the on_ready event. It also demonstrates storing the bot token securely in a .env file rather than directly in the code. Running the bot code connects the bot to Discord.

Uploaded by

Lekhaj G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
451 views12 pages

Discord Bot With Python

This document discusses how to create a Discord bot using Python. It begins with an introduction to Discord and what bots are. Discord is described as a platform for gamers to communicate via voice and text. Bots allow automating tasks like welcoming new members. The document then explains how to make a Discord bot with Python using the discord.py library. It shows how to install discord.py, create a Client object to represent the bot's connection to Discord, and handle the on_ready event. It also demonstrates storing the bot token securely in a .env file rather than directly in the code. Running the bot code connects the bot to Discord.

Uploaded by

Lekhaj G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

DISCORD BOT WITH

PYTHON
By Gajula Lekhaj
Roll no:21951A3316
Introduction:
In a world where video games are so
important to so many people,
communication and community around
games are vital. Discord offers both of
those and more in one well-designed
package. In this tutorial, you’ll learn how
to make a Discord bot in Python so that
you can make the most of this fantastic
platform.
By the end of this article you’ll learn:

•What Discord is and why it’s so


valuable
•What is Bot
•How to make a Discord Bot using Python
•How to create Discord connections
•How to handle events
What Is Discord?
 Discord is a voice and text communication platform for
gamers.
 Players, streamers, and developers use Discord to
discuss games, answer questions, chat while they play,
and much more. It even has a game store, complete with
critical reviews and a subscription service. It is nearly a
one-stop shop 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 to make a Discord bot in Python.
What Is Bot?
 Discord is growing in popularity. As such, automated processes, such as banning
inappropriate users and reacting to user requests are vital for a community to thrive and
grow.
 Automated programs that look and act like users and automatically respond to events and
commands on Discord are called bot users. Discord bot users (or just bots) have nearly 
unlimited applications.
 For example, let’s say you’re managing a new Discord guild and a user joins for the very
first time. Excited, you may personally reach out to that user and welcome them to your
community. You might also tell them about your channels or ask them to introduce
themselves.
 The user feels welcomed and 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 each new member, but you still want to send them something to recognize them as a
new member of the guild.
 With a bot, it’s possible to automatically react to the new member 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 to be creative with bots, once you know how to make them.
How to Make a Discord Bot in Python
 Since you’re learning how to make a Discord bot with Python,
you’ll be using discord.py.
 discord.py is a Python library that exhaustively implements
Discord’s APIs in an efficient and Pythonic way. This includes
utilizing Python’s implementation of Async IO.
 Begin by installing discord.py with pip:

 Now that you’ve installed discord.py, you’ll use it to create


your first connection to Discord!
Creating a Discord Connection
 The first step in implementing your bot user is to create a
connection to Discord. With discord.py, you do this by
creating an instance of Client:
 A Client is an object that represents a connection to Discord.
A Client handles events, tracks state, and generally interacts
with Discord APIs.
 Here, you’ve created a Client and implemented
its on_ready() event handler, which handles the event when
the Client has established a connection to Discord and it has
finished preparing the data that Discord has sent, such as
login state, guild and channel data, and more.
 In other words, on_ready() will be called (and your message
will be printed) once client is ready for further action.
 When you’re working with secrets such as your Discord
token, it’s good practice to read it into your program from an
environment variable. Using environment variables helps you:
 Avoid putting the secrets into source control
 Use different variables for development and production
environments without changing your code
 While you could export DISCORD_TOKEN={your-bot-token},
an easier solution is to save a .env file on all machines that will
be running this code. This is not only easier, since you won’t
have to export your token every time you clear your shell, but
it also protects you from storing your secrets in your shell’s
history.
 Create a file named .env in the same directory as bot.py:
 You’ll need to replace {your-bot-token} with your bot’s
token, which you can get by going back to the Bot page on
the Developer Portal and clicking Copy under
the TOKEN section:
 Looking back 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 your shell’s environment variables so that
you can use them in your code.
 Install dotenv with pip:

 Finally, client.run() runs your Client using your bot’s token.


 Now that you’ve set up both bot.py and .env, you can run
your code:

You might also like