File size: 6,673 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
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
import { getCommandConfig } from "../../index.js";
import { getGlobalPrefix } from "../../service-dqt/service.js";
import { writeCommandConfig } from "../../utils/io-json.js";

const permissionMap = {
    1: {
        key: "all",
        name: "Tất Cả Người Dùng",
    },
    2: {
        key: "adminBox",
        name: "Trưởng / Phó Cộng Đồng",
    },
    3: {
        key: "adminBot",
        name: "Quản Trị Bot",
    },
    4: {
        key: "adminLevelHigh",
        name: "Quản Trị Cấp Cao",
    },
};

export function getPermissionCommandName(command) {
    const permission = Object.values(permissionMap).find((p) => p.key === command.permission);
    return permission ? permission.name : "Tất Cả Người Dùng";
}


export async function handleSetCommandActive(api, message, commandParts) {
    const commandConfig = getCommandConfig();
    const prefix = getGlobalPrefix();

    if (commandParts.length < 3) {
        await api.sendMessage(
            {
                msg: "⚠️ Vui lòng nhập đúng cú pháp:" +
                    `\n${prefix}setcmd on/off <tên_lệnh> - Bật/tắt lệnh` +
                    `\n${prefix}setcmd p <tên_lệnh> <level> - Đặt quyền hạn lệnh (1-4)` +
                    `\n${prefix}setcmd cd <tên_lệnh> <giây> - Đặt thời gian chờ lệnh`,
                quote: message,
                ttl: 300000,
            },
            message.threadId,
            message.type
        );
        return;
    }

    const action = commandParts[1].toLowerCase();
    const cmdName = commandParts[2].toLowerCase();
    const command = commandConfig.commands.find(cmd => cmd.name === cmdName);

    if (!command) {
        await api.sendMessage(
            {
                msg: `❌ Không tìm thấy lệnh "${cmdName}"`,
                quote: message,
                ttl: 300000,
            },
            message.threadId,
            message.type
        );
        return;
    }

    switch (action) {
        case "on":
        case "off":
            const newActive = action === "on";
            command.active = newActive;
            writeCommandConfig(commandConfig);
            await api.sendMessage(
                {
                    msg: `✅ Đã ${newActive ? "bật" : "tắt"} lệnh "${cmdName}"`,
                    quote: message,
                    ttl: 300000,
                },
                message.threadId,
                message.type
            );
            break;

        case "p":
            if (commandParts.length < 4) {
                await api.sendMessage(
                    {
                        msg: "⚠️ Vui lòng nhập level quyền hạn (1-4):" +
                            "\n1: all (Tất cả)" +
                            "\n2: adminBox (Quản trị viên nhóm)" +
                            "\n3: adminBot (Quản trị viên bot)" +
                            "\n4: adminLevelHigh (Quản trị viên cấp cao)",
                        quote: message,
                        ttl: 300000,
                    },
                    message.threadId,
                    message.type
                );
                return;
            }

            const permissionLevel = parseInt(commandParts[3]);
            const newPermissionObj = permissionMap[permissionLevel];

            if (!newPermissionObj) {
                await api.sendMessage(
                    {
                        msg: "❌ Level quyền hạn không hợp lệ. Vui lòng chọn từ 1-4:\n" +
                            Object.entries(permissionMap)
                                .map(([level, { key, name }]) => `${level}: ${key} (${name})`)
                                .join("\n"),
                        quote: message,
                        ttl: 300000,
                    },
                    message.threadId,
                    message.type
                );
                return;
            }

            command.permission = newPermissionObj.key;
            writeCommandConfig(commandConfig);
            await api.sendMessage(
                {
                    msg: `✅ Đã thay đổi quyền hạn của lệnh "${cmdName}":\n- Quyền hạn mới: ${newPermissionObj.name}`,
                    quote: message,
                    ttl: 300000,
                },
                message.threadId,
                message.type
            );
            break;

        case "cd":
            if (commandParts.length < 4) {
                await api.sendMessage(
                    {
                        msg: `⚠️ Vui lòng nhập thời gian chờ (tính bằng giây)`,
                        quote: message,
                        ttl: 300000,
                    },
                    message.threadId,
                    message.type
                );
                return;
            }

            const newCountdown = parseInt(commandParts[3]);
            if (isNaN(newCountdown) || newCountdown < 0) {
                await api.sendMessage(
                    {
                        msg: "❌ Thời gian countdown phải là số nguyên dương",
                        quote: message,
                        ttl: 300000,
                    },
                    message.threadId,
                    message.type
                );
                return;
            }

            const oldCountdown = command.countdown || 0;
            command.countdown = newCountdown;
            writeCommandConfig(commandConfig);
            await api.sendMessage(
                {
                    msg: `✅ Đã cập nhật thời gian chờ của lệnh "${cmdName}":\n- Cũ: ${oldCountdown}s\n- Mới: ${newCountdown}s`,
                    quote: message,
                    ttl: 300000,
                },
                message.threadId,
                message.type
            );
            break;

        default:
            await api.sendMessage(
                {
                    msg: `❌ Hành động không hợp lệ. Vui lòng sử dụng:` +
                        `\n- on/off: Bật/tắt lệnh` +
                        `\n- p: Đặt quyền hạn` +
                        `\n- cd: Đặt thời gian chờ`,
                    quote: message,
                    ttl: 300000,
                },
                message.threadId,
                message.type
            );
            break;
    }
}