๐ What You'll Learn
In this comprehensive guide, you'll learn how to create a fully functional Discord bot from scratch. We'll cover everything from setting up your development environment to deploying your bot with modern slash commands. By the end, you'll have a working bot and the knowledge to expand its capabilities.
๐ Prerequisites
Before we begin, make sure you have:
- A Discord account with access to a server where you have "Manage Server" permissions
- Node.js 18 or higher installed on your computer (download here)
- A code editor like VS Code, Sublime Text, or any IDE you prefer
- Basic knowledge of JavaScript (helpful but not required)
๐ฏ Step 1: Create Your Discord Application
Every Discord bot starts as an "Application" in Discord's Developer Portal. Think of this as your bot's identity card that Discord uses to recognize and authenticate your bot.
Here's how to create your application:
- Visit the Discord Developer Portal and log in with your Discord account
- Click the "New Application" button in the top-right corner
- Enter a name for your bot (you can change this later)
- Accept Discord's Terms of Service and click "Create"
๐ Congratulations! You've just created your Discord application. Now let's turn it into a bot.
๐ค Step 2: Generate Your Bot Account
Now we need to create the actual bot user that will appear in your Discord server.
Follow these steps:
- In your application's dashboard, click on "Bot" in the left sidebar
- Click "Add Bot" and confirm your decision
- Your bot is now created! You'll see its username and avatar
- Under the "Token" section, click "Reset Token" (you may need to enter your 2FA code)
- IMPORTANT: Copy this token immediately and store it safely - you won't be able to see it again!
โ ๏ธ Security Warning: Your bot token is like a password. Never share it publicly, commit it to Git, or post it online. If someone gets your token, they can control your bot!
๐ Step 3: Secure Your Bot Token
Proper token management is crucial for bot security. We'll use environment variables to keep your token safe.
Create a .env
file in your project directory:
DISCORD_TOKEN=your_bot_token_here
APPLICATION_ID=your_application_id_here
To get your Application ID, go back to the "General Information" tab in the Developer Portal and copy the "Application ID".
For additional security, add this to your .gitignore
file:
.env
node_modules/
*.log
๐ซ Step 4: Generate Bot Invite URL
Before we code our bot, let's create an invite URL so we can add it to our server later.
Generate your invite URL:
- In the Developer Portal, navigate to "OAuth2" โ "URL Generator"
- Under "Scopes", check
bot
andapplications.commands
- Under "Bot Permissions", select the permissions your bot needs:
- Send Messages - Allow your bot to send messages
- Use Slash Commands - Enable modern slash commands
- Read Message History - Let your bot see previous messages
- Add other permissions as needed for your bot's features
- Copy the generated URL at the bottom
๐ Save this URL - you'll use it to invite your bot to servers!
๐ป Step 5: Set Up Your Development Environment
Now let's create the code structure for your bot.
Create a new project directory and initialize it:
mkdir my-discord-bot
cd my-discord-bot
npm init -y
Install the required dependencies:
npm install discord.js@14
npm install dotenv
The discord.js
library is the most popular Discord bot framework for Node.js, and dotenv
will help us load environment variables from our .env
file.
๐ ๏ธ Step 6: Write Your First Bot
Now for the exciting part - let's create a functional Discord bot with modern slash commands!
Create index.js
with the following code:
// Load environment variables
require('dotenv').config();
const { Client, GatewayIntentBits, REST, Routes, SlashCommandBuilder, Collection } = require('discord.js');
// Create a new client instance with necessary intents
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
// Create a collection to store commands
client.commands = new Collection();
// Define slash commands
const commands = [
new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong! and shows bot latency'),
new SlashCommandBuilder()
.setName('hello')
.setDescription('Greets you with a friendly message'),
new SlashCommandBuilder()
.setName('server')
.setDescription('Provides information about the server'),
new SlashCommandBuilder()
.setName('user')
.setDescription('Provides information about a user')
.addUserOption(option =>
option.setName('target')
.setDescription('The user to get info about')
.setRequired(false))
];
// Function to register slash commands
async function registerCommands() {
try {
console.log('๐ Started refreshing application (/) commands.');
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);
await rest.put(
Routes.applicationCommands(process.env.APPLICATION_ID),
{ body: commands }
);
console.log('โ
Successfully reloaded application (/) commands.');
} catch (error) {
console.error('โ Error registering commands:', error);
}
}
// Event: Bot is ready
client.once('ready', async () => {
console.log(`๐ ${client.user.tag} is online and ready!`);
console.log(`๐ Serving ${client.guilds.cache.size} servers`);
// Register slash commands
await registerCommands();
// Set bot activity status
client.user.setActivity('slash commands', { type: 'LISTENING' });
});
// Event: Handle slash command interactions
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
const { commandName } = interaction;
try {
switch (commandName) {
case 'ping':
const sent = await interaction.reply({
content: '๐ Pinging...',
fetchReply: true
});
const ping = sent.createdTimestamp - interaction.createdTimestamp;
await interaction.editReply(
`๐ **Pong!**\n` +
`๐ก **Latency:** ${ping}ms\n` +
`๐ **API Heartbeat:** ${Math.round(client.ws.ping)}ms`
);
break;
case 'hello':
await interaction.reply(
`๐ Hello **${interaction.user.displayName}**!\n` +
`Welcome to **${interaction.guild.name}**! ๐`
);
break;
case 'server':
await interaction.reply(
`๐ **Server Information**\n` +
`**Name:** ${interaction.guild.name}\n` +
`**Members:** ${interaction.guild.memberCount}\n` +
`**Created:** `
);
break;
case 'user':
const targetUser = interaction.options.getUser('target') || interaction.user;
const member = interaction.guild.members.cache.get(targetUser.id);
await interaction.reply(
`๐ค **User Information**\n` +
`**Username:** ${targetUser.username}\n` +
`**Display Name:** ${targetUser.displayName || 'None'}\n` +
`**Joined Server:** \n` +
`**Account Created:** `
);
break;
default:
await interaction.reply('โ Unknown command!');
}
} catch (error) {
console.error('โ Error handling interaction:', error);
const errorMessage = 'โ There was an error executing this command!';
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: errorMessage, ephemeral: true });
} else {
await interaction.reply({ content: errorMessage, ephemeral: true });
}
}
});
// Event: Handle errors
client.on('error', error => {
console.error('โ Discord client error:', error);
});
// Login to Discord
client.login(process.env.DISCORD_TOKEN);
This enhanced bot includes:
- Multiple Commands: ping, hello, server, and user info
- Error Handling: Graceful error management and user feedback
- Rich Responses: Formatted messages with emojis and Discord timestamps
- User Options: Commands that accept user input
- Status Updates: Bot activity and comprehensive logging
๐ Step 7: Test Your Bot
Now let's bring your bot to life!
Make sure your .env
file is properly configured with your tokens, then run:
node index.js
If everything is set up correctly, you should see output like:
๐ YourBotName#1234 is online and ready!
๐ Serving 0 servers
๐ Started refreshing application (/) commands.
โ
Successfully reloaded application (/) commands.
Now invite your bot to your server:
- Use the invite URL you generated earlier
- Select your server from the dropdown
- Review the permissions and click "Authorize"
- Complete any CAPTCHA if prompted
๐ Your bot should now appear online in your server! Try typing /ping
to test it.
๐งช Step 8: Test Your Commands
Discord slash commands provide an intuitive way for users to interact with your bot. Here's what each command does:
/ping
- Shows bot latency and response time/hello
- Gives a friendly greeting with the server name/server
- Displays server information including member count/user [optional: @user]
- Shows user information and join dates
๐ก Pro Tip: Slash commands auto-complete and show descriptions as you type, making them very user-friendly!
๐ Best Practices & Security Tips
Follow these essential practices to create a robust and secure Discord bot:
- ๐ Token Security:
- Never hardcode tokens in your source code
- Use environment variables or secure config files
- Add
.env
to your.gitignore
file - Rotate tokens immediately if they're accidentally exposed
- โก Performance Optimization:
- Use async/await for non-blocking operations
- Implement proper error handling for all commands
- Cache frequently accessed data to reduce API calls
- Use connection pooling for database operations
- ๐ก๏ธ Permission Management:
- Request only the minimum permissions your bot needs
- Use server-specific permissions when possible
- Regularly audit your bot's permissions
- Document what each permission is used for
- ๐ Monitoring & Logging:
- Log important events and errors with timestamps
- Monitor bot uptime and response times
- Set up alerts for critical failures
- Keep logs for debugging and compliance
๐ Next Steps & Advanced Features
Congratulations! You now have a working Discord bot. Here are some ideas to expand its capabilities:
- ๐ Database Integration: Add SQLite or MongoDB to store user data, server settings, or command history
- ๐ต Music Bot: Integrate with YouTube or Spotify APIs for music playback
- ๐ค AI Integration: Add OpenAI GPT or other AI services for intelligent responses
- ๐ Moderation Tools: Implement auto-moderation, warning systems, and member management
- ๐ฎ Games & Fun: Create mini-games, polls, or interactive entertainment
- ๐ Notifications: Set up RSS feeds, social media monitoring, or server alerts
- ๐ Analytics: Track server activity, command usage, and user engagement
๐ Earn Your Developer Badge
Now that your bot is running with slash commands, you're eligible for Discord's Active Developer Badge! This badge appears on your Discord profile and shows that you're an active bot developer.
๐ Requirements:
- โ Have an active Discord application (you just created one!)
- โ Deploy at least one slash command (your bot has several!)
- โ Have your command used within the last 30 days
๐ Ready to claim your badge? Follow our detailed guide: How to Get the Discord Developer Badge
๐ค Need Help?
Building your first Discord bot can be challenging, but you're not alone! Here are some resources:
- ๐ง Issues with your bot? Check the console output for error messages
- ๐ Want to learn more? Visit the discord.js Guide
- ๐ฌ Join the community: Discord.js official server for help and discussions
- ๐ค Want a no-code solution? Try Friendify for AI-powered bots without programming
๐ Congratulations! You've successfully created your first Discord bot with modern slash commands. Your bot is now ready to serve your community with helpful commands and can be expanded with countless features. Happy coding!