In this article, we will discuss how to build a Discord bot using Python and discord.py library. We will cover the following topics:
- Installing Python and discord.py
- Creating a new Discord application and generating a token
- Writing code for the bot
- Deploying the bot on Heroku
Let's get started!
**Step 1:** Install Python and discord.py library
If you don't have Python installed on your machine, go to https://www.python.org/downloads/ and download the latest version of Python for your operating system.
Once Python is installed, open a terminal or command prompt window and type:
```bash
pip install discord.py[voice]
```
This will install the discord.py library along with its dependencies.
**Step 2:** Create a new Discord application and generate a token
Go to https://discordapp.com/developers/applications/me and click on "New Application". Enter a name for your bot, select an icon if you want, then click on "Save Changes".
On the left side menu, click on "Bot" and scroll down until you see the "TOKEN" section. Click on "Click to Reveal Token" button to generate a token for your bot. Keep this token safe as anyone who has access to it can control your bot.
**Step 3:** Write code for the bot
Create a new file named `bot.py` and add the following code:
```python
import discord
# Replace with your bot's token
TOKEN = 'your-bot-token-here'
client = discord.Client()
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
@client.event
async def on_message(message):
if message.author == client.user:
return
# Replace with your own command prefix
if message.content.startswith('!'):
await message.channel.send('Hello, ' + message.author.mention + '!')
client.run(TOKEN)
```
This is a very basic bot that responds to messages starting with an exclamation mark (`!`) by sending back "Hello, [user]!" where `[user]` is the person who sent the message. You can replace this command and response with anything you like.
**Step 4:** Deploy the bot on Heroku
Go to https://www.heroku.com/ and sign up for a free account if you don't have one already. Then, click on "New" -> "Create new app". Enter a name for your app, select a region close to you, then click on "Create App".
On the next screen, scroll down until you see the "Config Vars" section and add two variables: `TOKEN` with value equal to your bot's token, and `PYTHONUNBUFFERED` with value set to `1`.
Next, click on "Deploy" -> "GitHub" -> "Connect to GitHub". Search for your repository containing the `bot.py` file, then click on "Connect".
Finally, scroll down again until you see the "Automatic Deploys" section and enable automatic deploys by clicking on the toggle button. This will ensure that any changes made to your code are automatically deployed to Heroku.
That's it! Your bot should now be up and running. You can invite it to a server by going to https://discordapp.com/oauth2/authorize?client_id=your-bot-id-here&scope=bot, then clicking on "Copy" next to the "Client Secret" field, and finally pasting this value into the "Access Token" field of a Discord OAuth2 URL generator like https://discord.com/oauth2/generate.
I hope you found this tutorial helpful! If you have any questions or comments, feel free to leave them below.