-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathrandom.js
44 lines (38 loc) · 1016 Bytes
/
random.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
function verifyMinAndMax(min, max){
if(!min||!max){
Bot.sendMessage('Need pass min and max for random value');
return
}
if((typeof(min)!='number')||(typeof(max)!='number')){
Bot.sendMessage('Min and max must be Number type');
return
}
if(min>max){
Bot.sendMessage('Max must be creater then min');
return
}
return true;
}
function rndFloat(min, max){
if(!verifyMinAndMax(min, max)){ return }
return (Math.random() * (max - min + 1)) + min
}
function rndInt(min, max){
if(!verifyMinAndMax(min, max)){ return }
return Math.floor(rndFloat(min, max))
}
publish({
sendMessage: function (messages){
var err_msg = 'need pass messages array. E.g: [\"Hello\", \"Hi!\"]';
if(!messages){
Bot.sendMessage(err_msg);
}
if(!messages.length || messages.length==0){
Bot.sendMessage(err_msg);
}
let random_int = rndInt(0, messages.length-1);
Bot.sendMessage(messages[random_int]);
},
randomInt: rndInt,
randomFloat: rndFloat
})