What you need to know before you start

Creating a Discord bot means building a program that runs on Discord's servers and responds to messages or events in your server. You will need three things: a Discord server where you have permission to add bots, a Discord Developer Account (free), and code written in a programming language like Python or JavaScript. The bot itself lives on your computer or a hosting service and connects to Discord through an API token — a secret password that lets your code talk to Discord.

Most people use a library like discord.py (Python) or discord.js (JavaScript) because these libraries handle the complicated connection work for you. You do not need to be an expert programmer, but you do need to be comfortable reading and following code examples. If you have never written code before, this guide will show you the steps, but you may want to learn basic Python or JavaScript first.

Key Takeaways

  • You must create a bot application in the Discord Developer Portal and copy its token before you can write any code.
  • Most beginners use discord.py (Python) or discord.js (JavaScript) because these libraries simplify the connection to Discord's servers.
  • Your bot code runs on your computer or a hosting service and connects to Discord using the token you generated in the Developer Portal.
  • You add the bot to your server using a special invite link generated in the Developer Portal, not by searching for it in Discord's bot store.
  • Keep your bot token secret — anyone who has it can control your bot and access your server.

Create your bot in the Discord Developer Portal

Go to discord.com/developers/applications and sign in with your Discord account. If you do not have one, create it first — it is free. Click the blue "New Application" button in the top right. Give your bot a name (you can change this later) and click "Create".

On the left side, click "Bot" and then click "Add Bot". Discord will create a bot user linked to your application. Under the bot's username, you will see a "TOKEN" section with a blue "Copy" button. Click it to copy your bot token to your clipboard. This token is like a password — do not share it, paste it in Discord, or commit it to a public GitHub repository. If you accidentally expose it, come back here and click "Regenerate" to create a new one.

Scroll down to "Intents" and turn on "Message Content Intent". This lets your bot read the text inside messages. Without it, your bot will see that a message arrived but not what the message says. Leave the other intents off for now — you can turn them on later if your bot needs them.

Generate an invite link and add the bot to your server

Still in the Developer Portal, click "OAuth2" on the left, then "URL Generator". Under "Scopes", check the box for "bot". Under "Permissions", check "Send Messages", "Read Messages/View Channels", and "Read Message History". Scroll down and copy the generated URL.

Paste that URL into your browser. Discord will ask you which server to add the bot to — pick the server where you want to test it. You must have permission to manage that server. The bot will appear in your server's member list, but it will be offline until you run your code.

Write your first bot in Python

Python is the easiest language to start with. First, install Python from python.org if you do not have it. Open a terminal or command prompt and run this command to install the discord.py library:

pip install discord.py

Create a new file called bot.py and paste this code into it:

import discord from discord.ext import commands bot = commands.Bot(command_prefix='!', intents=discord.Intents.default()) @bot.event async def on_ready(): print(f'{bot.user} has connected to Discord!') @bot.command(name='hello') async def hello(ctx): await ctx.send(f'Hello {ctx.author.name}!') bot.run('YOUR_TOKEN_HERE')

Replace 'YOUR_TOKEN_HERE' with the token you copied from the Developer Portal. Save the file. Open a terminal in the same folder and run python bot.py. If it works, you will see a message saying your bot has connected. Go to your Discord server and type !hello — your bot should reply with "Hello [your name]!".

This bot does one thing: it listens for the command !hello and replies with a greeting. The @bot.command decorator tells discord.py that the function below it is a command. The async def syntax means the function can wait for Discord to respond without freezing. You can add more commands by copying the same pattern and changing the function name and what it does.

Write your first bot in JavaScript

If you prefer JavaScript, you will use Node.js and the discord.js library. Download Node.js from nodejs.org. Create a new folder for your bot, open a terminal in that folder, and run:

npm init -y

Then install discord.js:

npm install discord.js

Create a file called bot.js and paste this code:

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.author.bot) return; if (message.content === '!hello') { message.reply(`Hello ${message.author.username}!`); } }); client.login('YOUR_TOKEN_HERE');

Replace 'YOUR_TOKEN_HERE' with your bot token. Save the file and run node bot.js in your terminal. Your bot should connect. Type !hello in your Discord server and it will reply. This code listens for any message and checks if it says !hello. If it does, the bot replies to the message.

Keep your bot running and troubleshoot common problems

The code above only runs while your terminal is open. If you close it, your bot goes offline. For testing, that is fine. For a bot that stays online all the time, you have two options: run it on a computer that stays on 24/7, or use a hosting service like Replit, Railway, or Heroku. These services run your code on their servers for free or a small fee.

If your bot does not respond to commands, check these things first. Make sure the bot is online in your server's member list. Make sure you copied your token correctly — even one wrong character will break it. Make sure you turned on "Message Content Intent" in the Developer Portal. Make sure the command prefix in your code matches what you are typing in Discord (the examples above use !). If you still have trouble, check the terminal where your bot is running — error messages there usually tell you what went wrong.

If your bot is online but does not have permission to send messages, go back to the OAuth2 URL Generator in the Developer Portal and make sure you checked "Send Messages" in the Permissions section. If you already added the bot before checking that box, remove it from your server and add it again using the updated invite link.

What to do next

Once your first command works, you can add more commands by following the same pattern. Both discord.py and discord.js have full documentation with examples for things like reactions, embeds (fancy formatted messages), buttons, and slash commands (the modern way to write commands in Discord). You can also make your bot respond to events other than commands — for example, when someone joins the server or reacts to a message.

If you want your bot to remember information between restarts, you will need a database. SQLite is simple and free. If you want your bot to do something on a schedule, both libraries have built-in timers. Start small, test each feature in your own server before inviting others to use it, and read the error messages in your terminal — they usually point you to exactly what is wrong.

Frequently Asked Questions

Can I use a bot token in my GitHub repository?

No. Anyone who finds your token can control your bot. If you push your code to GitHub, use environment variables to store your token instead. Create a file called .env, put TOKEN=your_token_here in it, add .env to your .gitignore file, and load it in your code using a library like python-dotenv or dotenv.

Why does my bot not see message content?

You did not turn on "Message Content Intent" in the Developer Portal. Go back to your bot's settings, scroll to Intents, and toggle it on. Your code also needs to request this intent — the examples above include it, but if you copied code from somewhere else, check that it is there.

How do I make my bot respond to slash commands instead of text commands?

Slash commands are the newer Discord standard. Both discord.py and discord.js support them, but the syntax is different from text commands. Search for "slash commands" in the documentation for whichever library you are using — they have full examples and it is worth learning because Discord is moving toward slash commands.

Can I run my bot on my phone or tablet?

Not easily. Bots need to run Python or Node.js, which are not designed for phones. Use a computer or a hosting service instead. Replit and Railway both work on any device with a web browser and will run your bot for free.

What is the difference between a bot and a webhook?

A bot is a full Discord user that can read messages, react to events, and do complex things. A webhook is simpler — it only sends messages to a channel, usually from an outside service like a website or monitoring tool. Use a bot if you want your program to listen and respond. Use a webhook if you only need to send notifications.