sodcalculator

10 Tech How-To

How to Make a Discord Bot: Step-by-Step Guide

Guide 10-21220 · Updated · Step by step

This guide walks you through creating a Discord bot from scratch, from setting up a developer application to writing and running your first bot script. You’ll learn the essential steps using Node.js and the Discord.js library, and by the end you’ll have a working bot that can respond to commands.

What You’ll Need

  • A Discord account and a server where you have permission to add bots (usually requires “Manage Server” permission).
  • Node.js installed on your computer (version 16.9.0 or higher recommended). Download from nodejs.org.
  • A code editor like Visual Studio Code, Notepad++, or any text editor.
  • Basic familiarity with the command line (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows).

Step-by-Step

  1. Create a Discord Application
    Go to the Discord Developer Portal and log in with your Discord account. Click the “New Application” button, give it a name (this will be your bot’s name), and click “Create”.
  2. Create the Bot User
    In the left sidebar, click “Bot”. Then click “Add Bot” and confirm. You’ll see a token – this is the bot’s password. Keep it secret; anyone with it can control your bot.
  3. Copy Your Bot Token
    On the Bot page, click “Reset Token” and then “Copy” to get a fresh token. Store it somewhere safe (like a .env file) – you’ll need it in your code.
  4. Set Bot Permissions
    Still on the Bot page, under “Privileged Gateway Intents”, enable the intents you need. For a basic bot, enable “Server Members Intent” and “Message Content Intent” (required for reading message commands).
  5. Invite the Bot to a Server
    In the left sidebar, click “OAuth2” then “URL Generator”. Under “Scopes”, check “bot”. Under “Bot Permissions”, choose what your bot needs – for a simple bot, “Send Messages” and “Read Message History” are enough. Copy the generated URL, open it in a browser, select your server, and authorize.
  6. Set Up Your Project Folder
    Create a new folder on your computer (e.g., `my-discord-bot`). Open a terminal in that folder and run `npm init -y` to create a package.json file.
  7. Install Discord.js
    In the same terminal, run `npm install discord.js`. This installs the library you’ll use to interact with the Discord API.
  8. Create the Bot Script
    Create a file named `index.js` (or `bot.js`) in your project folder. This is where your bot’s code will live.
  9. Write Basic Bot Code
    Open `index.js` and paste the following code. Replace `YOUR_BOT_TOKEN` with the token you copied earlier.
    const { Client, GatewayIntentBits } = require('discord.js');
    const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
    
    client.once('ready', () => {
      console.log(`Logged in as ${client.user.tag}!`);
    });
    
    client.on('messageCreate', message => {
      if (message.content === '!ping') {
        message.channel.send('Pong!');
      }
    });
    
    client.login('YOUR_BOT_TOKEN');
  10. Run the Bot
    In the terminal, run `node index.js`. If everything is set up correctly, you’ll see “Logged in as YourBot#1234!” in the console. Your bot is now online.
  11. Test the Bot
    Go to your Discord server and type `!ping` in a channel. Your bot should reply with “Pong!”.

Common Mistakes

  • Sharing your token – Never post it online or commit it to a public repository. Use environment variables to keep it safe.
  • Forgetting to enable intents – If your bot doesn’t respond to messages, you likely didn’t enable the Message Content Intent in the Developer Portal.
  • Not inviting the bot with the right permissions – If the bot can’t send messages, check its role permissions in the server.
  • Using an outdated code example – Discord.js v13+ changed a lot; make sure you’re using v14 syntax (like `GatewayIntentBits`).
  • Not restarting after code changes – You must stop (Ctrl+C) and run `node index.js` again for changes to take effect.

Tips

  • Use a `.env` file to store your token and load it with `dotenv` – this keeps credentials out of your code.
  • Start with simple commands, then expand to slash commands (using `client.slashCommands`) for a better user experience.
  • Use `try/catch` blocks around asynchronous operations to handle errors gracefully.
  • Keep your bot’s code modular – split commands into separate files as it grows.
  • Test your bot on a private server before adding it to a large community.

Troubleshooting

  • Bot doesn’t come online – Check that your token is correct and that you haven’t accidentally added spaces. Also ensure you’re running the right file (`node index.js`).
  • Bot logs in but doesn’t respond to messages – Verify that the Message Content Intent is enabled in the Developer Portal. Also check that the bot has permission to read messages in the channel.
  • “Cannot find module ‘discord.js'” – Run `npm install discord.js` in the correct folder, and make sure you’re running the script from that folder.
  • Bot goes offline after a while – If you’re running it on your computer, it stops when your computer sleeps. To keep it 24/7, host it on a cloud service like Heroku, Railway, or a Raspberry Pi.
  • “Invalid Form Body” error when inviting – Your OAuth2 URL might be incomplete. Re-generate it and ensure both “bot” scope and permissions are selected.

Frequently Asked Questions

Do I need to know JavaScript to make a Discord bot?

Yes, basic JavaScript knowledge is required if you use the Discord.js library. However, you can also use Python with discord.py, which is similar in complexity.

Can I make a bot without coding?

There are no-code bot builders like MEE6 or Zapier, but they are limited. For a custom bot, coding is necessary.

How do I add more commands?

Add more `if` statements in the `messageCreate` event, or better, use a command handler that reads files from a commands folder.

What are slash commands and should I use them?

Slash commands are the modern way to interact with bots (they appear when you type `/`). They’re more discoverable and secure. Discord.js v14 supports them natively.

How do I host my bot for free?

You can use free tiers of cloud services like Railway, Replit, or Glitch, but they may have limitations. For a small bot, these are fine.

Why is my bot not responding to messages?

Check that the Message Content Intent is enabled, the bot has permissions, and that your code is listening to the `messageCreate` event.

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 →