File size: 8,847 Bytes
8b2ea03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf1f9e3
8b2ea03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
const moment = require("moment-timezone")
const chalk = require("chalk")
const fs = require('fs-extra')
const util = require("util")
const {
    getRandom,
    getGroupAdmins,
    sleep
} = require("../lib/myfunc");

module.exports = async (conn, anu) => {
    var jeda = false;
    if (jeda) return console.log("spam welcome active");
    jeda = true;
    try {
        const {
            id,
            participants,
            action
        } = anu;


        if (action === "demote") {
            let members = conn.chats[id].metadata.participants
            await members.forEach(participant => {
                if (participant.id === participants[0]) {
                    participant.admin = null;
                }
            });
            let nana = members.filter(v => v.id === participants[0])
            console.log(nana)
        } else if (action === "promote") {
            let members = conn.chats[id].metadata.participants
            await members.forEach(participant => {
                if (participant.id === participants[0]) {
                    participant.admin = 'admin'
                }
            });
            let nana = members.filter(v => v.id === participants[0])
            console.log(nana)
        }



        if (anu.participants[0].includes('@lid')) return console.log(chalk.magenta('log 1'))
        if ((action == "remove" || action == "promote" || action == "demote") &&
            anu.participants[0].split("@")[0].includes(conn.user.id.split(":")[0])
        )
            return console.log(chalk.magenta('log 2'))
        const myGroup = Object.keys(db.data.chats);
        const from = anu.id
        const botNumber = conn.user.jid;
        const groupMetadata = ((conn.chats[from] || {}).metadata || await conn.groupMetadata(from).catch(_ => null)) || {}
        const groupName = groupMetadata.subject || [];
        const groupLength = groupMetadata.participants.length;
        const sender = conn.decodeJid(anu.participants[0])
        if (sender.includes('_')) return log('log 3')
        const senderNumber = sender.split("@")[0];
        const groupMembers = groupMetadata.participants || [];
        const groupAdmins = getGroupAdmins(groupMembers) || [];
        const groupDesc = groupMetadata.desc || [];
        const groupOwner = groupMetadata.owner || [];
        const user =
            groupMembers.find((u) => conn.decodeJid(u.id) === sender) || {};
        const bot =
            groupMembers.find((u) => conn.decodeJid(u.id) == conn.user.jid) || {};

        const isRAdmin = (user && user.admin == "superadmin") || false;
        const isAdmin = isRAdmin || (user && user.admin == "admin") || false;
        const isBotAdmin = (bot && bot.admin == "admin") || false;
        const pushname = await conn.getName(sender);
        const oneMem = anu.participants.length === 1;
        const itsMe = sender === botNumber;
        const timeWib = moment.tz("Asia/Jakarta").format("HH:mm");
        const chat = global.db.data.chats[id];
        const add = action == "add";
        const remove = action == "remove";
        const memb = groupMetadata.participants.length
        const isBanchat = myGroup.includes(from) ?
            db.data.chats[from].banchat :
            false;
        if (isBanchat) {
            return console.log(chalk.magenta('log 4'))
        }
        let m = {
            chat: from,
            pushname: pushname,
            sender: sender,
        };

        if (!chat) return console.log(chalk.magenta('log 5'))
        let sBye = chat.sBye;
        let sWelcome = chat.sWelcome;

        if (add && oneMem)
            console.log(
                chalk.magenta("[GROUP UPDATE]"),
                chalk.green(`${pushname} has joined from g`),
                chalk.magenta(`${groupName}`)
            );
        if (remove && oneMem)
            console.log(
                chalk.magenta("[GROUP UPDATE]"),
                chalk.green(`${pushname} has left the gc`),
                chalk.magenta(`${groupName}`)
            );

    
        let kickon = db.data.kickon[from];
        if (add && kickon && kickon.includes(senderNumber)) {
            let teks = `@user is not allowed to enter
because he has left this group before,
and has also been marked as a bad user`;
            let text = teks.replace("user", await conn.getName(sender));

            await conn.sendMessage(
                from, {
                    text,
                    mentions: [sender],
                    contextInfo: {
                        mentionedJid: [sender]
                    }
                },

            );
            if (!isBotAdmin)
                return conn.sendMessage(
                    from, {
                        text: `Failed to eject @${senderNumber} from the group because the bot is not an admin`,
                        contextInfo: {
                            mentionedJid: [sender]
                        },
                    },

                );
            if (isBotAdmin)
                return conn.groupParticipantsUpdate(from, [sender], "remove");
        }
        let welcome = 'https://telegra.ph/file/5bedca1f745b1649ecf0b.jpg'
        let goodbye = 'https://telegra.ph/file/c9b18fcfa9df16f13fd51.jpg'
        if (action == "add") {
            var link = welcome;
        } else {
            var link = goodbye;
        }

        const botRun = global.db.data.others['runtime']
        const botTime = botRun ? (new Date - botRun.runtime) : "Not detected"
        const runTime = clockString(botTime)
        let mimetype = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'

        let contextInfo = {
            forwardingScore: 1,
            isForwarded: true,
            mentionedJid: [sender],
            forwardednewsletterMessageInfo: {
                newsletterJid,
                serverMessageId: 100,
                newsletterName
            },
            externalAdReply: {
                showAdAttribution: false,
                title: `${action == "add"? 'W E L C O M E': 'G O O D  B Y E'}`,
                body: `BY 𝐁𝐋𝐔𝐄 πƒπ„πŒπŽπ`,
                sourceUrl: global.myUrl,
                mediaType: 1,
                renderLargerThumbnail: true,
                thumbnailUrl: link,
            }
        }




        switch (action) {

            case "add": {
                if (!chat.welcome) return
                let teks = `*Hello @user*
*welcome to group* ${groupName}
*Description : \n@desc*
${sWelcome}`;
                const welcomeText = (chat.sWelcome || teks)
                    .replace("user", await conn.getName(sender))
                    .replace("@desc", groupDesc.toString() || "unknow")
                    .replace("@subject", groupName);
                if (chat.welcome && !itsMe && oneMem) conn.sendMessage(
                    from, {
                        document: fs.readFileSync('./package.json'),
                        caption: welcomeText,
                        fileName: copyright,
                        mimetype,
                        pageCount: 100,
                        fileLength: 999999999999,
                        contextInfo
                    }
                );


            }
            break;

            case "remove": {
                if (!chat.welcome) return
                let teks = `Goodbye @user
${sBye}`;
                const byeText = (chat.sBye || teks)
                    .replace("user", await conn.getName(sender))
                    .replace("@desc", groupDesc.toString() || "unknow")
                    .replace("@subject", groupName);
                if (chat.welcome && !itsMe && oneMem) conn.sendMessage(
                    from, {
                        document: fs.readFileSync('./package.json'),
                        caption: byeText,
                        fileName: copyright,
                        mimetype,
                        pageCount: 100,
                        fileLength: 999999999999,
                        contextInfo
                    }
                );
            }
            break




        }

        await sleep(5000);
        jeda = false;
    } catch (err) {

        jeda = false;
        console.log(err);
        let e = String(err);
        if (e.includes("this.isZero")) {
            return;
        }
        if (e.includes("rate-overlimit")) {
            return;
        }
        if (e.includes("connection Closed")) {
            return;
        }
        if (e.includes("Timed Out")) {
            return;
        }
        console.log(chalk.white("GROUP :"), chalk.green(e));

        let text = `${util.format(anu)}

${util.format(groupMetadata)}

${util.format(err)}`
        conn.sendMessage(ownerBot, {
            text
        })

    }
};