To "code in Discord," you either format code in messages or build a bot that runs your code. Code blocks use triple backticks around your text and render in a monospace box with syntax colors; a bot needs an application on Discord's Developer Portal plus a script running on a host.

Formatting covers most casual needs: sharing a snippet, comparing versions of a function, or pasting an error for help. Building a bot fits when you want the server to react on its own — respond to commands, post on a schedule, or connect to an outside service. The steps below cover both, starting with formatting because it takes seconds and needs no tools.

How do you format code in a Discord message?

Discord treats text between backticks as code. One pair makes inline code; three pairs on their own lines make a block that keeps indentation and line breaks. The block is the important one for real code, because indentation is meaningful in Python and helpful everywhere else.

  1. Open the message box in any channel or direct message where you have permission to send messages.
  2. Type three backticks on their own line. On most keyboards the backtick sits to the left of the 1 key; on a phone, switch to the symbols page of the keyboard.
  3. Press Enter so the cursor moves to the next line. The input box shows a small hint that a code block is open.
  4. Paste or type the code exactly as it should run, keeping line breaks and indentation.
  5. Press Enter, type three more backticks on a new line, and send. The block closes and the message displays in a monospace box.
  6. For a single value such as a function name — print() — wrap it in one backtick on each side instead of three.

To add highlighting, put a language name directly after the opening three backticks with no space: python, javascript, html, css, json, bash, sql, and java all work. Highlighting only changes color, never how the code copies, so the wrong label is cosmetic. Long code can exceed the message character limit; split it into two blocks rather than letting it truncate, since a cut-off block loses its closing backticks.

If the block comes out as plain text, check that each set of three backticks is alone on its line with no trailing spaces and that you did not mix a language label with a space before the code. On mobile, autocorrect sometimes replaces the backtick with an apostrophe; switch to the plain symbol keyboard before typing.

What do you need before building a Discord bot?

A bot is a small program that stays connected to Discord and reacts to events. You need four things, and the order matters because each depends on the previous one.

  1. An application. Sign in at the Discord Developer Portal, choose New Application, and give it a name. The application is the container; the bot is a user inside it.
  2. A bot token. In the Bot tab, add a bot, then copy the token. Treat it like a password: anyone with it controls your bot. Store it in a file excluded from any public repository, never pasted into a channel.
  3. Message intent permissions. In the Bot tab, enable the privileged intents your code depends on; reading ordinary message content requires the message content intent, which must be switched on for the bot to see text commands.
  4. An invite link. In OAuth2 > URL Generator, tick bot under scopes and the permissions the bot needs — Send Messages and Read Message History cover a basic responder — then open the generated link and choose your server.

A hosting environment is the fourth item in practice: the script has to run somewhere that stays online. Your own computer works while it is on and awake; a small server or a hosting service keeps it running after you close the lid. Menu and tab names on the portal can differ slightly as the site is updated, but the sequence of application, bot, token, intents and invite has stayed the same for years.

How do you write a bot that responds to a command?

A minimal bot listens for messages, checks whether one starts with your chosen prefix, and replies. With a JavaScript library the flow looks like the steps below; a Python library uses the same logic under different names.

  1. Create a folder and initialize a project, then install the language's official Discord library with its package manager.
  2. Paste the token into an environment variable or a local config file, not into the script itself.
  3. Create the client and log in with the token.
  4. Listen for the ready event and print a short line so you know the connection worked.
  5. Listen for message events. Ignore any message with no prefix, and compare the text after the prefix to a command name such as ping.
  6. Reply in the same channel with a short fixed string. Stop here and test before adding anything else.
  7. Start the script and watch the console for the first run — every error appears here, not in Discord.

Once the basic reply works, grow one feature at a time: an embed with a colored side bar, a command with one argument such as !roll 20, then a role check so only moderators can run a command. Testing after each addition keeps the cause of a failure obvious.

Prefer slash commands for anything shared with other people. They appear in a menu when a user types a forward slash, so nobody has to memorize a prefix, and Discord validates argument types for you. Register the command definitions once at startup, then handle the interaction event instead of parsing message text.

Which setup fits your case?

  • Sharing a snippet with a friend or helper: use a fenced code block with a language label. Nothing to install.
  • Explaining one term in a sentence: use single-backtick inline code.
  • Teaching a group where code runs on the server: a bot with slash commands, hosted off your machine.
  • Testing bot logic: run the script locally and use a private test server with a separate token.
  • Connecting Discord to an outside service: a bot with an event listener that calls the service and posts the result back.

Settings that matter most, with the usual values for a first project:

SettingTypical choiceWhy it matters
Prefix styleText prefix or slash commandSlash avoids collisions with normal chat
Message content intentOn only if reading text commandsOff limits what the bot can see
Bot permissionsSend Messages, Read Message HistoryLeast privilege keeps mistakes contained
Token storageEnvironment variable or excluded fileLeaked tokens get abused fast
HostLocal machine first, then a serverLocal is free but sleeps when offline

You know the code formatting worked when the message shows a gray monospace box and the copy button appears on the block. You know the bot works when the console prints its ready line and a test command gets a reply within a couple of seconds. A reply that arrives only sometimes usually means the host was asleep or the process restarted.

What breaks a Discord bot or code block?

A block that renders as text. The opening and closing backticks must each sit on their own line. A single stray space after the opening trio cancels the format; delete it and resend.

Indentation lost in a translation. Pasting tabs into a block sometimes collapses spacing. Check the sent message, not just the input box, before anyone copies from it.

A bot that stays offline. If the console shows an authentication error, the token is wrong or a new one was generated in the portal, which invalidates the old value. Copy the current token and restart.

A bot online but silent. The most common cause is the message content intent left off, so the script never receives the text. The second is a permission gap in the channel itself, separate from the invite permissions.

Duplicate replies. Two copies of the script running at once both answer. Stop every instance before starting a new one; a restarting host that leaves the old process alive grows the queue.

A leaked token. If the token ever appears in chat or a public repository, regenerate it in the portal immediately. Regenerating revokes the old value and protects the bot from outside control.

Commands that worked yesterday. If the library was updated to a new major version, method names can change. Read the library's upgrade notes rather than guessing at the new signature, and pin the dependency to the version you tested.

Keep a copy of the working script before experiments; a bot that ran cleanly is the fastest way to find what an edit broke.