sodcalculator

10 Tech How-To

How to Connect a Chatbot to Discord

Guide 10-9981 · Updated · Step by step

Connecting a chatbot to Discord lets you automate moderation, answer questions, or run fun commands in your server. This guide walks you through creating a bot application, inviting it to your server, and writing a simple Python script that makes it respond to messages. You’ll need a Discord account, a server where you have the “Manage Server� permission, and a basic understanding of Python (or you can use a no-code platform like Zapier).

What You’ll Need

  • A Discord account (free) and a server you own or administer.
  • Python 3.8 or newer installed on your computer (if coding).
  • A code editor or text editor (like VS Code, Notepad++, or even Notepad).
  • An internet connection and the ability to install Python packages via pip.

Step-by-Step

  1. Go to the Discord Developer Portal. Open your web browser and navigate to discord.com/developers/applications. Log in with your Discord account.
  2. Create a new application. Click the “New Application� button in the top-right corner. Give it a name (this will be the bot’s name) and click “Create�.
  3. Go to the Bot section. On the left sidebar, click “Bot�. Then click “Add Bot� and confirm with “Yes, do it!�. You’ll see a token—this is the bot’s password. Click “Reset Token� and copy the new token. Keep it secret; anyone with it can control your bot.
  4. Set bot permissions. In the Bot section, under “Privileged Gateway Intents�, enable “MESSAGE CONTENT INTENT� (required for reading message content in most cases). Also, you can toggle “Public Bot� off if you want only you to add it.
  5. Invite the bot to your server. In the left sidebar, click “OAuth2� then “URL Generator�. Under “Scopes�, check “bot�. Under “Bot Permissions�, select the permissions your bot needs—for a simple responder, “Send Messages� and “Read Message History� are enough. Copy the generated URL at the bottom, open it in a new tab, choose your server from the dropdown, and click “Authorize�. Complete the CAPTCHA if asked.
  6. Install the discord.py library. Open a terminal or command prompt and run: pip install discord.py. If you’re on a Mac or Linux, you might need pip3 instead.
  7. Write the bot script. Create a new file named bot.py. Paste the following code:
    import discord
    from discord.ext import commands
    
    intents = discord.Intents.default()
    intents.message_content = True
    
    bot = commands.Bot(command_prefix='!', intents=intents)
    
    @bot.event
    async def on_ready():
        print(f'Logged in as {bot.user}')
    
    @bot.command()
    async def hello(ctx):
        await ctx.send('Hello! I am a bot.')
    
    bot.run('YOUR_BOT_TOKEN')
        

    Replace YOUR_BOT_TOKEN with the token you copied earlier.

  8. Run the bot. In the terminal, navigate to the folder containing bot.py and run python bot.py (or python3 bot.py). You should see “Logged in as YourBotName�.
  9. Test the bot. Go to your Discord server, type !hello in any text channel, and the bot should reply “Hello! I am a bot.�

Common Mistakes

  • Not enabling the Message Content Intent. Without it, the bot cannot see the content of messages, so commands won’t work.
  • Sharing the bot token. If you accidentally post it online, anyone can take over your bot. Reset it immediately.
  • Using the wrong prefix. If you type !hello but your bot is set to use ?, it won’t respond. Check your command prefix.
  • Forgetting to install discord.py. If you get ModuleNotFoundError, you didn’t install the library.

Tips

  • Use environment variables to store your token. Instead of hardcoding it, set an environment variable like DISCORD_TOKEN and read it with os.getenv('DISCORD_TOKEN'). This keeps your token safer.
  • Add a !ping command to check latency: await ctx.send(f'Pong! {round(bot.latency * 1000)}ms').
  • Use a slash command instead of prefix commands for a more modern feel. With discord.py, you can use @bot.tree.command() and sync them.
  • Keep the bot running 24/7 by hosting it on a free cloud service like Replit or Heroku (though Heroku’s free tier ended). For learning, running it locally is fine.

Troubleshooting

Bot doesn’t respond to commands. First, check the terminal for errors. If there’s an error about intents, make sure you enabled the Message Content Intent in the Developer Portal and that your code sets intents.message_content = True. Also verify that the bot is online (green dot in the member list).

“Privileged intent provided is not enabled or whitelisted.� This error appears when the intent is not enabled in the portal. Go to the Bot section and toggle “MESSAGE CONTENT INTENT� on.

Bot joins the server but is offline. This usually means the bot script isn’t running. Make sure you ran python bot.py and that the terminal doesn’t show any errors.

“Invalid token� error. You likely copied the wrong token or included quotes. Copy the token again from the Developer Portal, and make sure it’s inside the quotes in your code.

Command works but only sometimes. Check if your bot has the “Read Message History� permission in the channel. Also, if you have multiple bots, they might conflict on the same prefix.

Frequently Asked Questions

Can I connect a chatbot without coding? Yes. Platforms like Zapier, IFTTT, or bot builders like BotGhost allow you to create Discord bots with a visual interface. You still need to create the bot application in the Developer Portal to get a token.

How do I change the bot’s prefix? In your code, change the command_prefix argument in commands.Bot(). For example, command_prefix='?'. You can also use a list of prefixes like ['!', '?'].

Can I use JavaScript instead of Python? Yes. Use discord.js with Node.js. The steps for creating the bot application are the same; only the code differs.

Why does my bot go offline when I close the terminal? Because it’s running on your local machine. To keep it online 24/7, you need to host it on a server or a cloud platform.

How do I add more commands? Simply define more functions with @bot.command() decorator. For example, async def ping(ctx): await ctx.send('Pong!').

Is it safe to use a bot token in my code? For learning, it’s okay, but for production, always use environment variables or a secrets manager. Never commit your token to a public repository.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →