-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbot.js
executable file
·95 lines (83 loc) · 2.78 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
#!/usr/bin/env node
var proc=require('child_process').spawn;
var bot=new (require('net')).Socket();
var host="localhost";
var port=25639;
var delim="!";
var ex;
var playing=false;
bot.setEncoding('utf8');
var reconnect=function(){
bot.connect(port, host, function(){
console.log("Connected");
bot.write('clientnotifyregister schandlerid=0 event=any\r\n');
});
}
reconnect();
bot.on('data', function(data){
data=data.trim();
var args=data.split(" ");
var command=args.shift();
var params=argstojson(args);
if (command=="notifytextmessage"&¶ms.msg[0]==delim){
var usermsg=formatfrom(params.msg).trim().split(" ");
var usercommand=usermsg.shift().substring(1).toLowerCase();
var context={
args: usermsg,
bot: bot,
command: usercommand,
ex: ex,
from: params.invokername,
playing: playing,
callback: function(resargs) {
ex=proc(resargs.command, resargs.args, {
stdio: ['pipe', 'ignore', 'ignore']
});
playing=true;
console.log(params.invokername+": "+usercommand+" "+usermsg.join(" ")+" \nCOMMAND: "+resargs.command+" "+resargs.args.join(" "));
ex.on('exit', function(){
playing=false;
console.log("Stopped playback");
});
}
}
if (playing){
try{
require('./playing/'+usercommand)(context);
console.log(params.invokername+": "+usercommand+" "+usermsg.join(" "));
}
catch(e){
send(params.invokername+": "+((e.message.trim().toLowerCase().startsWith("cannot find module"))?usercommand+" cannot be done to currently playing file or URL":e.message));
}
}
else{
try{
require('./commands/'+usercommand)(context);
}
catch(e){
send(params.invokername+": "+((e.message.trim().toLowerCase().startsWith("cannot find module"))?usercommand+" is not a valid command":e.message));
}
}
}
});
bot.on('close', function(){
console.log("Connection closed. Retrying..");
reconnect();
});
var send=function(mes){
bot.write("sendtextmessage targetmode=2 msg="+formatto(mes)+"\r\n");
}
var formatto=function(message){
return message.replace(/ /g, '\\s');
}
var formatfrom=function(message){
return message.replace(/\\s/g, ' ').replace(/\\\//g,'/').replace(/\[URL\]/g, '').replace(/\[\/URL\]/g, '');
}
var argstojson=function(args){
var result={};
for (var i=0;i<args.length;i++){
var mapped=args[i].split("=");
result[mapped.shift()]=mapped.join('=');
}
return result;
}