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

Function kickCommand

commands/kick.js:3–128  ·  view source on GitHub ↗
(sock, chatId, senderId, mentionedJids, message)

Source from the content-addressed store, hash-verified

1const isAdmin = require('../lib/isAdmin');
2
3async function kickCommand(sock, chatId, senderId, mentionedJids, message) {
4 const isOwner = message.key.fromMe;
5 if (!isOwner) {
6 const { isSenderAdmin, isBotAdmin } = await isAdmin(sock, chatId, senderId);
7
8 if (!isBotAdmin) {
9 await sock.sendMessage(chatId, { text: 'Please make the bot an admin first.' }, { quoted: message });
10 return;
11 }
12
13 if (!isSenderAdmin) {
14 await sock.sendMessage(chatId, { text: 'Only group admins can use the kick command.' }, { quoted: message });
15 return;
16 }
17 }
18
19 let usersToKick = [];
20
21 if (mentionedJids && mentionedJids.length > 0) {
22 usersToKick = mentionedJids;
23 }
24 else if (message.message?.extendedTextMessage?.contextInfo?.participant) {
25 usersToKick = [message.message.extendedTextMessage.contextInfo.participant];
26 }
27
28 if (usersToKick.length === 0) {
29 await sock.sendMessage(chatId, {
30 text: 'Please mention the user or reply to their message to kick!'
31 }, { quoted: message });
32 return;
33 }
34
35 const botId = sock.user?.id || '';
36 const botLid = sock.user?.lid || '';
37 const botPhoneNumber = botId.includes(':') ? botId.split(':')[0] : (botId.includes('@') ? botId.split('@')[0] : botId);
38 const botIdFormatted = botPhoneNumber + '@s.whatsapp.net';
39
40 // Extract numeric part from bot LID (remove session identifier like :4)
41 const botLidNumeric = botLid.includes(':') ? botLid.split(':')[0] : (botLid.includes('@') ? botLid.split('@')[0] : botLid);
42 const botLidWithoutSuffix = botLid.includes('@') ? botLid.split('@')[0] : botLid;
43
44 const metadata = await sock.groupMetadata(chatId);
45 const participants = metadata.participants || [];
46
47 const isTryingToKickBot = usersToKick.some(userId => {
48 const userPhoneNumber = userId.includes(':') ? userId.split(':')[0] : (userId.includes('@') ? userId.split('@')[0] : userId);
49 const userLidNumeric = userId.includes('@lid') ? userId.split('@')[0].split(':')[0] : '';
50
51 // Direct match checks
52 const directMatch = (
53 userId === botId ||
54 userId === botLid ||
55 userId === botIdFormatted ||
56 userPhoneNumber === botPhoneNumber ||
57 (userLidNumeric && botLidNumeric && userLidNumeric === botLidNumeric)
58 );
59
60 if (directMatch) {

Callers 1

handleMessagesFunction · 0.85

Calls 1

isAdminFunction · 0.85

Tested by

no test coverage detected