Jonell01's picture
Upload 64 files
2821330 verified
const fs = require('fs');
const path = require('path');
const bannedUsersDir = path.join(__dirname, 'cache');
const warn = {};
if (!fs.existsSync(bannedUsersDir)) {
fs.mkdirSync(bannedUsersDir, { recursive: true });
}
module.exports.config = {
name: "member",
version: "1.0.0",
hasPermission: 2,
description: "Manage group members",
usePrefix: true,
hide: true,
commandCategory: "System",
usages: "",
cooldowns: 2,
credits: "Jonell Magallanes"
};
module.exports.handleEvent = async function ({ api, event }) {
const botId = api.getCurrentUserID();
const threadId = event.threadID.toString();
if (event.body && event.isGroup) {
const userId = event.senderID.toString();
const bannedUsersFilePath = path.join(bannedUsersDir, `${threadId}.json`);
let bannedUsers = [];
if (fs.existsSync(bannedUsersFilePath)) {
bannedUsers = JSON.parse(fs.readFileSync(bannedUsersFilePath));
}
if (bannedUsers.includes(userId)) {
try {
api.removeUserFromGroup(userId, threadId);
const userInfo = await api.getUserInfo(userId);
const userName = userInfo[userId].name;
api.sendMessage(`๐Ÿ‘ค ๐—ฅ๐—ฒ๐—บ๐—ผ๐˜ƒ๐—ฒ๐—ฑ ๐—ณ๐—ฟ๐—ผ๐—บ ๐˜๐—ต๐—ฒ ๐—š๐—ฟ๐—ผ๐˜‚๐—ฝ\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\nUser ${userName} is banned from this group and has been removed.`, threadId);
} catch (error) {
console.error(`Failed to handle banned user removal: ${error}`);
}
}
}
if (event.logMessageType === 'log:subscribe' && event.logMessageData.addedParticipants.some(participant => participant.userFbId)) {
const addedUserId = event.logMessageData.addedParticipants[0].userFbId.toString();
const adderUserId = event.author.toString();
const bannedUsersFilePath = path.join(bannedUsersDir, `${threadId}.json`);
let bannedUsers = [];
if (fs.existsSync(bannedUsersFilePath)) {
bannedUsers = JSON.parse(fs.readFileSync(bannedUsersFilePath));
}
if (bannedUsers.includes(addedUserId)) {
try {
api.removeUserFromGroup(addedUserId, threadId);
const addedUserInfo = await api.getUserInfo(addedUserId);
const addedUserName = addedUserInfo[addedUserId].name;
api.sendMessage(`๐Ÿ‘ค ๐—ฅ๐—ฒ๐—บ๐—ผ๐˜ƒ๐—ฒ๐—ฑ ๐—ณ๐—ฟ๐—ผ๐—บ ๐˜๐—ต๐—ฒ ๐—š๐—ฟ๐—ผ๐˜‚๐—ฝ\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\nUser ${addedUserName} is banned from this group and has been removed.`, threadId);
if (!warn[adderUserId]) {
warn[adderUserId] = 1;
const adderUserInfo = await api.getUserInfo(adderUserId);
const adderUserName = adderUserInfo[adderUserId].name;
api.sendMessage(`โš ๏ธ ๐—ช๐—ฎ๐—ฟ๐—ป๐—ถ๐—ป๐—ด\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n${adderUserName}, you attempted to re-add a banned user. This is your first warning.`, threadId);
} else {
warn[adderUserId]++;
if (warn[adderUserId] >= 2) {
api.removeUserFromGroup(adderUserId, threadId);
const adderUserInfo = await api.getUserInfo(adderUserId);
const adderUserName = adderUserInfo[adderUserId].name;
api.sendMessage(`๐Ÿ‘ค ๐—ฅ๐—ฒ๐—บ๐—ผ๐˜ƒ๐—ฒ๐—ฑ ๐—ณ๐—ฟ๐—ผ๐—บ ๐˜๐—ต๐—ฒ ๐—š๐—ฟ๐—ผ๐˜‚๐—ฝ\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n${adderUserName}, you have been removed for repeatedly attempting to re-add banned users.`, threadId);
} else {
const adderUserInfo = await api.getUserInfo(adderUserId);
const adderUserName = adderUserInfo[adderUserId].name;
api.sendMessage(`โš ๏ธ ๐—ช๐—ฎ๐—ฟ๐—ป๐—ถ๐—ป๐—ด\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n${adderUserName}, you attempted to re-add a banned user again. This is your final warning.`, threadId);
}
}
} catch (error) {
console.error(`Failed to handle user re-addition: ${error}`);
}
}
}
};
module.exports.run = async ({ api, event, args }) => {
const botId = api.getCurrentUserID();
const threadId = event.threadID.toString();
// Check admin privileges directly within this function
try {
const threadInfo = await api.getThreadInfo(threadId);
const adminIds = threadInfo.adminIDs.map(admin => admin.id);
if (!adminIds.includes(botId)) {
api.sendMessage("Need Admin Privilege to perform administrative actions.", threadId);
return;
}
} catch (error) {
console.error(`Failed to check admin privileges: ${error}`);
api.sendMessage("Failed to check admin privileges.", threadId);
return;
}
const command = args[0];
const bannedUsersFilePath = path.join(bannedUsersDir, `${threadId}.json`);
let bannedUsers = [];
if (fs.existsSync(bannedUsersFilePath)) {
bannedUsers = JSON.parse(fs.readFileSync(bannedUsersFilePath));
}
if (command === 'ban') {
const userId = Object.keys(event.mentions)[0] || args[1];
if (userId) {
if (!bannedUsers.includes(userId)) {
bannedUsers.push(userId);
updateBannedUsersFile(bannedUsers, bannedUsersFilePath);
try {
api.removeUserFromGroup(userId, threadId);
const userInfo = await api.getUserInfo(userId);
const userName = userInfo[userId].name;
api.sendMessage(`๐Ÿ‘ค ๐—•๐—ฎ๐—ป๐—ป๐—ฒ๐—ฑ ๐—ณ๐—ฟ๐—ผ๐—บ ๐˜๐—ต๐—ฒ ๐—š๐—ฟ๐—ผ๐˜‚๐—ฝ\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\nUser ${userName} has been banned and removed from this group.`, threadId);
} catch (error) {
console.error(`Failed to ban user: ${error}`);
}
} else {
try {
const userInfo = await api.getUserInfo(userId);
const userName = userInfo[userId].name;
api.sendMessage(`โš ๏ธ ๐—”๐—น๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐˜† ๐—•๐—ฎ๐—ป๐—ป๐—ฒ๐—ฑ\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\nUser ${userName} is already banned from this group.`, threadId);
} catch (error) {
console.error(`Failed to get user info: ${error}`);
}
}
} else {
api.sendMessage(`โ— ๐—˜๐—ฟ๐—ฟ๐—ผ๐—ฟ\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\nPlease mention a user or provide a user ID to ban.`, threadId);
}
} else if (command === 'unban') {
const userId = Object.keys(event.mentions)[0] || args[1];
if (userId) {
const index = bannedUsers.findIndex(ban => ban === userId);
if (index !== -1) {
bannedUsers.splice(index, 1);
updateBannedUsersFile(bannedUsers, bannedUsersFilePath);
try {
const userInfo = await api.getUserInfo(userId);
const userName = userInfo[userId].name;
api.sendMessage(`โœ… ๐—จ๐—ป๐—ฏ๐—ฎ๐—ป๐—ป๐—ฒ๐—ฑ\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\nUser ${userName} has been unbanned from this group.`, threadId);
} catch (error) {
console.error(`Failed to unban user: ${error}`);
}
} else {
try {
const userInfo = await api.getUserInfo(userId);
const userName = userInfo[userId].name;
api.sendMessage(`โš ๏ธ ๐—ก๐—ผ๐˜ ๐—•๐—ฎ๐—ป๐—ป๐—ฒ๐—ฑ\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\nUser ${userName} is not banned from this group.`, threadId);
} catch (error) {
console.error(`Failed to get user info: ${error}`);
}
}
} else {
api.sendMessage(`โ— ๐—˜๐—ฟ๐—ฟ๐—ผ๐—ฟ\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\nPlease mention a user or provide a user ID to unban.`, threadId);
}
} else if (command === 'list') {
if (bannedUsers.length > 0) {
try {
const userInfo = await api.getUserInfo(bannedUsers);
const bannedList = bannedUsers.map(ban => userInfo[ban].name).join(', ');
api.sendMessage(`๐Ÿ“ ๐—Ÿ๐—ถ๐˜€๐˜ ๐—ผ๐—ณ ๐—•๐—ฎ๐—ป๐—ป๐—ฒ๐—ฑ ๐—จ๐˜€๐—ฒ๐—ฟ๐˜€\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\nBanned users in this group: ${bannedList}`, threadId);
} catch (error) {
console.error(`Failed to get user info for list command: ${error}`);
}
} else {
api.sendMessage(`๐Ÿ“ ๐—Ÿ๐—ถ๐˜€๐˜ ๐—ผ๐—ณ ๐—•๐—ฎ๐—ป๐—ป๐—ฒ๐—ฑ ๐—จ๐˜€๐—ฒ๐—ฟ๐˜€\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\nNo users are currently banned from this group.`, threadId);
}
} else {
api.sendMessage(`โ— ๐—˜๐—ฟ๐—ฟ๐—ผ๐—ฟ\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\nInvalid command. Usage: /member ban/unban/list [@mention or user ID]`, threadId);
}
};
function updateBannedUsersFile(bannedUsers, filePath) {
fs.writeFileSync(filePath, JSON.stringify(bannedUsers, null, 2));
}