-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdouyu.js
93 lines (90 loc) · 2.41 KB
/
douyu.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
const Live = require('./lib/live');
const database = require('./database');
const config = require('./config');
const shield = require('./shield');
const log = LOGGER('douyu');
const socket = require('./socket')();
exports = module.exports = function() {
var live = new Live(config.room_id);
socket.emitter.on('connection', (nsp, socket) => {
if (live.inited) {
socket.emit('login', {
message: '登陆成功',
room_id: live.room_id,
room: live.room
});
}
});
live.on('response', function(err, response) {
if (err) return log.error(err);
log.info(response);
});
live.on('welcome', function(response) {
var uid = response.body.uid;
var autoMusic = config.auto_music[uid];
database.addSong(autoMusic); // 自动点歌
database.getUser(response.body.uid, (user) => {
socket.getIO((io) => {
io.emit('danmu', {
type: 'welcome',
response: response,
user: user,
song: autoMusic
});
});
});
database.recordUser(response.body.uid, response.body.nn);
});
live.on('chat', function(response) {
if (shield(response)) return;
socket.getIO((io) => {
io.emit('danmu', {
type: 'chat',
response: response
});
});
// 点歌相关
var nickname = response.body.nn.trim();
var message = response.body.txt.trim();
message = message.replace(/#/g, '#');
database.writeFlag(response, nickname, message);;
database.writeSong(response, nickname, message);;
database.cutSong(response, nickname, message);;
});
live.on('gift', function(response) {
socket.getIO((io) => {
io.emit('danmu', {
type: 'gift',
response: response
});
});
database.recordGift(response.body.uid, response.body.nn);
});
live.on('deserve', function(response) {
socket.getIO((io) => {
io.emit('danmu', {
type: 'deserve',
response: response
});
});
database.recordDeserve(response);
});
live.on('message', function(response) {
var type = response.body.type;
socket.getIO((io) => {
io.emit('danmu', {
type: 'log',
response: response
});
if (type === 'loginres') {
io.emit('login', {
message: '登陆成功',
room_id: live.room_id,
room: live.room
});
}
});
log.info(response.raw);
});
return live;
};