On Discord, people interact to share ideas, hash out projects, and chat most life and hobbies in general. But Discord bots can make your chat rooms more fun in improver to automating tasks. These tin can be anything from telling random jokes to playing specific music and more than.

Nonetheless, there are some essential steps you must follow while learning how to make a Discord bot. We'll explore them in this article.

Let's go started.

1. Create Your Discord Server

Before you create a Discord bot, you lot have to beginning by creating a server, as this is the bot'southward place of assignment.

A Discord server or chat room is a space where y'all manage channels and communications on the platform.

To create a Discord server, head over to the Discord website and log in to your Discord dashboard. Or create an account if y'all don't already take i.

Once in your Dashboard, follow these steps to brand a Discord server:

  1. Click the addition (+) icon on the left sidebar.
    Discord dashboard-create server
  2. Select the Create My Own option.
    Discord server creation
  3. Choose a purpose for creating your server.
  4. Customize your server with a contour picture and a name. Then click Create.
    Discord server naming and customization

You've now created a Discord server and are gear up to make a bot for decision-making sure activities on it.

two. Set up Up and Create Your Discord Bot

Next, you want to create a Discord bot inside a Discord application by going to the Discord Programmer Portal. Or you can blazon the following URL in your browser accost field instead:

          https://discord.com/developers/applications                  

In one case in the programmer console:

  1. Click New Application at the acme-left.
    Discord developer console
  2. Provide a name for your application in the given field. Then click Create.
    Discord developer console bot creation
  3. Look to the left sidebar, and select Bot.
  4. Click Add Bot at the extreme correct.
    Discord developer dashboard-bot
  5. From the side by side prompt, cull Aye, exercise it!
    Discord developer dashboard bot step
  6. Click Copy to copy your bot token. Create a text file within your project root and paste information technology there. You tin can name the file secret.txt. Brand sure that y'all don't reveal the copied token.
    Doscord developer console token
  7. Roll downward and toggle on the switch under PRESENCE INTENT. Also, switch on the toggle under SERVER MEMBERS INTENT.
    Discord bot intent settings
  8. Side by side, click OAuth2 on the left bar to add authentication priorities to your Discord bot.
  9. Select URL Generator to generate a bot invitation URL for the server you created before.
  10. From the options, under SCOPES, select bot.
    Discord developer console bot URL
  11. Scroll downward to the BOT PERMISSION department and select roles for your Discord bot. In this case, we'll give it the Administrator role.
    Discord bot admin role
  12. Scroll to the lesser and click COPY to copy the generated URL to your clipboard.

Now to link your server to the bot.

Every bit we mentioned, a bot works primarily inside the server. Hence, later creating a Discord bot every bit we did, yous'll take to assign it to a server where you want it to piece of work.

And then once you lot copy the generated URL, follow these next steps to link your conversation room to the Discord bot:

  1. Paste the copied URL in your browser's address field and log on to it.
  2. From the prompt, click Select a server and choose the one you lot created earlier.
    Discord server bot link interface
  3. Click Keep to proceed.
  4. You'll see a new bill of fare with a list of permissions you lot gear up earlier. Select Authorize.
    Discord bot link to server
  5. Solve the CAPTCHA to complete authorizing your Discord bot.

You've now created a Discord bot. Go back to your server dashboard, and you'll encounter the robot on the right sidebar. Simply as yous can see, it's currently offline. So information technology doesn't work yet until you program it to do so.

To make your bot piece of work, you lot need to write a script in whatever programming linguistic communication that works all-time for yous.

3. Programme Your Discord Bot Using Python

For this tutorial, we'll utilise Python and host the Discord bot on the local automobile. You can host yours on deject services like Heroku if you lot desire in one case everything is fix. Let's swoop in.

Set upward Python and Libraries

To program your Discord bot with Python, you lot'll employ the discord.py module, which simply works with Python 3.five.3 or afterward.

If you lot've not done so already or have an before version, become to python.org to download the latest Python version.

Yous tin can check the Python version installed on your PC past entering the following control in your terminal:

          python            --version                  

If Python is upwardly to date, open the terminal to your project root. Then activate a Python virtual environs.

Also, yous'll accept to install discord.py and a vocalisation support package chosen discord.py[vocalization].

With the virtual environs active, run the following control in your last to install these packages:

                      pip            install            -U            discord            .py            discord            .py            [voice]                  

Send a Message With Your Discord Bot

Hither, yous'll program your Discord bot to answer with a custom message when you send a text in a aqueduct.

Simply first, blazon the post-obit lawmaking at the top of your script to initialize your Discord bot:

                      import            discord            
# Import the commands module:
from discord.ext import commands
# Initialize the bot example and use a blank prefix:
bot = commands.Bot(command_prefix=" ")
#Read your bot token from the txt file in your project root:
Secret = open("secret.txt", 'r')
Undercover = Secret.read()
#Run the bot in an event loop:
bot .run(Secret)

The bot variable invokes custom commands from the command class. And the command_prefix in the parenthesis lets you specify a graphic symbol that precedes it. Characters like the dollar sign ($), assertion (!), ampersand (&), and more are common prefixes that precede bot commands.

Just we've blanked the prefix in our case. So you don't need ane while instructing the Discord bot.

Using the @bot.command() decorator, permit's make a custom command, Hi, and provide a respond text for the Discord bot when it sees it.

Note that custom commands are Python functions:

                      import            discord            
# Import the commands module:
from discord.ext import commands
# Initialize the bot instance:
bot = commands.Bot(command_prefix=" ")
@bot.command()
async def Hi (ctx):
await ctx.send("Hi, welcome to our server")
Surreptitious = open("clandestine.txt", 'r')
Secret = Hush-hush.read()
bot .run(Secret)

Now run the Python script in your terminal. And so go to Discord and send "Hello" to run into the magic unfold.

Looking closely, you'll see that we've used the async await method to serve the function. This is essential, as it'southward the just way for the module to communicate with Discord.

The ctx instance is a context, and it has many functions. But in this example, information technology allows your Discord bot to transport a message.

Welcome New Aqueduct Members With Your Bot

In this example, you'll see how to use events to trigger a greeting when someone joins your server. You'll also learn how to brandish the username of a new fellow member and the server they've joined in the greeting message.

We'll use @bot.event here instead. Here'southward how that works:

                      @bot.event
async def on_member_join (member):
guild = member.lodge
if guild.system_channel is not None:
detailMessage = 'We welcome {0.mention} to the {1.name}!'.format(member, guild)
expect guild .system_channel .send(detailMessage)

The on_member_join function is a type of result. There are many others, though. The on_ready method, for instance, is a widely used effect for checking if a bot is ready or not.

Further, the condition that follows the if statement checks if the connected server (guild or chat room) exists or not. If it does, it uses the Python string format method to output the new member's username and the server they've joined.

Now ask a friend to join your server to see what happens. You might desire to endeavour this showtime with a dummy Discord account, though, to ensure that it works.

Ask Discord Bot to Join or Leave an Sound Channel

Now let's expand the code. And this time, y'all'll tell your bot to join or go out an sound aqueduct when it receives a particular control.

In the example code below, the Discord bot joins yous in an sound aqueduct when yous type "enter":

                      @bot.command()
async def enter (ctx):
if ctx .author .voice:
await ctx .bulletin .writer .vocalisation .channel .connect()

The status within the if statement checks if y'all've joined an audio aqueduct already. If so, the await keyword connects your Discord bot with it.

It means you lot must've joined an audio channel earlier the command will work.

Note: Ensure that you stop and restart your Python script after each update. Information technology lets Python sync your changes.

One time you re-execute your Python script, open up Discord, and type enter—you lot'll now see that your Discord bot has joined the sound channel.

Request your bot to leave the channel is as easy as calculation it.

The post-obit lawmaking tells it to exit the channel when yous type the appropriate command. Nosotros'll utilise the word "leave" in this instance:

                      @bot.control()
async def leave (ctx):
if ctx .voice_client:
expect ctx .guild .voice_client .disconnect()

Stop and execute your script once again. The Discord bot should now leave the audio channel when you send the "leave" command.

Keep Improving Your Discord Bot

That'south it! You lot've made yourself a functional Discord bot. Put the case blocks of code together and continue adding features to your Discord bot. For instance, yous tin tell information technology to play music from your PC or a streaming platform as soon as it joins an audio channel.

Discord bots are fun to employ. If you don't yet automate tasks with them in your chat rooms, you're missing out on some serious efficiency.

How to Add together Bots to Your Discord Server

Read Next

Nigh The Writer