How to Code a Discord Bot with Python Part 1

This works, but if you need a tutorial with more detail please head to https://discord.gg/GWdhBSp

Hello! In part one of this tutorial, you will learn how to code a simple Discord bot with Python. If you don’t know what Discord is, it is a chatting service similar to Skype.

Okay. First you are going to go to https://discordapp.com/developers/applications/me .  From there, create a bot application by hitting the “new app” button. Next, title your app. If you want, add the profile picture to it by hitting the button that says “app icon” above it. After that, click “Create App”. If it is successful, it should take you to a page where it shows your bot app, your client id, and client secret. Click on “Create a bot user”. A menu shows up asking whether you want to carry on with this action or not. Click yes. After that, remember to note down your Bot Token for running your bot and your Client ID for adding it to a server. Congratulations, you have completed step 1!

Alright, lets get started with coding the actual bot. First, you are going to need to install the discord.py module. To do this, open your command prompt and enter “pip install discord.py”. Next, open your IDE and create a new script. Title it anything you want. After you create the script, start by importing discord.py and commands from discord.ext.

import discord
from discord.ext import commands

After this, we will need to define our bot. Do this by typing:

bot = commands.Bot(command_prefix='bot prefix', description='description here')

Put you bot’s prefix where it says “bot prefix” and your description where it says “description here”. In my case, I made “test!” my prefix. Now is where the fun begins. First, to make it show when it connects to discord, type the following:

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)

This simply states your bot’s name when it successfully connects to Discord and is ready to be used. But our bot isn’t ready to be run yet, so be patient. Now we will add a command! Start by typing:

@bot.command()
"""Description of command goes here"""
async def hello():
    await bot.say('Hello!')

We just defined a simple command called hello, which makes the bot say hello back to you. Also, before you decide to make a help command, the commands extension will automatically define a command called “help” for your bot, which will display any commands you define. This prevents the need to add the help command yourself. So far your code should look like this:

import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='prefix here', description='your description')

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)

@bot.command()
async def hello():
    await bot.say('Hello!')

This still isn’t enough for it to run, so we will have to add the code that runs it. To do this, get out your Bot Token and add to your code:

bot.run('put your bot token here')

Remember to put your Bot Token where it says to in that piece of code. Congratulations, you now have a fully functional bot! (almost)  Now, to test it, we will add it to our server. To do this, add your client id to the following link where it says CLIENT_ID_GOES_HERE:

https://discordapp.com/api/oauth2/authorize?client_id=CLIENT_ID_GOES_HERE&scope=bot&permissions=0

Next, open the link in your browser. It should come to a page asking you which server you want to add the bot to. Open the menu and click what server you wish to add it to. Press the authorize button. Now, your bot should be in that server. If you take a look at your server, the bot will show as offline. That’s because we haven’t ran the code yet! Now, go to your IDE and hit the run button. After a few seconds it should say “Logged in as (Bot’s name)”. Wow, you just completed your first bot! Now, enter in the chat your command prefix followed by the command we added earlier, “hello”.  For example, since my prefix is “test!”, i would type “test!hello”. Your bot should respond back to you, “Hello!”. That’s all for today. Part 2 will be coming soon! Peace.

tutorialdiscord