Knowledgebase

How to Run a Python Discord Bot on Debian 10 Print

  • 0

Introduction

Discord is a video, voice, and text communication platform that allows bots to perform automated tasks based on user commands. This article explains how to run a Python-based Discord bot and host it on a Debian 10 cloud server at Rcs.

1. Create a Discord Application

  1. Go to the Discord Developer Portal and login.
  2. Click the New Application button.
  3. Enter an application name and click Create.
  4. Select the Bot tab from the Settings menu.
  5. Click Add Bot then click Yes, do it!.
  6. Click Copy to copy your bot's token to the clipboard. Save a copy to use later in this guide.

2. Install Prerequisites

Install the Discord API wrapper for Python.

# pip install discord.py

3. Create the Discord Bot

  1. Switch to your home directory.

     # cd ~
  2. Create a project folder.

     # mkdir discord-bot
  3. Switch to the project folder.

     # cd discord-bot
  4. Create a python file named discord_bot.py.

     # nano discord_bot.py
  5. Paste this sample code to your discord_bot.py file. Replace <YOUR BOT TOKEN> with the token you saved in section 1.

     import discord
    
     TOKEN = '<YOUR BOT TOKEN>'
    
     client = discord.Client()
    
     @client.event
     async def on_ready():
         print('We have successfully loggged in as {0.user}'.format(client))
    
    
     @client.event
     async def on_message(message):
         if message.author == client.user:
             return
    
         if message.content.lower() == 'hello':
             await message.channel.send(f'Hello, {message.author.display_name}!')
             return
    
         if message.content.lower() == 'bye':
             await message.channel.send(f'See you later, {message.author.display_name}!')
             return
    
     client.run(TOKEN)
  6. Save and exit the file.

4. Run the Discord Bot

Your Discord bot stops running when you exit the SSH session. To keep the bot running full-time, use Tmux or PM2.

Option 1: Run the Bot with Tmux

  1. Follow this guide to install Tmux.

  2. Create a tmux session.

     # tmux new -s DiscordBot
  3. Change to the Discord bot project directory.

     # cd ~/discord-bot 
  4. Start the bot.

     # python3 discord_bot.py

To stop the Discord Bot, press Ctrl + C

Option 2: Run the Bot with PM2

PM2 is a process manager for Python.

  1. Install npm.

     # apt install npm -y 
  2. Install PM2.

     # npm install -g pm2
  3. Change to the Discord bot project directory.

     # cd ~/discord-bot 
  4. Start the bot.

     # pm2 start discord_bot.py --interpreter=/usr/bin/python3

Common PM2 Commands

Make sure you are in the ~/discord-bot directory, then use these commands to control your bot.

  • List all PM2 processes:

      # pm2 list
  • Stop the Discord bot:

      # pm2 stop discord_bot.py
  • Restart the Discord bot:

      # pm2 restart discord_bot.py

5. Invite the Bot to a Discord Server

  1. Go to the Discord Developer Portal and log in.
  2. Click the Application that you have created.
  3. Navigate to the OAuth2 Tab from the Settings menu.
  4. In the Scopes Section, select bot.
  5. In the Bot Permissions Section, select Administrator. This gives the bot an Administrator Role from the Discord Server.
  6. Click the Copy Button to copy the generated invite link.
  7. Open the invite link on your browser.
  8. Select the Discord server for the bot, then click Continue.
  9. Authorize the bot and complete the CAPTCHA.

The Discord bot is now up and running.

More Information

To learn more about Discord bots, please see these resources:

Introduction Discord is a video, voice, and text communication platform that allows bots to perform automated tasks based on user commands. This article explains how to run a Python-based Discord bot and host it on a Debian 10 cloud server at Rcs. 1. Create a Discord Application Go to the Discord Developer Portal and login. Click the New Application button. Enter an application name and click Create. Select the Bot tab from the Settings menu. Click Add Bot then click Yes, do it!. Click Copy to copy your bot's token to the clipboard. Save a copy to use later in this guide. 2. Install Prerequisites Deploy a Rcs Debian 10 cloud server instance Log in as root Update the Debian server Update Python to the latest version Install the Discord API wrapper for Python. # pip install discord.py 3. Create the Discord Bot Switch to your home directory. # cd ~ Create a project folder. # mkdir discord-bot Switch to the project folder. # cd discord-bot Create a python file named discord_bot.py. # nano discord_bot.py Paste this sample code to your discord_bot.py file. Replace with the token you saved in section 1. import discord TOKEN = '' client = discord.Client() @client.event async def on_ready(): print('We have successfully loggged in as {0.user}'.format(client)) @client.event async def on_message(message): if message.author == client.user: return if message.content.lower() == 'hello': await message.channel.send(f'Hello, {message.author.display_name}!') return if message.content.lower() == 'bye': await message.channel.send(f'See you later, {message.author.display_name}!') return client.run(TOKEN) Save and exit the file. 4. Run the Discord Bot Your Discord bot stops running when you exit the SSH session. To keep the bot running full-time, use Tmux or PM2. Option 1: Run the Bot with Tmux Follow this guide to install Tmux. Create a tmux session. # tmux new -s DiscordBot Change to the Discord bot project directory. # cd ~/discord-bot Start the bot. # python3 discord_bot.py To stop the Discord Bot, press CTRL + C Option 2: Run the Bot with PM2 PM2 is a process manager for Python. Install npm. # apt install npm -y Install PM2. # npm install -g pm2 Change to the Discord bot project directory. # cd ~/discord-bot Start the bot. # pm2 start discord_bot.py --interpreter=/usr/bin/python3 Common PM2 Commands Make sure you are in the ~/discord-bot directory, then use these commands to control your bot. List all PM2 processes: # pm2 list Stop the Discord bot: # pm2 stop discord_bot.py Restart the Discord bot: # pm2 restart discord_bot.py 5. Invite the Bot to a Discord Server Go to the Discord Developer Portal and log in. Click the Application that you have created. Navigate to the OAuth2 Tab from the Settings menu. In the Scopes Section, select bot. In the Bot Permissions Section, select Administrator. This gives the bot an Administrator Role from the Discord Server. Click the Copy Button to copy the generated invite link. Open the invite link on your browser. Select the Discord server for the bot, then click Continue. Authorize the bot and complete the CAPTCHA. The Discord bot is now up and running. More Information To learn more about Discord bots, please see these resources: Discord's documentation Discord developer portal Discord.py's documentation

Was this answer helpful?
Back

Powered by WHMCompleteSolution