const { getStreamsFromAttachment, getTime } = global.utils; module.exports = { config: { name: "إرسال_إشعار", aliases: ["sendnoti"], version: "1.5", author: "NTKhang", countDown: 5, role: 0, description: { vi: "Tạo và gửi thông báo đến các nhóm do bạn quản lý", en: "Create and send notification to groups that you manage" , ar: "إرسال إشعار عام" }, category: "box chat", guide: { vi: " {pn} create : Tạo một group noti (notification) mới với tên gọi " + "\n Ví dụ:\n {pn} create TEAM1" + "\n\n {pn} add : thêm box chat hiện tại vào group noti (bạn phải là quản trị viên của box chat này)" + "\n Ví dụ:\n {pn} add TEAM1" + "\n\n {pn} list: hiển thị danh sách các group noti bạn đang quản lý" + "\n\n {pn} info : xem thông tin của group noti " + "\n\n {pn} delete: xóa box chat hiện tại khỏi group noti (bạn phải là người tạo group noti này)" + "\n Ví dụ:\n {pn} delete TEAM1" + "\n\n {pn} send | : gửi thông báo tới tất cả các nhóm trong group noti (bạn phải là quản trị viên của những box đó)" + "\n Ví dụ:\n {pn} remove TEAM1" + "\n\n {pn} remove : xóa group noti (bạn phải là người tạo group noti )" + "\n Ví dụ:\n {pn} remove TEAM1", en: " {pn} create : Create a new notification group with name " + "\n مثال:\n {pn} create TEAM1" + "\n\n {pn} add : add current box chat to notification group (you must be admin of this box chat)" + "\n مثال:\n {pn} add TEAM1" + "\n\n {pn} list: show list of notification groups you are managing" + "\n\n {pn} info : view info of notification group " + "\n\n {pn} delete: remove current box chat from notification group (you must be creator of this group)" + "\n مثال:\n {pn} delete TEAM1" + "\n\n {pn} send | : send notification to all groups in notification group (you must be admin of those groups)" + "\n مثال:\n {pn} remove TEAM1" + "\n\n {pn} remove : remove notification group (you must be creator of notification group )" + "\n مثال:\n {pn} remove TEAM1" , ar: "{pn} <الرسالة>" } }, langs: { vi: { missingGroupName: "Vui lòng nhập tên groupNoti", groupNameExists: "Group send noti mang tên %1 đã được tạo trước đó bởi bạn rồi, vui lòng chọn tên khác", createdGroup: "Đã tạo group send noti thành công:\n- Name: %1\n- ID: %2", missingGroupNameToAdd: "Vui lòng nhập tên groupNoti bạn muốn thêm nhóm chat này vào", groupNameNotExists: "Hiện tại bạn chưa tạo/quản lý group noti nào mang tên: %1", notAdmin: "Bạn không phải là quản trị viên của nhóm chat này", added: "Đã thêm nhóm chat hiện tại vào group noti: %1", missingGroupNameToDelete: "Vui lòng nhập tên groupNoti bạn muốn xóa nhóm chat này khỏi danh sách", notInGroup: "Hiện tại nhóm chat này chưa có trong group noti %1", emptyList: "Hiện tại bạn chưa tạo/quản lý group noti nào", showList: "Danh sách các group noti bạn đang quản lý:\nHiển thị theo định dạng:\n - \n%1", deleted: "Đã xóa nhóm chat hiện tại khỏi group noti: %1", failed: "Đã xảy ra lỗi khi gửi thông báo đến %1 nhóm chat: \n%2", missingGroupNameToRemove: "Vui lòng nhập tên groupNoti bạn muốn xóa bỏ", removed: "Đã xóa bỏ group noti: %1", missingGroupNameToSend: "Vui lòng nhập tên groupNoti bạn muốn gủi tin nhắn", groupIsEmpty: "Hiện tại group noti \"%1\" chưa có nhóm chat nào trong danh sách", sending: "Đang gửi thông báo đến %1 nhóm chat", success: "Đã gửi thông báo đến %1 nhóm chat trong group noti \"%2\" thành công", notAdminOfGroup: "Bạn không phải là quản trị viên của nhóm này", missingGroupNameToView: "Vui lòng nhập tên groupNoti bạn muốn xem thông tin", groupInfo: "- Group Name: %1\n- ID: %2\n- Ngày tạo: %3\n%4 ", groupInfoHasGroup: "- Gồm các nhóm chat: \n%1", noGroup: "Hiện tại bạn chưa tạo/quản lý group noti nào" }, en: { missingGroupName: "Please enter groupNoti name", groupNameExists: "Notification group with name %1 has been created by you before, please choose another name", createdGroup: "Created notification group successfully:\n- Name: %1\n- ID: %2", missingGroupNameToAdd: "Please enter groupNoti name you want to add this group chat to", groupNameNotExists: "You have not created/manage any notification group with name: %1", notAdmin: "You are not admin of this group chat", added: "Added current group chat to notification group: %1", missingGroupNameToDelete: "Please enter groupNoti name you want to delete this group chat from list", notInGroup: "Current group chat is not in notification group %1", emptyList: "You have not created/manage any notification group", showList: "List of notification groups you are managing:\nShow in format:\n - \n%1", deleted: "Deleted current group chat from notification group: %1", failed: "Failed to send notification to %1 group chats: \n%2", missingGroupNameToRemove: "Please enter groupNoti name you want to remove", removed: "Removed notification group: %1", missingGroupNameToSend: "Please enter groupNoti name you want to send message", groupIsEmpty: "Notification group \"%1\" is empty", sending: "Sending notification to %1 group chats", success: "Sent notification to %1 group chats in notification group \"%2\" successfully", notAdminOfGroup: "You are not admin of this group", missingGroupNameToView: "Please enter groupNoti name you want to view info", groupInfo: "- Group Name: %1\n - ID: %2\n - Created at: %3\n%4 ", groupInfoHasGroup: "- Has group chats: \n%1", noGroup: "You have not created/manage any notification group" }, ar: { missingGroupName: "Please enter groupNoti name", groupNameExists: "Notification group with name %1 has been created by you before, please choose another name", createdGroup: "Created notification group successfully:\\n- Name: %1\\n- ID: %2", missingGroupNameToAdd: "Please enter groupNoti name you want to add this group chat to", groupNameNotExists: "You have not created/manage any notification group with name: %1", notAdmin: "You are not admin of this group chat", added: "Added current group chat to notification group: %1", missingGroupNameToDelete: "Please enter groupNoti name you want to delete this group chat from list", notInGroup: "Current group chat is not in notification group %1", emptyList: "You have not created/manage any notification group", showList: "List of notification groups you are managing:\\nShow in format:\\n - \\n%1", deleted: "Deleted current group chat from notification group: %1", failed: "Failed to send notification to %1 group chats: \\n%2", missingGroupNameToRemove: "Please enter groupNoti name you want to remove", removed: "Removed notification group: %1", missingGroupNameToSend: "Please enter groupNoti name you want to send message", groupIsEmpty: "Notification group \\\"%1\\\" is empty", sending: "Sending notification to %1 group chats", success: "Sent notification to %1 group chats in notification group \\\"%2\\\" successfully", notAdminOfGroup: "You are not admin of this group", missingGroupNameToView: "Please enter groupNoti name you want to view info", groupInfo: "- Group Name: %1\\n - ID: %2\\n - Created at: %3\\n%4 ", groupInfoHasGroup: "- Has group chats: \\n%1", noGroup: "You have not created/manage any notification group" } }, onStart: async function ({ message, event, args, usersData, threadsData, api, getLang, role }) { const { threadID, senderID } = event; const groupsSendNotiData = await usersData.get(senderID, 'data.groupsSendNoti', []); switch (args[0]) { case "create": { const groupName = args.slice(1).join(' '); const groupID = Date.now(); if (!groupName) return message.reply(getLang('missingGroupName')); const groupsSendNotiData = await usersData.get(senderID, 'data.groupsSendNoti', []); if (groupsSendNotiData.some(item => item.groupName === groupName)) return message.reply(getLang('groupNameExists', groupName)); groupsSendNotiData.push({ groupName, groupID, threadIDs: [] }); await usersData.set(senderID, groupsSendNotiData, 'data.groupsSendNoti'); message.reply(getLang('createdGroup', groupName, groupID)); break; } case "add": { const groupName = args.slice(1).join(' '); if (!groupName) return message.reply(getLang('missingGroupNameToAdd')); const getGroup = (groupsSendNotiData || []).find(item => item.groupName == groupName); if (!getGroup) return message.reply(getLang('groupNameNotExists', groupName)); if (role < 1) return message.reply(getLang('notAdmin')); getGroup.threadIDs.push(threadID); await usersData.set(senderID, groupsSendNotiData, 'data.groupsSendNoti'); message.reply(getLang('added', groupName)); break; } case "list": { if (!groupsSendNotiData.length) return message.reply(getLang('noGroup')); const msg = groupsSendNotiData.reduce((acc, item) => { acc += `+ ${item.groupName} - ${item.threadIDs.length}\n`; return acc; }, ''); message.reply(getLang('showList', msg)); break; } case "delete": { const groupName = args.slice(1).join(' '); if (!groupName) return message.reply(getLang('missingGroupNameToDelete')); const getGroup = (groupsSendNotiData || []).find(item => item.groupName == groupName); if (!getGroup) return message.reply(getLang('groupNameNotExists', groupName)); const findIndexThread = getGroup.threadIDs.findIndex(item => item == threadID); if (findIndexThread == -1) return message.reply(getLang('notInGroup', groupName)); getGroup.threadIDs.splice(findIndexThread, 1); await usersData.set(senderID, groupsSendNotiData, 'data.groupsSendNoti'); message.reply(getLang('deleted', groupName)); break; } case "remove": case "-r": { const groupName = args.slice(1).join(' '); if (!groupName) return message.reply(getLang('missingGroupNameToRemove')); const findIndex = (groupsSendNotiData.threadIDs || []).findIndex(item => item.groupName == groupName); if (findIndex == -1) return message.reply(getLang('groupNameNotExists', groupName)); groupsSendNotiData.splice(findIndex, 1); await usersData.set(senderID, groupsSendNotiData, 'data.groupsSendNoti'); message.reply(getLang('removed', groupName)); break; } case "send": { const groupName = args.slice(1).join(' ').split('|')[0].trim(); if (!groupName) return message.reply(getLang('missingGroupNameToSend')); const getGroup = (groupsSendNotiData || []).find(item => item.groupName == groupName); if (!getGroup) return message.reply(getLang('groupNameNotExists', groupName)); if (getGroup.threadIDs.length == 0) return message.reply(getLang('groupIsEmpty', groupName)); const messageSend = args.slice(2).join(' ').split('|').slice(1).join(' ').trim(); const formSend = { body: messageSend }; if (event.attachments.length || event.attachments.messageReply?.attachments.length) formSend.attachment = await getStreamsFromAttachment([...event.attachments, ...(event.messageReply?.attachments || [])].filter(item => ["photo", 'png', "animated_image", "video", "audio"].includes(item.type))); const success = []; const failed = []; const pendings = []; const { threadIDs } = getGroup; const msgSend = message.reply(getLang('sending', groupName, threadIDs.length)); for (const tid of threadIDs) { await new Promise((r) => setTimeout(r, 1000)); pendings.push( new Promise(async (resolve, reject) => { const { adminIDs, threadName } = await threadsData.get(tid); if (!adminIDs.includes(senderID)) return reject({ error: 'PERMISSION_DENIED', threadID: tid, threadName }); api.sendMessage(formSend, tid, (err) => { if (err) reject({ ...err, threadID: tid, threadName }); resolve({ threadID: tid, threadName }); }); }) ); } for (const item of pendings) { try { await item; success.push({ threadID: item.threadID, threadName: item.threadName }); } catch (err) { failed.push({ threadID: item.threadID, threadName: item.threadName, error: item.error }); } } api.unsendMessage((await msgSend).messageID); let msg = ""; if (success.length) msg += `${getLang('success', success.length, groupName)}\n`; if (failed.length) msg += getLang('failed', failed.length, failed.map(item => `\n- id: ${item.threadID}` + `\n- Name: ${item.threadName}` + `\n- Error: ${item.error == 'PERMISSION_DENIED' ? getLang('notAdminOfGroup') : ''}` ).join('\n') ); message.reply(msg); break; } case "info": { const groupName = args.slice(1).join(' '); if (!groupName) return message.reply(getLang('missingGroupNameToView')); const getGroup = (groupsSendNotiData || []).find(item => item.groupName == groupName); if (!getGroup) return message.reply(getLang('groupNameNotExists', groupName)); const { threadIDs } = getGroup; const allThreadData = await threadsData.getAll(); const msg = threadIDs.reduce((acc, tid) => { const threadData = allThreadData.find(i => i.threadID == tid) || {}; acc += ` + ID: ${tid}\n + Name: ${threadData.threadName || 'null'}\n\n`; return acc; }, ''); message.reply(getLang('groupInfo', groupName, getGroup.groupID, getTime(getGroup.groupID, 'DD/MM/YYYY HH:mm:ss'), msg ? getLang('groupInfoHasGroup', msg) : getLang('groupIsEmpty', groupName))); break; } default: { return message.SyntaxError(); } } } };