-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathhandleCommand.js
130 lines (129 loc) · 7.93 KB
/
handleCommand.js
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
module.exports = function ({ api, models, Users, Threads, Currencies }) {
const stringSimilarity = require('string-similarity'),
escapeRegex = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"),
logger = require("../../utils/log.js");
const axios = require('axios')
const moment = require("moment-timezone");
return async function ({ event }) {
const dateNow = Date.now()
const time = moment.tz("Asia/Ho_Chi_minh").format("HH:MM:ss DD/MM/YYYY");
const { allowInbox, PREFIX, ADMINBOT, DeveloperMode, adminOnly, keyAdminOnly } = global.config;
const { userBanned, threadBanned, threadInfo, threadData, commandBanned } = global.data;
const { commands, cooldowns } = global.client;
var { body, senderID, threadID, messageID } = event;
var senderID = String(senderID),
threadID = String(threadID);
const threadSetting = threadData.get(threadID) || {}
const prefixRegex = new RegExp(`^(<@!?${senderID}>|${escapeRegex((threadSetting.hasOwnProperty("PREFIX")) ? threadSetting.PREFIX : PREFIX )})\\s*`);
if (!prefixRegex.test(body)) return;
const adminbot = require('./../../config.json');
if(!ADMINBOT.includes(senderID) && adminbot.adminOnly == true) {
if(!ADMINBOT.includes(senderID) && adminbot.adminOnly == true) return api.sendMessage('[ MODE ] - Chỉ admin bot mới có thể sử dụng bot', threadID, messageID)
}
const dataAdbox = require('./../../modules/commands/cache/data.json');
var threadInf = (threadInfo.get(threadID) || await Threads.getInfo(threadID));
const findd = threadInf.adminIDs.find(el => el.id == senderID);
if(dataAdbox.adminbox.hasOwnProperty(threadID) && dataAdbox.adminbox[threadID] == true && !ADMINBOT.includes(senderID) && !findd && event.isGroup == true ) return api.sendMessage('[ MODE ] - Chỉ admin nhóm mới được sử dụng bot!!', event.threadID, event.messageID)
if (userBanned.has(senderID) || threadBanned.has(threadID) || allowInbox == ![] && senderID == threadID) {
if (!ADMINBOT.includes(senderID.toString())) {
if (userBanned.has(senderID)) {
const { reason, dateAdded } = userBanned.get(senderID) || {};
return api.sendMessage(global.getText("handleCommand", "userBanned", reason, dateAdded), threadID, async (err, info) => {
await new Promise(resolve => setTimeout(resolve, 5 * 1000));
return api.unsendMessage(info.messageID);
}, messageID);
} else {
if (threadBanned.has(threadID)) {
const { reason, dateAdded } = threadBanned.get(threadID) || {};
return api.sendMessage(global.getText("handleCommand", "threadBanned", reason, dateAdded), threadID, async (err, info) => {
await new Promise(resolve => setTimeout(resolve, 5 * 1000));
return api.unsendMessage(info.messageID);
}, messageID);
}
}
}
}
const [matchedPrefix] = body.match(prefixRegex),
args = body.slice(matchedPrefix.length).trim().split(/ +/);
commandName = args.shift().toLowerCase();
var command = commands.get(commandName);
if (!command) {
var allCommandName = [];
const commandValues = commands['keys']();
for (const cmd of commandValues) allCommandName.push(cmd)
const checker = stringSimilarity.findBestMatch(commandName, allCommandName);
if (checker.bestMatch.rating >= 0.5) command = client.commands.get(checker.bestMatch.target);
else return api.sendMessage(global.getText("handleCommand", "commandNotExist", checker.bestMatch.target), threadID);
}
if (commandBanned.get(threadID) || commandBanned.get(senderID)) {
if (!ADMINBOT.includes(senderID)) {
const banThreads = commandBanned.get(threadID) || [],
banUsers = commandBanned.get(senderID) || [];
if (banThreads.includes(command.config.name))
return api.sendMessage(global.getText("handleCommand", "commandThreadBanned", command.config.name), threadID, async (err, info) => {
await new Promise(resolve => setTimeout(resolve, 5 * 1000))
return api.unsendMessage(info.messageID);
}, messageID);
if (banUsers.includes(command.config.name))
return api.sendMessage(global.getText("handleCommand", "commandUserBanned", command.config.name), threadID, async (err, info) => {
await new Promise(resolve => setTimeout(resolve, 5 * 1000));
return api.unsendMessage(info.messageID);
}, messageID);
}
}
if (command.config.commandCategory.toLowerCase() == 'nsfw' && !global.data.threadAllowNSFW.includes(threadID) && !ADMINBOT.includes(senderID))
return api.sendMessage(global.getText("handleCommand", "threadNotAllowNSFW"), threadID, async (err, info) => {
await new Promise(resolve => setTimeout(resolve, 5 * 1000))
return api.unsendMessage(info.messageID);
}, messageID);
var threadInfo2;
if (event.isGroup == !![])
try {
threadInfo2 = (threadInfo.get(threadID) || await Threads.getInfo(threadID))
if (Object.keys(threadInfo2).length == 0) throw new Error();
} catch (err) {
logger(global.getText("handleCommand", "cantGetInfoThread", "error"));
}
var permssion = 0;
var threadInfoo = (threadInfo.get(threadID) || await Threads.getInfo(threadID));
const find = threadInfoo.adminIDs.find(el => el.id == senderID);
if (ADMINBOT.includes(senderID.toString())) permssion = 2;
else if (!ADMINBOT.includes(senderID) && find) permssion = 1;
if (command.config.hasPermssion > permssion) return api.sendMessage(global.getText("handleCommand", "permssionNotEnough", command.config.name), event.threadID, event.messageID);
if (!client.cooldowns.has(command.config.name)) client.cooldowns.set(command.config.name, new Map());
const timestamps = client.cooldowns.get(command.config.name);;
const expirationTime = (command.config.cooldowns || 1) * 1000;
if (timestamps.has(senderID) && dateNow < timestamps.get(senderID) + expirationTime)
return api.setMessageReaction('😼', event.messageID, err => (err) ? logger('Đã có lỗi xảy ra khi thực thi setMessageReaction', 2) : '', !![]);
var getText2;
if (command.languages && typeof command.languages == 'object' && command.languages.hasOwnProperty(global.config.language))
getText2 = (...values) => {
var lang = command.languages[global.config.language][values[0]] || '';
for (var i = values.length; i > 0x2533 + 0x1105 + -0x3638; i--) {
const expReg = RegExp('%' + i, 'g');
lang = lang.replace(expReg, values[i]);
}
return lang;
};
else getText2 = () => {};
try {
const Obj = {};
Obj.api = api
Obj.event = event
Obj.args = args
Obj.models = models
Obj.Users = Users
Obj.Threads = Threads
Obj.Currencies = Currencies
Obj.permssion = permssion
Obj.getText = getText2
command.run(Obj)
timestamps.set(senderID, dateNow);
if (DeveloperMode == !![])
logger(global.getText("handleCommand", "executeCommand", time, commandName, senderID, threadID, args.join(" ") , (Date.now()) - dateNow), "[ DEV MODE ]");
return;
} catch (e) {
return api.sendMessage(global.getText("handleCommand", "commandError", commandName, e), threadID);
}
};
};