MCPcopy
hub / github.com/mruniquehacker/Knightbot-MD / banCommand

Function banCommand

commands/ban.js:6–79  ·  view source on GitHub ↗
(sock, chatId, message)

Source from the content-addressed store, hash-verified

4const { isSudo } = require('../lib/index');
5
6async function banCommand(sock, chatId, message) {
7 // Restrict in groups to admins; in private to owner/sudo
8 const isGroup = chatId.endsWith('@g.us');
9 if (isGroup) {
10 const senderId = message.key.participant || message.key.remoteJid;
11 const { isSenderAdmin, isBotAdmin } = await isAdmin(sock, chatId, senderId);
12 if (!isBotAdmin) {
13 await sock.sendMessage(chatId, { text: 'Please make the bot an admin to use .ban', ...channelInfo }, { quoted: message });
14 return;
15 }
16 if (!isSenderAdmin && !message.key.fromMe) {
17 await sock.sendMessage(chatId, { text: 'Only group admins can use .ban', ...channelInfo }, { quoted: message });
18 return;
19 }
20 } else {
21 const senderId = message.key.participant || message.key.remoteJid;
22 const senderIsSudo = await isSudo(senderId);
23 if (!message.key.fromMe && !senderIsSudo) {
24 await sock.sendMessage(chatId, { text: 'Only owner/sudo can use .ban in private chat', ...channelInfo }, { quoted: message });
25 return;
26 }
27 }
28 let userToBan;
29
30 // Check for mentioned users
31 if (message.message?.extendedTextMessage?.contextInfo?.mentionedJid?.length > 0) {
32 userToBan = message.message.extendedTextMessage.contextInfo.mentionedJid[0];
33 }
34 // Check for replied message
35 else if (message.message?.extendedTextMessage?.contextInfo?.participant) {
36 userToBan = message.message.extendedTextMessage.contextInfo.participant;
37 }
38
39 if (!userToBan) {
40 await sock.sendMessage(chatId, {
41 text: 'Please mention the user or reply to their message to ban!',
42 ...channelInfo
43 });
44 return;
45 }
46
47 // Prevent banning the bot itself
48 try {
49 const botId = sock.user.id.split(':')[0] + '@s.whatsapp.net';
50 if (userToBan === botId || userToBan === botId.replace('@s.whatsapp.net', '@lid')) {
51 await sock.sendMessage(chatId, { text: 'You cannot ban the bot account.', ...channelInfo }, { quoted: message });
52 return;
53 }
54 } catch {}
55
56 try {
57 // Add user to banned list
58 const bannedUsers = JSON.parse(fs.readFileSync('./data/banned.json'));
59 if (!bannedUsers.includes(userToBan)) {
60 bannedUsers.push(userToBan);
61 fs.writeFileSync('./data/banned.json', JSON.stringify(bannedUsers, null, 2));
62
63 await sock.sendMessage(chatId, {

Callers 1

handleMessagesFunction · 0.85

Calls 2

isAdminFunction · 0.85
isSudoFunction · 0.85

Tested by

no test coverage detected