Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Node.JS 

[Discord.js Bot Guide] - Step-by-Step Tutorial w/ Moderation Commands

🤖 [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!
Really HQ tutorial, I've never created a Discord server, but this looks good!
(Aug 5, 2025 03:42 PM)xclouded1 Wrote:  Really HQ tutorial, I've never created a Discord server, but this looks good!

Thanks bud!
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 :)
The risk of not taking risk is riskier than comfort.

Never Deal Off-Site!


Possibly Related Threads...
Thread / Author Replies Views Last Post
by HerrWinstonAug 19, 2025 08:22 AM 3 180 Aug 19, 2025 08:22 AM
Last Post: Bshol
[Javascript]  by NOTHING MATTERSOct 3, 2017 10:49 AM 4 526 Oct 3, 2017 10:49 AM
Last Post: /wrists
[PHP] by VespeiApr 19, 2017 10:41 AM 0 305 Apr 19, 2017 10:41 AM
Last Post: Vespei