(sock, chatId, senderId, message, durationInMinutes)
| 1 | const isAdmin = require('../lib/isAdmin'); |
| 2 | |
| 3 | async function muteCommand(sock, chatId, senderId, message, durationInMinutes) { |
| 4 | |
| 5 | |
| 6 | const { isSenderAdmin, isBotAdmin } = await isAdmin(sock, chatId, senderId); |
| 7 | if (!isBotAdmin) { |
| 8 | await sock.sendMessage(chatId, { text: 'Please make the bot an admin first.' }, { quoted: message }); |
| 9 | return; |
| 10 | } |
| 11 | |
| 12 | if (!isSenderAdmin) { |
| 13 | await sock.sendMessage(chatId, { text: 'Only group admins can use the mute command.' }, { quoted: message }); |
| 14 | return; |
| 15 | } |
| 16 | |
| 17 | try { |
| 18 | // Mute the group |
| 19 | await sock.groupSettingUpdate(chatId, 'announcement'); |
| 20 | |
| 21 | if (durationInMinutes !== undefined && durationInMinutes > 0) { |
| 22 | const durationInMilliseconds = durationInMinutes * 60 * 1000; |
| 23 | await sock.sendMessage(chatId, { text: `The group has been muted for ${durationInMinutes} minutes.` }, { quoted: message }); |
| 24 | |
| 25 | // Set timeout to unmute after duration |
| 26 | setTimeout(async () => { |
| 27 | try { |
| 28 | await sock.groupSettingUpdate(chatId, 'not_announcement'); |
| 29 | await sock.sendMessage(chatId, { text: 'The group has been unmuted.' }); |
| 30 | } catch (unmuteError) { |
| 31 | console.error('Error unmuting group:', unmuteError); |
| 32 | } |
| 33 | }, durationInMilliseconds); |
| 34 | } else { |
| 35 | await sock.sendMessage(chatId, { text: 'The group has been muted.' }, { quoted: message }); |
| 36 | } |
| 37 | } catch (error) { |
| 38 | console.error('Error muting/unmuting the group:', error); |
| 39 | await sock.sendMessage(chatId, { text: 'An error occurred while muting/unmuting the group. Please try again.' }, { quoted: message }); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | module.exports = muteCommand; |
no test coverage detected