This is a simple Discord bot created using JavaScript and the Discord.js library.
Note
This Bot is for Example Purposes Only.
- Responds to the "!ping" command with "Pong!"
- Easily customizable and extendable
- Clone this repository to your local machine.
- Install Node.js if you haven't already: Node.js Download.
- Install the required dependencies using
npm install
. - Create a bot on the Discord Developers Portal and obtain a bot token.
- Add your bot token to the
config.json
file. - Run the bot using
node bot.js
.
Important
You Need Code Editor to Run the Code.
After successfully creating your bot, you will need to get the bot's token. This token is used to authenticate your bot. You can get your token with the following steps:
- Go to the "Bot" tab on the left.
- Click the "Copy" button in the "Token" section to copy the token to the clipboard.
Warning
Do Not Share Discord Bot Tokens with Others. Otherwise, your bot may be stolen.
First you need to create a bot on the Discord Developers Portal:
- Go to the Discord Developers Portal
- Create a new application and give it a name. This will be the name of your bot.
- Go to the "Bot" tab on the left and click the "Add Bot" button.
- You can customize your bot, but you can skip this step and use the default settings.
!ping
: Replies with "Pong!"- Add more custom commands in the
bot.js
file.
Full Code
const Discord = require('discord.js');
const client = new Discord.Client();
const token = 'YOUR_BOT_TOKEN'; // Add your bot's token here
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});
client.on('message', (message) => {
if (message.content === '!ping') {
message.reply('Pong!');
}
});
client.login(token);
This JavaScript code represents a basic Discord bot that responds to the !ping
command with Pong!
when invoked. Here's an explanation of the code:
const Discord = require('discord.js'); // Import the discord.js library.
const client = new Discord.Client(); // Create a new Discord client instance.
const token = 'YOUR_BOT_TOKEN'; // Replace 'YOUR_BOT_TOKEN' with your actual bot's token.
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});
- We start by importing the
discord.js
library, which allows us to interact with the Discord API. This library simplifies the process of creating Discord bots in JavaScript. - Next, we create a new instance of the Discord
client
and assign it to the client variable. Thisclient
object will be responsible for managing our bot's interactions with the Discord server. - We store the bot's
token
in the token variable. You should replaceYOUR_BOT_TOKEN
with the actual token you obtain when creating your Discord bot through the Discord Developer Portal. - The
client.on('ready', () => { ... })
block is an event listener. It listens for the 'ready' event, which occurs when the bot successfully logs in to the Discord server. When this event is triggered, the code inside the arrow function is executed, and it logs a message to the console, displaying the bot's username and tag.
client.on('message', (message) => {
if (message.content === '!ping') {
message.reply('Pong!');
}
});
- The
client.on('message', (message) => { ... })
block is another event listener. It listens for the 'message' event, which is triggered every time a message is sent in any channel that the bot has access to. - Inside the 'message' event handler, we check if the content of the message is equal to "!ping" using
if (message.content === '!ping') { ... }
. If the message content matches "!ping," the following code block is executed:message.reply('Pong!');
sends a reply to the message with the text "Pong!" This is how the bot responds to the "!ping" command with "Pong!" in the same channel where the command was issued.
client.login(token); // Log in to Discord using the bot's token.
- Finally, we log in to the Discord server using
client.login(token)
. This is where the bot's token is used for authentication, allowing the bot to connect to the server and respond to events.
gh repo clone musarda/Discord-JavaScript-Bot
This project is licensed under the MIT License.