forked from seckela/groupme-roll-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
130 lines (90 loc) · 2.77 KB
/
bot.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
var HTTPS = require('https');
var botID = process.env.BOT_ID,
botCommand = /^\/roll/;
function respond() {
var request = JSON.parse(this.req.chunks[0]);
if(request.text && botCommand.test(request.text)){
commandHandler(this, request);
} else {
console.log("don't care");
this.res.writeHead(200);
this.res.end();
}
}
function toSignedString(num) {
if (isNaN(num) || num == 0) return '';
if (num < 0) return '' + num;
return '+' + num;
}
function toNumber(str) {
if (typeof str == "string") str = str.replace(/\s/g, '');
return parseInt(str);
}
function commandHandler(relThis, command){
var count = 1, min = 1, max = 100, pre = NaN, post = NaN, msg = "1-100", rolls = [], sum = 0;
if (args = command.text.match(/(\d+)(\s*[+-]?\s*\d+)?[dD](\d+)(\s*[+-]?\s*\d+)?/)) {
[count, pre, max, post] = args.slice(1).map(toNumber);
msg = count + toSignedString(pre) + "d" + max + toSignedString(post);
} else if (args = command.text.match(/(\d+)[\s-]+(\d+)(\s*[+-]?\s*\d+)?/)) {
[min, max, post] = args.slice(1).map(toNumber);
msg = min + "-" + max + toSignedString(post);
} else if (args = command.text.match(/[Mm]{2,}\s*[+-]?(\d+)?/)) {
var [level] = args.slice(1).map(toNumber);
level = level || 1;
max = 4;
count = 2 + level;
pre = 1;
msg = "Magic Missle Level " + level;
}
for (var i = 0; i < count; i++) {
var roll = min + Math.floor(Math.random()*(max-min+1));
rolls.push(roll);
sum += roll;
if (!isNaN(pre)) sum += pre;
}
if (!isNaN(post)) sum += post;
msg = "" + command.name + " rolled " + msg + ": " + sum;
if ((count > 1 || !isNaN(pre) || !isNaN(post)) && count < 50) {
msg += " [" + rolls.join(" ") + "]";
}
console.log({count, min, max, pre, post, sum, rolls, msg});
relThis.res.writeHead(200);
relThis.res.end();
setTimeout(function(){ postMessage(msg, command.name, command.user_id); }, 500);
}
function postMessage(message, name, id) {
var botResponse, options, body, botReq;
options = {
hostname: 'api.groupme.com',
path: '/v3/bots/post',
method: 'POST'
};
body = {
"bot_id" : botID,
"text" : message,
"attachments": [
{
"type": "mentions",
"user_ids": [id],
"loci": [
[0,name.length + 1]
]
}
]
};
botReq = HTTPS.request(options, function(res) {
if(res.statusCode == 202) {
//neat
} else {
console.log('rejecting bad status code ' + res.statusCode);
}
});
botReq.on('error', function(err) {
console.log('error posting message ' + JSON.stringify(err));
});
botReq.on('timeout', function(err) {
console.log('timeout posting message ' + JSON.stringify(err));
});
botReq.end(JSON.stringify(body));
}
exports.respond = respond;