-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.js
87 lines (76 loc) · 2.17 KB
/
utils.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
exports = module.exports = {};
let mongo = require('./mongo');
let config = require('./config');
let agent = require('superagent');
const getCommand = (text) => {
let command = text.split(' ');
command.shift();
return command.join(' ');
}
exports.getCommand = getCommand;
const sendMessage = async (messageBody) => {
let agentRes = await agent.post(`${config.coolq.host}:${config.coolq.port}/send_msg`).send(messageBody);
if (agentRes.body.status !== 'ok') {
console.error(agentRes.body);
}
}
exports.sendMessage = sendMessage;
const getGroupList = async () => {
let agentRes = await agent.get(`${config.coolq.host}:${config.coolq.port}/get_group_list`);
if (agentRes.body.status !== 'ok') {
console.error(agentRes.body);
}
return agentRes.body.data;
}
exports.getGroupList = getGroupList;
const leaveGroup = async (groupId) => {
con
let agentRes = await agent.post(`${config.coolq.host}:${config.coolq.port}/set_group_leave`).send({
group_id: groupId,
});
if (agentRes.body.status !== 'ok') {
console.error(agentRes.body);
}
}
exports.leaveGroup = leaveGroup;
let randomSplit = (value, part) => {
let pointList = [value];
while (pointList.length < part) {
let point = Math.floor(Math.random() * value)
if (!pointList.includes(point) && point) pointList.push(point);
}
pointList.sort((a, b) => a - b);
let optPoint = [pointList[0]];
for (let i = 0; i < pointList.length - 1; i++) {
optPoint.push(pointList[i + 1] - pointList[i]);
}
return optPoint;
}
exports.randomSplit = randomSplit;
let dice = (times, max) => {
let opt = [];
for (let i = 0; i < times; i++) {
let value = Math.ceil(Math.random() * max);
opt.push(value);
}
return opt;
}
exports.dice = dice;
let diceSum = (times, max) => {
let opt = [];
for (let i = 0; i < times; i++) {
let value = Math.ceil(Math.random() * max);
opt.push(value);
}
return opt.reduce((a, b) => a + b, 0);
}
exports.diceSum = diceSum;
let random = (from, to) => {
let max = to - from + 1;
return Math.floor(Math.random() * max) + from;
}
exports.random = random;
let isAdmin = (userId) => {
return config.adminIdList.includes(userId);
}
exports.isAdmin = isAdmin;