(sock, chatId, message)
| 5 | const { isSudo } = require('../lib/index'); |
| 6 | |
| 7 | async function unbanCommand(sock, chatId, message) { |
| 8 | // Restrict in groups to admins; in private to owner/sudo |
| 9 | const isGroup = chatId.endsWith('@g.us'); |
| 10 | if (isGroup) { |
| 11 | const senderId = message.key.participant || message.key.remoteJid; |
| 12 | const { isSenderAdmin, isBotAdmin } = await isAdmin(sock, chatId, senderId); |
| 13 | if (!isBotAdmin) { |
| 14 | await sock.sendMessage(chatId, { text: 'Please make the bot an admin to use .unban', ...channelInfo }, { quoted: message }); |
| 15 | return; |
| 16 | } |
| 17 | if (!isSenderAdmin && !message.key.fromMe) { |
| 18 | await sock.sendMessage(chatId, { text: 'Only group admins can use .unban', ...channelInfo }, { quoted: message }); |
| 19 | return; |
| 20 | } |
| 21 | } else { |
| 22 | const senderId = message.key.participant || message.key.remoteJid; |
| 23 | const senderIsSudo = await isSudo(senderId); |
| 24 | if (!message.key.fromMe && !senderIsSudo) { |
| 25 | await sock.sendMessage(chatId, { text: 'Only owner/sudo can use .unban in private chat', ...channelInfo }, { quoted: message }); |
| 26 | return; |
| 27 | } |
| 28 | } |
| 29 | let userToUnban; |
| 30 | |
| 31 | // Check for mentioned users |
| 32 | if (message.message?.extendedTextMessage?.contextInfo?.mentionedJid?.length > 0) { |
| 33 | userToUnban = message.message.extendedTextMessage.contextInfo.mentionedJid[0]; |
| 34 | } |
| 35 | // Check for replied message |
| 36 | else if (message.message?.extendedTextMessage?.contextInfo?.participant) { |
| 37 | userToUnban = message.message.extendedTextMessage.contextInfo.participant; |
| 38 | } |
| 39 | |
| 40 | if (!userToUnban) { |
| 41 | await sock.sendMessage(chatId, { |
| 42 | text: 'Please mention the user or reply to their message to unban!', |
| 43 | ...channelInfo |
| 44 | }, { quoted: message }); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | try { |
| 49 | const bannedUsers = JSON.parse(fs.readFileSync('./data/banned.json')); |
| 50 | const index = bannedUsers.indexOf(userToUnban); |
| 51 | if (index > -1) { |
| 52 | bannedUsers.splice(index, 1); |
| 53 | fs.writeFileSync('./data/banned.json', JSON.stringify(bannedUsers, null, 2)); |
| 54 | |
| 55 | await sock.sendMessage(chatId, { |
| 56 | text: `Successfully unbanned ${userToUnban.split('@')[0]}!`, |
| 57 | mentions: [userToUnban], |
| 58 | ...channelInfo |
| 59 | }); |
| 60 | } else { |
| 61 | await sock.sendMessage(chatId, { |
| 62 | text: `${userToUnban.split('@')[0]} is not banned!`, |
| 63 | mentions: [userToUnban], |
| 64 | ...channelInfo |
no test coverage detected