File size: 9,869 Bytes
2821330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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));
}