File size: 1,693 Bytes
183a3de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import schedule from 'node-schedule';
import fs from 'fs/promises';
import path from 'path';

const countdownJobs = new Map();
const groupSettingsPath = path.resolve('./assets/data/group_settings.json');

async function isBotActive(threadId) {
    try {
        const data = await fs.readFile(groupSettingsPath, 'utf-8');
        const groupSettings = JSON.parse(data);
        return groupSettings[threadId]?.activeBot === true;
    } catch (error) {
        console.error('Lỗi đọc group_settings.json:', error);
        return false;
    }
}

export async function sendReactionWaitingCountdown(api, message, count) {
    const messages = Array(count).fill(message);
    const messageId = message.data.cliMsgId || Date.now().toString();
    const threadId = message.threadId || message.data?.threadId;

    const isActive = await isBotActive(threadId);
    if (!isActive) {
        return;
    }

    const date = new Date(Date.now() + 300);
    const job = schedule.scheduleJob(date, async () => {
        try {
            while (messages.length > 0) {
                try {
                    await api.addReaction("CLOCK", messages);
                    await new Promise(resolve => setTimeout(resolve, 1000));
                    await api.addReaction("UNDO", messages);
                } catch (error) {
                }
                messages.splice(0, 1);
            }
        } catch (error) {
            console.error(`Error in countdown job ${messageId}:`, error);
        } finally {
            job.cancel();
            countdownJobs.delete(messageId);
        }
    });
    
    countdownJobs.set(messageId, job);
}