Discord.py Introduction - Setting up

Discord.py Introduction - Setting up

Creating a discord bot, and creating commands using Python

·

2 min read

Discord.py is a modern, easy to use, feature-rich, and async ready API wrapper for Discord. It implements the entire Discord API.

Basically, in dummy terms, Discord.py is a version of Python which you can use to code a discord bot.

In this post I will tell you how to setup a discord bot, and also create a command.

Requirements: Discord Account, Computer

Creating your bot

First off, open your web browser, and head to the Discord Development Portal, and you should be greeted with this screen: image.png Obviously, you won't have all those applications created.

Next, you will need to create an application. Click 'New Application' in the top right of the page, and give it name. Don't worry, you can change this later. You should see this screen: image.png

Click 'Bot', click 'Add bot' and finally, press 'Yes, do it!'

image.png

Leave this tab open, we will need it later.

Creating a simple command

Next, open up your code editor, and make a new file called main.py Now, use pip to install discord.py: pip install discord.py

Copy this code into your file:

import os
import discord
from discord.ext import commands

client = commands.Bot(command_prefix="!")

@client.command()
async def ping(ctx):
    await ctx.send("Pong!")


client.run('YOUR_TOKEN')

Now, where it says client.run('YOUR__TOKEN') you will need to copy and paste your discord bot's token.

To find this token, head to the tab you were on earlier (your discord bot application tab) and under the 'Bot' section you should see a option to copy your bot's token, hit copy, and replace YOUR_TOKEN with that.

Inviting your bot

To invite your bot to a discord server, head back to the applications tab, and click 0Auth2, then URL generator.

image.png From the options available, click 'bot'

image.png Once you have done that, choose the permissions your bot will need. I recommend clicking 'Administrator' for ease.

image.png

Copy the link, paste it into a new tab, invite the bot to your server, run your code, and type !ping. Your bot should reply with pong!

Thanks for reading, if this was helpful, please leave a thumbs up and follow :)