Skip to content

Instantly share code, notes, and snippets.

@guarzo
Last active March 15, 2025 21:04
Show Gist options
  • Save guarzo/a4d238b932b6a168ad1c5f0375c4a561 to your computer and use it in GitHub Desktop.
Save guarzo/a4d238b932b6a168ad1c5f0375c4a561 to your computer and use it in GitHub Desktop.
How to create a discord bot

Setting Up a Discord Bot

Table of Contents

Introduction

Discord bots are applications that run inside Discord servers and provide automated functions for users. From moderating chats to playing music or responding to commands, bots can greatly enhance a Discord community. This guide will walk you through creating a simple Discord bot from scratch, including all the setup steps and a basic example. By the end, you'll have a bot that you can invite to your server and run on your own computer.

Prerequisites

  • A Discord account and your own Discord server (where you have permission to add bots).
  • A PC with internet access and the ability to run a terminal/command prompt.
  • Basic familiarity with using a command line and editing text files.
  • Node.js (JavaScript runtime) installed on your system. (We'll use Node.js + JavaScript in this guide.)
    Alternatively, you can use Python – see notes in Step 5 if you prefer Python.

Step 1: Create a Discord Application

To create a Discord bot, you first need to set up a Discord Application in the Discord Developer Portal. This application will hold your bot and allow you to generate a token for it.

  1. Log in to the Discord Developer Portal: Go to the Discord Developer Portal and sign in with your Discord account​:contentReference[oaicite:0]{index=0}.
  2. Create a New Application: Click the "New Application" button. In the popup, enter a name for your bot (this can be anything, e.g. "MyFirstBot") and then click Create​:contentReference[oaicite:1]{index=1}.
    • After creation, you will see your new application’s details page. You can set an avatar and description for your application here (optional).

Step 2: Add a Bot to Your Application

Now that you have a Discord application, you need to add a Bot user to it.

  1. On the application’s page, click on the Bot tab in the left sidebar​:contentReference[oaicite:2]{index=2}.
  2. Click the "Add Bot" button, then confirm by clicking "Yes, do it!" when prompted​:contentReference[oaicite:3]{index=3}. This will create a bot user for your application.
  3. (Optional) Customize your bot: On the Bot settings page, you can give your bot a username and avatar. You can also toggle whether the bot is public (allow anyone to invite it) or private. For most cases, leave Public Bot enabled and Require OAuth2 Code Grant disabled​:contentReference[oaicite:4]{index=4}.

Once added, your bot will be assigned a token (password) and a Client ID. We'll use the token in the next steps.

Step 3: Get Your Bot Token

The bot token is essentially your bot’s login password for Discord’s servers. You will need this token to run your bot code.

  1. In the Bot tab (where you added the bot), locate the Token section. Click the Copy button to copy your bot's token to your clipboard​:contentReference[oaicite:5]{index=5}.
  2. Save the token in a safe place for now. You'll need to paste it into your code in a later step.

Warning: Treat your bot token like a password – keep it private. If someone else gets your token, they can use your bot to do malicious things (like spamming or banning users) on any server where the bot is present​:contentReference[oaicite:6]{index=6}. If you accidentally expose the token, regenerate it immediately in the Developer Portal​:contentReference[oaicite:7]{index=7}.

Step 4: Invite Your Bot to a Discord Server

Your bot needs to be invited to a server in order to interact with it. To do this, you'll create an invite URL with the proper permissions and scopes.

  1. In the Developer Portal, go to the OAuth2 section (in the left sidebar) and select "URL Generator".
  2. Under Scopes, check the box for "bot"​:contentReference[oaicite:8]{index=8}. This indicates you want a URL to add the bot to a server.
  3. Under Bot Permissions, select the permissions your bot will need on the server. For a simple bot (like one that replies to messages), you might select permissions such as Send Messages, View Channels, and Read Message History. If unsure, you can select Administrator for now, but be aware this grants full access​:contentReference[oaicite:9]{index=9}.
  4. Scroll down and you will see a generated Invite URL. Copy this URL.
  5. Open the invite URL in a new browser tab. Select the server where you want to add the bot, then click Authorize. (Note: You must have Manage Server permission in that server to be able to invite the bot​:contentReference[oaicite:10]{index=10}.)
  6. Complete the CAPTCHA (if prompted). Your bot should now be added to the server, appearing in the member list. (It will be offline until you run the bot code in Step 6.)

Note: You can re-use the invite URL to add your bot to other servers (as long as you have the required permissions on those servers). Make sure to only grant the minimum necessary permissions for security.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment