> ## Knowledge Base Index
> Fetch the complete knowledge base index at: https://help.squarecloud.app/sitemap.xml
> Use this file to discover available pages before exploring further.
> Pure-Markdown content can be obtained by appending a '.md' suffix to the content URLs listed in the sitemap (without the trailing slash).

# How to create a Discord bot from scratch

## What a bot is and how it works

A Discord bot is an application that connects to the Discord API and acts as an automated user: it responds to commands, moderates the server, plays music, sends alerts, and much more. To create one, you need two things: register the application in the **Discord Developer Portal** and write the code that brings it to life.

## 1. Create the application in the Discord Developer Portal

1. Go to the Discord Developer Portal and log in.
2. Click **New Application**, give it a name, and confirm.
3. In the side menu, go to **Bot**.
4. Click **Reset Token** to generate your bot's token.

## 2. Get and protect your token

The **token** is your bot's password — whoever has the token controls the bot. So:

* Never share it or push the token to GitHub.
* Store it in an environment variable (e.g., `DISCORD_TOKEN`), never directly in the code.
* If it leaks, generate a new one immediately under **Reset Token**.

## 3. Configure the Intents

Still under the **Bot** tab, enable the **Privileged Gateway Intents** your bot needs — especially the **Message Content Intent** if it will read messages. Without it, the bot won't receive message content (see [Why isn't my bot reading messages? Understanding Privileged Intents](https://help.squarecloud.app/en-us/article/why-isnt-my-bot-reading-messages-understanding-privileged-intents-1ilx31c/)).

## 4. Invite the bot to your server

1. Go to **OAuth2 > URL Generator**.
2. Under **Scopes**, check `bot` (and `applications.commands` if you'll use slash commands).
3. Under **Bot Permissions**, check the permissions you need.
4. Copy the generated URL, open it in your browser, and choose the server.

## 5. Write your first "Hello World"

Pick a library (discord.js for JavaScript or discord.py for Python) and make the bot respond to a simple command. Not sure which to use? See [discord.js or discord.py: which one to choose to get started](https://help.squarecloud.app/en-us/article/discordjs-or-discordpy-which-one-to-choose-to-get-started-24x7oi/).

Minimal example in discord.js:
```
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.once('ready', () => console.log('Bot online!'));
client.login(process.env.DISCORD_TOKEN);
```

## Next step: keep the bot online 24/7

On your computer, the bot is only online while the code is running. For it to work all the time, you need to host it. See the guide [How to host a Discord bot](https://help.squarecloud.app/en-us/article/how-to-host-a-discord-bot-e2udcn/) and put your bot online 24/7 on Square Cloud.