Skip to content

Commit f951a02

Browse files
author
cmd1152
committed
:(
1 parent 250595d commit f951a02

File tree

2 files changed

+65
-17
lines changed

2 files changed

+65
-17
lines changed

HackChat-BotLib.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,27 @@ class Client {
126126
hc.nick = updateBy.nick;
127127
hc.trip = updateBy.trip;
128128
}
129+
if (this.cmdstart !== false) {
130+
let originText = hc.text;
131+
if (hc.cmd == "whisper") originText = hc.msg; //前面有扩展;
132+
if (originText.startsWith(this.cmdstart)) {
133+
let originCmd = originText.substring(this.cmdstart.length).split(" ");
134+
let runcmd = originCmd.shift();
135+
this._COMMAND(
136+
runcmd,
137+
originCmd,
138+
this.selNick(hc.nick),
139+
hc.cmd == "whisper"?(text,e=false)=>{this.whisper(hc.nick,`${e?"\x00\n":""}${text}`)}:(text,e)=>{this.chat(`${e?"":`@{hc.nick} `}${text}`)},
140+
hc.cmd == "whisper"
141+
)
142+
}
143+
}
129144
this.onmessage(event.data,hc);
130145
}
131146
["onclose","onchangechannel","joinfailed","oncaptcha","onjoin","onjoined","onmessage"].forEach(func=>{
132147
this[func] = () => {}
133148
});
134-
["channel","nick","joined","ping","pingtime","check"].forEach(ve=>{
149+
["channel","nick","joined","ping","pingtime","check","cmdstart","command"].forEach(ve=>{
135150
this[ve] = false
136151
});
137152
this.customId = 0;
@@ -212,6 +227,42 @@ class Client {
212227
text: text
213228
})
214229
}
230+
reply(obj) { //来源十字街,有改动
231+
let replyText = '';
232+
let originalText = args.text;
233+
let overlongText = false;
234+
if (originalText.length > 152) {
235+
replyText = originalText.slice(0, 152);
236+
overlongText = true;
237+
}
238+
if (args.trip) {
239+
replyText = '>' + args.trip + ' ' + args.nick + ':\n';
240+
} else {
241+
replyText = '>' + args.nick + ':\n';
242+
}
243+
originalText = originalText.split('\n');
244+
if (originalText.length >= 8) {
245+
originalText = originalText.slice(0, 8);
246+
overlongText = true;
247+
}
248+
for (let replyLine of originalText) {
249+
if (!replyLine.startsWith('>>')) replyText += '>' + replyLine + '\n';
250+
}
251+
if (overlongText) replyText += '>……\n';
252+
replyText += '\n';
253+
var nick = args.nick
254+
replyText += '@' + nick + ' ';
255+
return replyText;
256+
}
257+
_COMMAND(cmd, args, info, back, whisper) {
258+
if (this.command[cmd]) {
259+
try {
260+
back(this.command[cmd].run(args,info,back,whisper));
261+
} catch (e) {
262+
back(`命令执行出错:${e.message}`);
263+
}
264+
} else back("命令未找到");
265+
}
215266
}
216267

217268
module.exports = Client

test.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
let Client = require("./HackChat-BotLib.js");
2-
let client = new Client("wss://hack.chat/chat-ws");
3-
let join_channel = "your-channel";
2+
let client = new Client("wss://hack.chat/chat-ws"); //104.131.138.176
3+
let join_channel = "lounge";
44

55
client.onjoin = () => {
66
client.join({
@@ -15,20 +15,17 @@ client.onmessage = (raw, json) => {
1515
}
1616

1717
client.onjoined = () => {
18-
client.chat("Hello, world!");
19-
let id = client.getcustomId();
20-
client.chat("UpdateMessage Test",id);
21-
setTimeout(()=>{
22-
client.updatemessage(id,'overwrite','good')
23-
},3000);
24-
console.log("加入成功");
18+
client.chat("Hello, World!");
2519
}
2620

27-
client.ping = 10000;
28-
client.onchangechannel = (channel) => {
29-
client.close("被踢出或者移动到了"+channel);
30-
}
31-
32-
client.onclose = (reason) => {
33-
console.log(`连接断开`,reason)
21+
client.onmessage = (raw, json) => {
22+
if (json.cmd == "chat" && json.text.includes("6") && json.nick !== client.nick) {
23+
let id = client.getcustomId()
24+
client.chat(client.reply(json),id);
25+
["我注意到","你说了", "一句“6”",",","最近你", "过得如何?"].forEach((t,i)=>{
26+
setTimeout(()=>{
27+
client.updatemessage(id,'append',t);
28+
},i*3000)
29+
})
30+
}
3431
}

0 commit comments

Comments
 (0)