Documentation

Generating Discord bots with discord.py and discord.js

How Codexe builds Discord bots: slash commands, event listeners, gateway intents, and choosing between the Python and Node.js libraries.

Python or Node.js

Codexe generates Discord bots in two stacks: discord.py (Python) and discord.js (Node.js). Both cover the same Discord features - the choice is about which language you would rather maintain.

  • discord.py - concise, decorator-driven, quick to read. A good default if you are not already a JavaScript developer.
  • discord.js - the largest bot ecosystem, and the obvious pick if the rest of your stack is Node.

What gets generated

A generated bot is a runnable project with the gateway connection, command registration and event handling already wired up:

my-bot/
├── bot.py                  # or index.js
├── requirements.txt        # or package.json
├── .env.example            # DISCORD_TOKEN placeholder
└── cogs/ (or commands/)
    ├── moderation.py
    └── utility.py

The bot token is never written into the source. It is read from an environment variable, and the generated .env.example shows which variables to set.

Treat your bot token like a password. Anyone holding it controls the bot. If you paste it anywhere public, regenerate it in the Discord developer portal immediately.

Slash commands

Modern Discord bots use application (slash) commands rather than reading message text. They are registered with Discord, appear in the client’s command picker with typed arguments, and do not need the privileged message content intent.

# discord.py
@bot.tree.command(name="ping", description="Check latency")
async def ping(interaction: discord.Interaction):
    await interaction.response.send_message(
        f"Pong! {round(bot.latency * 1000)}ms"
    )

Two things regularly trip people up. First, commands must be synced with Discord before they appear - globally this can take up to an hour, while syncing to a single guild is effectively instant, which is why generated bots usually sync per-guild during development. Second, an interaction must be answered within three seconds; anything slower has to be deferred first.

Gateway intents

Intents tell Discord which events to send your bot. Requesting fewer is both faster and required - some are privileged and must be enabled explicitly in the developer portal.

  • Message Content - privileged. Needed only if you read message text; slash commands do not require it.
  • Server Members - privileged. Needed for join/leave events and member lookups.
  • Presence - privileged, and rarely necessary.

If a bot connects but never reacts to anything, an unticked privileged intent in the developer portal is the first thing to check.

Running your bot

Create an application in the Discord developer portal, add a bot user, copy the token into your environment, and invite it to your server with the scopes the generated README lists. Then run it like any normal Python or Node project.

Start building

Use the Discord bot generator and say which library you want, what commands it needs, and what it should react to.

Discord bots | Codexe