This repository has been archived by the owner on Jul 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunication.new.js
78 lines (71 loc) · 2.62 KB
/
communication.new.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
/* eslint-disable no-unused-vars */
/* eslint-disable no-undef */
const Entities = require('html-entities').AllHtmlEntities;
const entities = new Entities();
const sd = require('silly-datetime');
const {
iiroseBot
} = require('./iirose-bot-ts/build/main');
const EventEmitter = require('events').EventEmitter;
const botchannel = new EventEmitter();
var logindata = JSON.parse(fs.readFileSync('./login.json'));
function switchRoom(roomid) {
logindata.roomid = roomid;
fs.writeFileSync('./login.json', JSON.stringify(logindata));
botchannel.emit('switchRoom', roomid);
}
function sendMsg(msg) {
botchannel.emit('chat', msg);
}
function sendDanmu(msg) {
alert('正在开发...');
}
const start = async () => {
console.log('starting bot...');
const connection = await iiroseBot({
username: logindata.login,
password: logindata.password,
roomId: logindata.roomid
}); /* 使用bot */
connection.on('PUBLIC_MESSAGE', (event) => {
console.log(event);
let user = event.message.user.username;
let content = event.message.content;
let id = event.message.id;
let userrank = event.message.user.rank;
printMessage(`[${userrank}] (${id}) ${user} said: ${content}`);
});
connection.on('USER_JOIN', (event) => {
console.log(event);
});
connection.on('USER_LEAVE', (event) => {
console.log(event);
});
connection.on('USER_SWITCH_ROOM', (event) => {
console.log(event);
});
connection.on('UPDATE_ROOM_STORE', (event) => {
document.getElementById('rooms').innerHTML = '';
connection.getRooms().forEach((room, i, rooms) => {
console.log(room);
document.getElementById('rooms').innerHTML = document.getElementById('rooms').innerHTML + `<button onclick="switchRoom('${room.id}')">点击切换</button>房间:${room.name}<br/>`;
});
});
botchannel.on('switchRoom', (roomid) => {
connection.switchRoom(roomid);
setTimeout(() => {
location.reload();
}, 1000);
});
botchannel.on('chat', (msg) => connection.createMessage({
content: msg,
color: connection.color
}));
function printMessage(msg) {
var time = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
console.log(`[${time}] [${process.uptime()}] ${msg}`);
document.getElementById('messages').value = document.getElementById('messages').value + `[${time}] ${msg}` + '\n';
document.getElementById('messages').scrollTop = document.getElementById('messages').scrollHeight;
}
};
start().then(() => console.log('bot started'));