Hack Forums
[Node.JS] [Discord.js Bot Guide] - Step-by-Step Tutorial w/ Moderation Commands - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [Node.JS] [Discord.js Bot Guide] - Step-by-Step Tutorial w/ Moderation Commands (/showthread.php?tid=6305039)



[Discord.js Bot Guide] - Step-by-Step Tutorial w/ Moderation Commands - Vectura - Aug 5, 2025

πŸ€– [Discord.js Bot Guide] - Step-by-Step Tutorial w/ Moderation Commands

Perfect for beginners. Works in private & public servers!



πŸŽ’ REQUIREMENTS
β€’ Node.js β†’ https://nodejs.org
β€’ Code Editor (e.g. Visual Studio Code β†’ https://code.visualstudio.com)
β€’ Discord account β†’ https://discord.com




πŸš€ STEP 1–4: CREATE BOT IN DISCORD DEVELOPER PORTAL
1. Go to: https://discord.com/developers/applications
2. Click "New Application" β†’ Give it a name β†’ Click "Create"
3. On the left: Go to "Bot" β†’ Click "Add Bot" β†’ Confirm
4. Under Privileged Gateway Intents: Enable:
- SERVER MEMBERS
- MESSAGE CONTENT




πŸ” STEP 5–6: SETUP TOKENS & PERMISSIONS
5. Copy your Bot Token (you’ll need it later). Also copy Client ID.
6. Generate bot invite link (replace YOUR_CLIENT_ID):
Bot Invite Link




πŸ’» STEP 7–10: LOCAL SETUP
7. Create folder "mybot" and open terminal inside it
8. Run:
npm init -y
9. Then run:
npm install discord.js@14 dotenv
10. Create files:
β€’ index.js
β€’ .env β†’ Add:
BOT_TOKEN=your_token_here




πŸ“œ STEP 11–13: BASIC BOT CODE
[index.js]
Code
require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMembers
  ]
});

client.once('ready', () => {
  console.log(`βœ… Logged in as ${client.user.tag}`);
});

client.login(process.env.BOT_TOKEN);





πŸ’¬ STEP 14–16: BASIC COMMANDS
Add inside your file:

Code
client.on('messageCreate', msg => {
  if (msg.author.bot) return;

  if (msg.content === '!ping') {
    msg.reply('πŸ“ Pong!');
  }

  if (msg.content === '!hello') {
    msg.reply(`Hello, ${msg.author.username}!`);
  }
});




πŸ›‘οΈ STEP 17–21: MODERATION COMMANDS
Add more commands below:

Code
// Kick
if (msg.content.startsWith('!kick')) {
  const member = msg.mentions.members.first();
  if (!member) return msg.reply('Mention someone!');
  if (!msg.member.permissions.has('KickMembers')) return msg.reply('You need Kick permissions.');
  member.kick().then(() => msg.reply(`${member.user.tag} has been kicked.`));
}

// Ban
if (msg.content.startsWith('!ban')) {
  const member = msg.mentions.members.first();
  if (!member) return msg.reply('Mention someone!');
  if (!msg.member.permissions.has('BanMembers')) return msg.reply('You need Ban permissions.');
  member.ban().then(() => msg.reply(`${member.user.tag} has been banned.`));
}

// Mute
if (msg.content.startsWith('!mute')) {
  const role = msg.guild.roles.cache.find(r => r.name === "Muted");
  const member = msg.mentions.members.first();
  if (!role) return msg.reply('Create a role named "Muted" first.');
  if (!member) return msg.reply('Mention someone!');
  member.roles.add(role);
  msg.reply(`${member.user.tag} has been muted.`);
}




🚦 STEP 22: START YOUR BOT LOCALLY
After you’ve written your code, it’s time to run your bot for the first time!

In your terminal, inside the β€œmybot” folder, run:

Code
node index.js

If everything is set up correctly, you’ll see this in your terminal:

Code
βœ… Logged in as YourBotName#1234

πŸ’‘ Keep your terminal open to keep your bot online.
To stop the bot, press CTRL + C.




πŸ”§ STEP 23–26: EXTRA TIPS
23. Create a β€œMuted” role in your server with no send permissions in channels
24. You can expand with commands like !unmute, !userinfo, !serverinfo, etc.
25. Use .env to hide secrets β€” NEVER commit it to GitHub!
26. Host your bot on Repl.it, Glitch, Heroku or use VPS for 24/7 uptime




βœ… YOUR BOT IS READY!
Join your server, type !ping or !kick @user and you’re good to go.
Feel free to share your bot invite with friends or keep it private.

In the next tutorial I'll go into more detail about slash commands, for example, or leave your suggestions in the comments, and who knows, I might work them out for you!



RE: [Discord.js Bot Guide] - Step-by-Step Tutorial w/ Moderation Commands - xclouded1 - Aug 5, 2025

Really HQ tutorial, I've never created a Discord server, but this looks good!


RE: [Discord.js Bot Guide] - Step-by-Step Tutorial w/ Moderation Commands - Vectura - Aug 5, 2025

(Aug 5, 2025 03:42 PM)xclouded1 Wrote:  Really HQ tutorial, I've never created a Discord server, but this looks good!

Thanks bud!


RE: [Discord.js Bot Guide] - Step-by-Step Tutorial w/ Moderation Commands - HerrWinston - Aug 5, 2025

Was a good read, i like .js aswell, i just cant learn it right now, but once i will try that out, this Thread will come in handy :)