-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathauto-response.js
More file actions
69 lines (56 loc) · 1.99 KB
/
auto-response.js
File metadata and controls
69 lines (56 loc) · 1.99 KB
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
/**
* Auto-Response template for Showdown ChatBot
* Install as an Add-on
*
* Edit "autoResponseChat" and "autoResponsePrivateMessage" functions
* in order to add more automated responses.
*/
'use strict';
const Text = Tools('text');
exports.setup = function (App) {
/**
* Automated response for chat messages
* @param {String} room - Chat room ID
* @param {Object} user - Object (id, name, group)
* @param {String} message - Message received
* @param {function} response - function you can call in order to send a response
*/
function autoResponseChat(room, user, message, response) {
// Code here to handle the chat messages responses
if (App.bot.rooms[room] && App.bot.rooms[room].type === 'battle') {
// We are in a battle
if (Text.toId(message).indexOf(Text.toId(App.bot.getBotNick())) >= 0 && (/(hi)|(hello)/gi).test(message)) {
return response("Hello " + user.name + "!"); // Say hello back
}
if (['gl', 'hf', 'good luck', 'have fun'].indexOf(message.toLowerCase().replace(/[^a-z\s]+/g, "")) >= 0) {
return response("Have fun!"); // Say have fun back in battle
}
}
}
/**
* Automated response for private messages
* @param {Object} user - Object (id, name, group)
* @param {String} message - Message received
* @param {function} response - function you can call in order to send a response
*/
function autoResponsePrivateMessage(user, message, response) {
// Code here to handle the private messages responses
}
/* Event handlers */
function chatHandler(room, time, by, message) {
autoResponseChat(room, Text.parseUserIdent(by), message, res => {
App.bot.sendTo(room, res);
});
}
App.bot.on('userchat', chatHandler);
function privateHandler(from, message) {
autoResponsePrivateMessage(Text.parseUserIdent(from), message, res => {
App.bot.pm(from, res);
});
}
App.bot.on('pm', privateHandler);
exports.destroy = function () {
App.bot.removeListener('userchat', chatHandler);
App.bot.removeListener('pm', privateHandler);
};
};