(sock, chatId, mentionedJids, message)
| 1 | const isAdmin = require('../lib/isAdmin'); |
| 2 | |
| 3 | async function demoteCommand(sock, chatId, mentionedJids, message) { |
| 4 | try { |
| 5 | // First check if it's a group |
| 6 | if (!chatId.endsWith('@g.us')) { |
| 7 | await sock.sendMessage(chatId, { |
| 8 | text: 'This command can only be used in groups!' |
| 9 | }); |
| 10 | return; |
| 11 | } |
| 12 | |
| 13 | // Check admin status first, before any other operations |
| 14 | try { |
| 15 | const adminStatus = await isAdmin(sock, chatId, message.key.participant || message.key.remoteJid); |
| 16 | |
| 17 | if (!adminStatus.isBotAdmin) { |
| 18 | await sock.sendMessage(chatId, { |
| 19 | text: '❌ Error: Please make the bot an admin first to use this command.' |
| 20 | }); |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | if (!adminStatus.isSenderAdmin) { |
| 25 | await sock.sendMessage(chatId, { |
| 26 | text: '❌ Error: Only group admins can use the demote command.' |
| 27 | }); |
| 28 | return; |
| 29 | } |
| 30 | } catch (adminError) { |
| 31 | console.error('Error checking admin status:', adminError); |
| 32 | await sock.sendMessage(chatId, { |
| 33 | text: '❌ Error: Please make sure the bot is an admin of this group.' |
| 34 | }); |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | let userToDemote = []; |
| 39 | |
| 40 | // Check for mentioned users |
| 41 | if (mentionedJids && mentionedJids.length > 0) { |
| 42 | userToDemote = mentionedJids; |
| 43 | } |
| 44 | // Check for replied message |
| 45 | else if (message.message?.extendedTextMessage?.contextInfo?.participant) { |
| 46 | userToDemote = [message.message.extendedTextMessage.contextInfo.participant]; |
| 47 | } |
| 48 | |
| 49 | // If no user found through either method |
| 50 | if (userToDemote.length === 0) { |
| 51 | await sock.sendMessage(chatId, { |
| 52 | text: '❌ Error: Please mention the user or reply to their message to demote!' |
| 53 | }); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | // Add delay to avoid rate limiting |
| 58 | await new Promise(resolve => setTimeout(resolve, 1000)); |
| 59 | |
| 60 | await sock.groupParticipantsUpdate(chatId, userToDemote, "demote"); |
no test coverage detected