> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Discord Bot Errors: Invalid Token, Intents, Lavalink

> Fix Discord bot errors on Square Cloud: LoginFailure and TokenInvalid, disallowed intents, Message Content Intent, Lavalink code 1006 and bots going offline.

Discord bot errors on Square Cloud usually trace back to the token, the gateway intents, or a version mismatch with Lavalink. Match the exact message below.

## "LoginFailure: Improper token has been passed" / TokenInvalid

**What it means:** Discord rejected the token your bot logs in with. The exact message depends on the library:

* **discord.py:** `discord.errors.LoginFailure: Improper token has been passed.`
* **discord.js v14:** `Error [TokenInvalid]: An invalid token was provided.`
* **older discord.js:** `Error [TOKEN_INVALID]: An invalid token was provided.`
* **other libraries:** an HTTP `401 Unauthorized` on login.

**Why it happens:**

* The token was regenerated or revoked in the Discord Developer Portal. Generating a new token instantly invalidates the old one everywhere it's used.
* The token string has extra spaces or quotes copied in by accident.
* The code is reading the wrong environment variable, or the variable isn't set on Square Cloud.

**How to fix:**

1. Go to the [Developer Portal](https://discord.com/developers/applications) → your application → **Bot** → **Reset Token**.
2. Update the token in the application's [environment variables](/en/platform/environment-variables) (dashboard **Settings** → **Environment Variables**, or `squarecloud app env set`). Never commit the token in a file that gets uploaded in the zip.
3. Double-check there are no stray spaces or quotes around the value.
4. Update the library (`discord.js@latest` or `pip install -U discord.py`).
5. Restart the application, so it loads the new value.

<Warning>Never hardcode your bot token in source files. Always read it from an environment variable set in the dashboard.</Warning>

## "Used disallowed intents" / Message Content Intent

**What it means:** the bot logs in and appears online, but ignores every message, or the gateway rejects the connection with "used disallowed intents".

**Why it happens:** since 2022, **Message Content** is a privileged intent. Without it explicitly enabled in both places, message content arrives empty (or the gateway connection is refused if your code declares an intent that isn't enabled for the app).

**How to fix, in both places:**

1. Discord Developer Portal → your application → **Bot** → **Privileged Gateway Intents** → enable **Message Content Intent**.
2. Declare it in code as well:

```javascript theme={"system"}
// discord.js
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
  ],
});
```

```python theme={"system"}
# discord.py
intents = discord.Intents.default()
intents.message_content = True
client = commands.Bot(command_prefix="!", intents=intents)
```

3. Restart the application.

<Note>Once a bot passes 100 servers, privileged intents (including Message Content) also require approval from Discord, requested from the same Developer Portal page.</Note>

## Lavalink connection closed with code 1006

**What it means:** the bot's Lavalink client closes the WebSocket abnormally with code 1006, often preceded by "Unexpected server response: 400".

**Why it happens:** this is a version mismatch. Lavalink v4 is REST-based and is incompatible with a client wrapper built for v3, so the handshake fails and the socket closes with 1006.

**How to fix:**

1. Align your bot's Lavalink client wrapper version with the Lavalink server's major version (v3 client with v3 server, v4 client with v4 server).
2. On Square Cloud, the Lavalink server itself binds port `80`, but your bot must connect to it through the edge on port `443` with `secure: true`.
3. Restart both the Lavalink application and the bot application after fixing the version mismatch.

For the full Lavalink hosting setup (config file, port, deployment), see the [Lavalink server tutorial](/en/tutorials/how-to-create-your-lavalink-server).

## Bot goes offline after working locally

**What it means:** the bot logs in fine when you run it on your machine, but goes offline (or restarts repeatedly) once deployed.

**Why it happens, most commonly:**

* An unstable connection at the gateway level with no reconnect handling.
* Conflicting or incompatible dependency versions between what you tested locally and what's in your dependencies file.
* The bot was stopped for going over its RAM or CPU, or for flooding the Discord API. In that case the logs show a `[SQUARE-SHIELD]` line and you get an email: see the [status table](/en/platform/limitations-and-restrictions#limit-exceeded-statuses).

**How to fix:**

1. Check the application's logs in the dashboard first, they show the actual crash reason.
2. Add error handlers so a transient error doesn't crash the process: `process.on("unhandledRejection", ...)` and `client.on("error", ...)` in discord.js, equivalent handling in discord.py.
3. Add `AUTORESTART=true` to your `squarecloud.app` so Square Cloud restarts the bot after a crash. It's off by default, and it only restarts in the cases listed in [`AUTORESTART`](/en/getting-started/config-file#autorestart). It keeps the bot alive through transient errors, but it never fixes an invalid token, a syntax error or a missing dependency: those still need a fix in the code.
4. Watch for Discord API rate limits (429): the global limit is about 50 requests/second per bot token, with tighter per-route buckets (channel create/edit allows roughly 2 changes per 10 minutes per channel). Cache aggressively, use an async queue that respects the `X-RateLimit-Remaining`/`X-RateLimit-Reset-After` headers, and use webhooks for mass sends. A bot that keeps flooding Discord is stopped with [`ABUSE_REQUESTS`](/en/platform/limitations-and-restrictions#container_temporarily_suspended).

## Related guides

* [Discord bot hosting tutorial](/en/tutorials/bots/discord): set up and deploy a bot from scratch.
* [Environment variables](/en/platform/environment-variables): store the token outside your code.
* [App won't start](/en/platform/troubleshooting/app-wont-start): upload errors, missing modules and other boot failures.

<Tip>If the logs don't point to a clear cause, our support team can help you dig further.</Tip>

## Contact us

If you continue facing **technical difficulties**, our **specialized support team** is available to assist you. [**Contact us**](https://squarecloud.app/en/support) and we'll be happy to help you resolve any issue: support quality is a big part of why developers rate Square Cloud [**4.9/5 across 402 reviews**](https://www.trustpilot.com/review/squarecloud.app) on Google and Trustpilot.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.