-
Notifications
You must be signed in to change notification settings - Fork 27
/
walkie-talkie.js
160 lines (121 loc) · 4.37 KB
/
walkie-talkie.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const
_description = "Send and recieve messages from other rooms",
_help = {
"/hay": "Get a list of room ids for walkie-talkie messaging.",
"/hay [room id] [message]": "Send a message to another room. Ex: /hay boteshop Hi there"
},
NOTICE_RATE = 60000*5 // 5 minutes
;
var WalkieTalkiePlugin = function(data) {
var that = this;
that.bot = data.bot;
that.data = data;
that.help = _help;
that.lastNotices = {};
that.displayRooms = function(input, user, message){
var rooms = [];
if(!that.data.world) return;
rooms = that.getRooms(input,user,message);
rooms = rooms.map(function(r){
return {
text: r.title,
name: r.id
};
});
var msg = WalkieTalkiePlugin.formatOutput(
"Rooms",
"A list of room ids to use with /hay",
rooms,
{
name: "ID",
text: "Room"
}
);
that.bot.emit("do:commandResponsePM",msg.str,user, {
htmlMessage: msg.html
});
return;
};
that.getRooms = function(input, user, message) {
var rooms = [];
if(!that.data.world) return;
rooms = that.data.world.bots.filter(function(bc){
//Check disabled
if(bc.bot && bc.bot.disabledPlugins.indexOf("walkie-talkie") >= 0) {
return false;
}
//Check banned
if(bc.room && bc.room.banned && bc.room.banned.indexOf(user.uri) >= 0) {
return false;
}
return true;
}).map(function(bc){
if(bc.room) {
return {
title: bc.room.title || bc.room.name,
id: bc.room.handle || bc.room.jqbx_id
};
}
if(bc.bot && bc.bot.room) {
return {
title: bc.bot.room.name,
id: bc.bot.room.id
};
}
return null;
}).filter(function(r){
return (r && r!="" && r!=null);
});
return rooms;
};
that.isValidRoom = function(input, user, message) {
if(!input || !user) return false;
var
rooms = that.getRooms(input, user, message),
id = input.split(" ")[0]
;
if(!rooms || rooms.length <= 0) return false;
return rooms.find(function(r){
return (r && r.id && r.id == id);
});
};
that.commands = {
"/hay": function(input, user, message, isHelp) {
if(isHelp) {
var msg = WalkieTalkiePlugin.helpMessage();
that.bot.emit("do:commandResponsePM", msg.str, user, {
htmlMessage: msg.html
});
return;
}
if(!input || input == ""){
that.displayRooms(input, user, message);
return;
}
if(!that.isValidRoom(input,user,message)){
that.bot.emit("do:commandResponsePM","Invalid room. Use /hay for a list of rooms.",user);
return;
}
var
username = user.username || user.id || user.uri.split(":")[2],
roomMessage = "",
handle = that.data.room.handle || that.data.room.jqbx_id || that.bot.room.id,
title = that.data.room.title || that.bot.room.name || handle || "",
userMessage,
roomID = input.split(" ")[0].trim()
;
roomMessage += "Message from "+ title + ". ";
roomMessage += "Reply with /hay " + handle +" your message";
userMessage = username + " ("+title+"): " + message.replace("@"+that.bot.user.username,"").split(" ").slice(2).join(" ");
that.bot.emit("do:roomMessage", roomID, userMessage, user);
if(!that.lastNotices[roomID] || Date.now()-that.lastNotices[roomID] > NOTICE_RATE){
that.bot.emit("do:roomMessage", roomID, roomMessage, user, {type: "notice"});
that.lastNotices[roomID] = Date.now();
}
}
};
return that;
};
WalkieTalkiePlugin.description = _description;
WalkieTalkiePlugin.help = _help;
module.exports = WalkieTalkiePlugin;