MOCKA CODES Software Tools Create your first Discord Bot

ARTICLE ID: COD-GUI-CRE

Create your first Discord Bot

A starter guide for creating a Discord bot and introducing basic bot concepts for communities and gaming servers.

05 Jun 2025 3 MIN READ -- VIEWS MOCKA CODES
Create your first Discord Bot hero visual

Discord is a popular platform for gamers and communities to communicate, share content and play games. Discord bots are automated programs that can be used to perform various tasks on a Discord server, such as moderating chats, playing music, and responding to user commands. In this article, we’ll show you how to create a basic Discord botGlossaryDiscord BotAn automated Discord account controlled by code to respond to commands, manage communities, or integrate external services. using Node.js and the Discord.js library.

Prerequisites

Before we start, you’ll need to have the following:

  • Node.js and NPM installed on your machine.
  • A Discord account.
  • A Discord server where you have administrator privileges.
  • A Discord botGlossaryDiscord BotAn automated Discord account controlled by code to respond to commands, manage communities, or integrate external services. token. To get a bot token, follow these steps:
  1. Go to the Discord Developer Portal.
  2. Click on “New Application” and give it a name.
  3. Navigate to the “Bot” section and click “Add Bot”.
  4. Click on “Copy” to copy the bot token to your clipboard.

Setting up the project

First, create a new directory for your project and navigate to it in your terminal. Then, initialize a new Node.js project using NPM:

text
npm init -y

Next, install the Discord.js library:

text
npm install discord.js

Creating the bot

Create a new file named bot.js in your project directory. This will be the entry point for your bot. Paste the following code into the file:

{ console.log(Logged in as ${client.user.tag}!); }); client.on(‘message’, msg => { if (msg.content === ‘ping’) { msg.reply(‘Pong!’); } }); client.login(‘your-token-goes-here’);”>

text
const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('Pong!');
  }
});

client.login('your-token-goes-here');

Let’s go over this code. First, we import the discord.js library and create a new Client instance. The Client class represents our Discord botGlossaryDiscord BotAn automated Discord account controlled by code to respond to commands, manage communities, or integrate external services..

Next, we add two event listeners using the client.on() method:

  • The 'ready' event is emitted when the bot has connected to Discord and is ready to receive messages. We log a message to the console to confirm that the bot has successfully logged in.
  • The 'message' event is emitted whenever a message is sent in a channel that the bot can see. We check if the message content is "ping", and if it is, we reply with "Pong!". Finally, we call the client.login() method and pass in our bot token.

Running the bot

To run the bot, open a terminal window in your project directory and type:

text
node bot.js

You should see a message in the console indicating that the bot has logged in.

Adding the bot to your Discord server

To add your bot to your Discord server, you’ll need to generate an invite link. Follow these steps:

  1. Go to the Discord Developer Portal.
  2. Click on your bot application.
  3. Navigate to the “OAuth2” section.
  4. Under “Scopes”, select “bot”.
  5. Under “Bot Permissions”, select the permissions you want your bot to have.
  6. Copy the generated invite link and paste it into your browser. You’ll be prompted to select a server where you want to add the bot. Make sure you have administrator privileges on that server, and then click “Authorize”. The bot should now appear in your Discord server’s member list.

Conclusion

In this article, we showed you how to create a basic Discord botGlossaryDiscord BotAn automated Discord account controlled by code to respond to commands, manage communities, or integrate external services. using Node.js and the Discord.js library. We covered how to set up