(sock: any, message: any, args: any, context: BotContext)
| 47 | usage: '.ban @user or reply to message', |
| 48 | |
| 49 | async handler(sock: any, message: any, args: any, context: BotContext) { |
| 50 | const chatId = context.chatId || message.key.remoteJid; |
| 51 | const channelInfo = context.channelInfo || {}; |
| 52 | const _isGroup = context.isGroup; |
| 53 | |
| 54 | let userToBan; |
| 55 | if (message.message?.extendedTextMessage?.contextInfo?.mentionedJid?.length > 0) { |
| 56 | userToBan = message.message.extendedTextMessage.contextInfo.mentionedJid[0]; |
| 57 | } |
| 58 | else if (message.message?.extendedTextMessage?.contextInfo?.participant) { |
| 59 | userToBan = message.message.extendedTextMessage.contextInfo.participant; |
| 60 | } |
| 61 | |
| 62 | if (!userToBan) { |
| 63 | await sock.sendMessage(chatId, { |
| 64 | text: '❌ *Please mention a user or reply to their message*\n\nUsage: `.ban @user` or reply with `.ban`', |
| 65 | ...channelInfo |
| 66 | }, { quoted: message }); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | try { |
| 71 | const botId = sock.user.id.split(':')[0] + '@s.whatsapp.net'; |
| 72 | if (userToBan === botId || userToBan === botId.replace('@s.whatsapp.net', '@lid')) { |
| 73 | await sock.sendMessage(chatId, { |
| 74 | text: '❌ *Cannot ban the bot account*', |
| 75 | ...channelInfo |
| 76 | }, { quoted: message }); |
| 77 | return; |
| 78 | } |
| 79 | } catch(e: any) {} |
| 80 | |
| 81 | try { |
| 82 | const bannedUsers = await getBannedUsers(); |
| 83 | |
| 84 | if (!bannedUsers.includes(userToBan)) { |
| 85 | bannedUsers.push(userToBan); |
| 86 | await saveBannedUsers(bannedUsers); |
| 87 | |
| 88 | await sock.sendMessage(chatId, { |
| 89 | text: `🚫 *User Banned Successfully!*\n\n@${userToBan.split('@')[0]} has been banned from using the bot.\n\n` + |
| 90 | `*Storage:* ${HAS_DB ? 'Database' : 'File System'}`, |
| 91 | mentions: [userToBan], |
| 92 | ...channelInfo |
| 93 | }, { quoted: message }); |
| 94 | } else { |
| 95 | await sock.sendMessage(chatId, { |
| 96 | text: `⚠️ *Already Banned*\n\n@${userToBan.split('@')[0]} is already banned!`, |
| 97 | mentions: [userToBan], |
| 98 | ...channelInfo |
| 99 | }, { quoted: message }); |
| 100 | } |
| 101 | } catch(error: any) { |
| 102 | console.error('Error in ban command:', error); |
| 103 | await sock.sendMessage(chatId, { |
| 104 | text: '❌ *Failed to ban user!*\n\nPlease try again.', |
| 105 | ...channelInfo |
| 106 | }, { quoted: message }); |
nothing calls this directly
no test coverage detected