discord bot using javascript made by deadcode Discord Bot with JavaScript
This README explains how to create a simple Discord bot using JavaScript and the discord.js library.
๐ฆ Requirements
Node.js (v18 or higher recommended)
npm (comes with Node.js)
A Discord account
Access to the Discord Developer Portal
โ๏ธ Step 1: Create a New Application
Go to the Discord Developer Portal.
Click New Application and give it a name.
Navigate to the Bot tab and click Add Bot.
Copy the Bot Token (youโll need this later).
โ๏ธ Step 2: Set Up Your Project
Create a new folder for your bot project.
Open a terminal in that folder.
Initialize npm:
npm init -y
Install required libraries:
npm install discord.js dotenv
โ๏ธ Step 3: Create Your Bot File
Create a file named index.js (or main.js) and add the following code:
const { Client, GatewayIntentBits } = require('discord.js'); require('dotenv').config();
const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent ] });
client.once('clientReady', () => {
console.log(โ
Logged in as ${client.user.tag});
});
const prefix = "/";
client.on('messageCreate', message => { if (message.author.bot) return;
if (message.content === ${prefix}hello) {
message.reply('๐ Hello there!');
}
if (message.content === ${prefix}ping) {
message.reply('๐ Pong!');
}
if (message.content === ${prefix}roll) {
const number = Math.floor(Math.random() * 100) + 1;
message.reply(๐ฒ You rolled a ${number}!);
}
});
client.login(process.env.BOT_TOKEN);
โ๏ธ Step 4: Add Environment Variables
Create a .env file in the root of your project:
BOT_TOKEN=YOUR_DISCORD_BOT_TOKEN
Replace YOUR_DISCORD_BOT_TOKEN with the token you copied from the Developer Portal.
โ๏ธ Step 5: Invite Your Bot to a Server
In the Developer Portal, go to OAuth2 โ URL Generator.
Select bot and applications.commands scopes.
Choose permissions (e.g., Send Messages).
Copy the generated URL and open it in your browser.
Invite the bot to your server.
๐ Step 6: Run Your Bot
In your terminal:
node index.js
You should see:
โ Logged in as YourBotName#1234
๐ Libraries Used
discord.js โ Core library to interact with Discord API.
dotenv โ Loads environment variables from .env file for secure token management.